Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50273

Copy and paste as values in vba loop code

$
0
0
I have asked this question on both Ozgrid and Mr.Excel:
http://www.ozgrid.com/forum/showthre...kakkmaddafakka

http://www.mrexcel.com/forum/excel-q...loop-code.html
(could not find out how to attach files...)

Anyways, my problem is this:

FYI:
I have one masterbook with one sheet. I have a folder of about 1000 workbooks that are identical layout-wise, and their sheets are identical layout, naming and number of sheets-wise, the difference lies in the numbers within these books' sheets. I want to copy specific cells from these book's sheets; there are three specific sheets I want the code to loop through for each book, if they contain numbers/text in specified cells then I want to copy specific cells. For some of these books two out the three sheets are desirable to copy from due to existence of info in them. Some of the cells are: text, values and values as a result of an in-cell and in-sheet formula.

I have a code that is working fine with respect to all the above, except for managing to copy/pasting cells that are a result of an in-cell and in-sheet formula. Below you can see the code (which I received a lot of help with from here: http://www.ozgrid.com/forum/showthre...kakkmaddafakka , but I didn't receive any help on this topic):

Code:

Option Explicit
Option Base 1
Sub ImpData()
    Dim sPath As String
    Dim sFil As String
    Dim strName As String
    Dim owbk As Workbook
    Dim twbk As Workbook
    Dim lw As Long
    Dim ar As Variant
    Dim arrn As Variant
    Dim arsh As Variant
    Dim i As Integer
    Dim j As Integer
   
    Application.ScreenUpdating = False
    arsh = Array("E2 Test Cycle", "E3 Test Cycle ", "D2 Test Cycle") 'Need space after "E3 Test Cycle_"
    ar = Array("C", "D", "I", "N", "S", "X")
    arrn = Array("D2", "D5:D9", "D14:D18", "E25:I25", "E26:I26", "E27:I27")
    Set twbk = ActiveWorkbook
    sPath = "H:\Motor database\EIAPP test\" 'Change to suit - test folder with 6 books
    sFil = Dir(sPath & "*.xls") 'Note it opens csv format
   
    Do While sFil <> ""
        strName = sPath & sFil
        Set owbk = Workbooks.Open(strName)
        For j = 1 To 3
            If Sheets(arsh(j)).Range("D14").Value > 0 Then
                twbk.Sheets(1).Range("A" & Rows.Count).End(xlUp)(2).Value = Sheets(arsh(j)).Range("D2").Value
                For i = 1 To 6
                    If i <= 3 Then
                        Sheets(arsh(j)).Range(arrn(i)).Copy
                        twbk.Sheets(1).Range(ar(i) & Rows.Count).End(xlUp)(2).PasteSpecial 12, Transpose:=True
                    Else
                        Sheets(arsh(j)).Range(arrn(i)).Copy twbk.Sheets(1).Range(ar(i) & Rows.Count).End(xlUp)(2)
                    End If
                Next i
            End If
        Next j
        owbk.Close False 'Close don't save
        sFil = Dir
    Loop
End Sub

Question:
NOTE! View attached files:
"1.xls" is the type of book I have 1000 of, but it has been modified and cropped; deleted all sheets but one, deleted about 150 rows, to get below the size constraint for upload. "Input2-0.xlsm" is the master book to copy into.

When I want to copy another row of cells, e.g. "E46:I46" I make the following adjustment to the code (*) and it works:

Code:

ar = Array("C", "D", "I", "N", "S", "X", "AC"*)
arrn = Array("D2", "D5:D9", "D14:D18", "E25:I25", "E26:I26", "E27:I27", "E46:I46"*)
Set twbk = ActiveWorkbook
sPath = "H:\Motor database\EIAPP test\" 'Change to suit
sFil = Dir(sPath & "*.xls") 'Note it opens csv format
 
Do While sFil <> ""
    strName = sPath & sFil
    Set owbk = Workbooks.Open(strName)
    For j = 1 To 3
        If Sheets(arsh(j)).Range("D14").Value > 0 Then
            twbk.Sheets(1).Range("A" & Rows.Count).End(xlUp)(2).Value = Sheets(arsh(j)).Range("D2").Value
            For i = 1 To 7*

But when i e.g. want to copy "E70:I70" which are a result of an in-cell formula (and in-sheet), I only get long columns with 0s and #refs.

Any idea how to modify the copy/paste statement? I have tried without luck...

THANKS!
Attached Files

Viewing all articles
Browse latest Browse all 50273

Latest Images

Trending Articles



Latest Images