I'm pretty new to vba and usually able to google my way through solutions. I've even done this process before successfully in other workbooks but I'm stuck
What I'm trying to do is:
From original WB (wbThisWB), ask user to open file (wbImportWB), copy data from A3:AB Last Row. Paste to the end of rows in the first workbook (wbThisWB)
If I remove error handling, on the red line above, I am getting the error:
Runtime Error: '9':
Subscript out of range
Note that the sheet name is the same on both workbooks. I've also tried changing Sheets("CBS") to Sheets("Sheet1") and in this case, it runs to the end but does not paste the data.
If anyone can can see where I've gone wrong, I'd appreciate some insight so much.
What I'm trying to do is:
From original WB (wbThisWB), ask user to open file (wbImportWB), copy data from A3:AB Last Row. Paste to the end of rows in the first workbook (wbThisWB)
VBA Code:
Dim wbThisWB As Workbook
Dim wbImportWB As Workbook
Dim strFullPath As String
Dim ImportLR As Long
Dim ThisLR As Long
Set wbThisWB = ThisWorkbook
wbThisWB.Activate
Sheets("Sheet1").Select
ThisLR = Range("A" & Rows.Count).End(xlUp).Row
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Please select a file to open:"
.Show
On Error Resume Next
strFullPath = .SelectedItems(1)
If Err.Number <> 0 Then
wbThisWB = Nothing
Exit Sub
End If
On Error GoTo 0
End With
Application.ScreenUpdating = False
Set wbImportWB = Workbooks.Open(strFullPath)
wbImportWB.Activate
ImportLR = Range("A" & Rows.Count).End(xlUp).Row
' On Error Resume Next
wbImportWB.Sheets("CBS").Range("A3:AB" & ImportLR).Copy
[COLOR=rgb(184, 49, 47)] [B] wbThisWB.Sheets("CBS").Range("A" & ThisLR).PasteSpecial Paste:=xlPasteValues[/B][/COLOR]
'On Error GoTo 0
wbImportWB.Close False
Set wbThisWB = Nothing
Set wbImportWB = Nothing
If I remove error handling, on the red line above, I am getting the error:
Runtime Error: '9':
Subscript out of range
Note that the sheet name is the same on both workbooks. I've also tried changing Sheets("CBS") to Sheets("Sheet1") and in this case, it runs to the end but does not paste the data.
If anyone can can see where I've gone wrong, I'd appreciate some insight so much.
Last edited by a moderator: