Search code examples
excelcopy-pasteoffsetvba

Select last entry and offset by 1 row then paste value not working


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.


Solution

  • 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