0

I'm having Run time error 1004 with this code.

Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and     perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog




'Optimize Macro Speed
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual

'Retrieve Target Folder Path From User
  Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

With FldrPicker
  .Title = "Select A Target Folder"
  .AllowMultiSelect = False
    If .Show <> -1 Then GoTo NextCode
    myPath = .SelectedItems(1) & "\"
End With

   'In Case of Cancel
NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
  myExtension = "*.xls*"

'Target Path with Ending Extention
  myFile = Dir(myPath & myExtension)

    'Loop through each Excel file in folder
  Do While myFile <> ""
'Set variable equal to opened workbook
  Set wb = Workbooks.Open(Filename:=myPath & myFile)

'Ensure Workbook has opened before moving on to next line of code
  DoEvents

Dim LastRow As Long
Dim rng1 As Range
wb.Worksheets(1).Activate
Set rng1 = Range("B15:E81,N15:O81")

With ThisWorkbook.Worksheets("Sheet1") '<-- modify "Sheet1" to your sheet's name
LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row ' get last row with data in column "E"
' paste
.Range("E" & LastRow + 1) = rng1
End With

'Save and Close Workbook
  wb.Close SaveChanges:=True

'Ensure Workbook has closed before moving on to next line of code
  DoEvents

'Get next file name
  myFile = Dir
  Loop

'Message Box when tasks are completed
  MsgBox "Task Complete!"

ResetSettings:
  'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub
  1. I'm using this segment of code to extract data from B15 to E81 from all the excel workbook in the folder.

  2. Upon copying, it will activate the workbook where this code lie

  3. Select last entry from column E

  4. Offset by 1 row

  5. Paste selection to column activated cell

Appreciate all the help I could find. Thanks in advance.

Community
  • 1
  • 1
Tyler
  • 604
  • 3
  • 10
  • 24

2 Answers2

2

First, as suggested by @teylyn, you should avoid using Select and Activate (99.9% of the time they are not needed, and the only thing they do "contribute" is time wasting, since it takes the code longer to run).

Second, you should also specify which Worksheet you want to paste in ThisWorkbook object.

Code

Dim LastRow As Long

wb.Worksheets(1).Range("B15:E81").Copy

With ThisWorkbook.Worksheets("Sheet1") '<-- modify "Sheet1" to your sheet's name
    LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row ' get last row with data in column "E"

    ' paste
    .Range("E" & LastRow + 1).PasteSpecial Paste:=xlPasteValues
End With
Shai Rado
  • 33,032
  • 6
  • 29
  • 51
  • 1
    Thirdly, use `xlUp` instead of `xlDown`. (I'm pretty certain that is the source of the error, and you automatically fixed it in your code, but forgot to mention it as the likely cause.) – YowE3K Dec 07 '17 at 08:00
  • Your code works great but it encounter error after the 3rd file operation, with the pastevalue code. I come up with rng1 = Range("B15:E81") to replace how i copy using the post suggested by @teylyn. But how do i paste into the next empty cell of column E? – Tyler Dec 07 '17 at 08:24
  • @Tyler the line `.Range("E" & LastRow + 1).PasteSpecial Paste:=xlPasteValues` after it finds the updated `LastRow` is how you paste in the next empty row. I think the error might be coming from another place in your code. Please share the other relevant parts of your code. – Shai Rado Dec 07 '17 at 08:37
  • https://www.dropbox.com/s/xy3wtbcc4fjf1d8/LoopExcel.bas?dl=0 You can access the module from the link above. I add rng1 to .Range("E" & LastRow + 1) = rng1 and nothing happen. You can check the edit i made from the module itself anyway – Tyler Dec 07 '17 at 08:48
  • @Tyler i can;t access Dropbox from current PC, sorry – Shai Rado Dec 07 '17 at 08:49
  • How do I share with you in this case? – Tyler Dec 07 '17 at 08:50
  • @Tyler you don't share on **SO**, you need to edit the code in your post above. – Shai Rado Dec 07 '17 at 08:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/160687/discussion-between-tyler-and-shai-rado). – Tyler Dec 07 '17 at 08:57
1

Your code snippet runs without errors for me. You may want to rethink the approach, though. Code that uses Activate and Select is slow and inefficient. In most cases Activate and Select are not required. The objects can be addressed directly.

See this question for techniques to avoid Select and Activate.

teylyn
  • 34,374
  • 4
  • 53
  • 73