Copying data from many workbooks to a summary workbook with Excel-VBA. Run time errors

后端 未结 1 1314
执笔经年
执笔经年 2021-01-28 16:56

I have files in a folder and I want to copy data from these files and paste them into another Master workbook sheet.

I keep getting a runtime error ‘1004’:

相关标签:
1条回答
  • 2021-01-28 17:21

    Here is a template for what you'd like done. NOTE that forward slashes can cause run time error b/c vba handles them in an annoying way.

     Sub DougsLoop()
         Dim wbk As Workbook
         Dim Filename As String
         Dim path As String
         Dim rCell As Range
         Dim rRng As Range
         Dim wsO As Worksheet
         Dim StartTime As Double
         Dim SecondsElapsed As Double
         Dim sheet As Worksheet
    
         Application.ScreenUpdating = False 'these three statements help performance by disabling the self titled in each, remeber to re-enable at end of code
         Application.DisplayAlerts = False
         Application.Calculation = xlCalculationManual
    
         StartTime = Timer 'Starts timer to see how long code takes to execute. I like having this in macors that loop through files
    
         path = "C:\Users\jjordan\Desktop\Test Dir\GA Test" & "\" 'pay attention to this line of code********
         Filename = Dir(path & "*.xl??")
         Set wsO = ThisWorkbook.Sheets("Sheet5")
    
         Do While Len(Filename) > 0 'this tells the code to stop when there are no more files in the destination folder
             DoEvents
             Set wbk = Workbooks.Open(path & Filename, True, True)
                 For Each sheet In ActiveWorkbook.Worksheets
                    Set rRng = sheet.Range("a2:n100")
                    For Each rCell In rRng.Cells
                        wsO.Cells(wsO.Rows.count, 1).End(xlUp).Offset(1, 0).Value = rCell
                    Next rCell
                 Next
             wbk.Close False
             Filename = Dir
         Loop
    
         Application.ScreenUpdating = True
         Application.DisplayAlerts = True
         Application.Calculation = xlCalculationAutomatic
         SecondsElapsed = Round(Timer - StartTime, 2)
         MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
     End Sub
    

    alter to this to your needs and you'll find it works perfectly :)

    EDIT: Also in your code you make use of COPY & PASTE a lot. Try avoid doing this in the future. Try doing something:

     ThisWorkbook.Sheets("Sheet1").Range("a1").Value = OtherWork.Sheets("Sheet1").Range("a1").Value
    

    This is more efficient and wont bog down your code as much.

    here is some offset logic

     wsO.Cells(wsO.Rows.count, 1).End(xlUp).Offset(1, 0).Value =
     wsO.Cells(wsO.Rows.count, 1).End(xlUp).Offset(0, 1).Value = 
     wsO.Cells(wsO.Rows.count, 1).End(xlUp).Offset(0, 2).Value = 
    

    notice the Offset(x,y) value? Essentially x is down and y is right. this is of course referencing the original position. So to get a value to go in the same row but three columns over you would use "Offset(0,3)" etc etc

    Ill let you alter your code to do this. :)

    I guess actually trying to piece it together was a struggle? Here this version assumes the macro is in the master workbook(and that youre running it form the master). If you want to change go ahead, but this is as far as I go. At some point, you'll have to experiment on your own.

    0 讨论(0)
提交回复
热议问题