I have three files. One excel file that is empty and contains my Macro. Second excel file that has about 20 tabs with updated data (Variable "UpdatedFiles" contains the file path) that needs to by copy/pasted into my third excel file (Variable "ProvisionFiles" contains the file path), that has extra tabs that link to the tabs that I am copy/pasting in.
My code works great right up to the point that I hit the Copy/Paste Section of my For Each Loop. Note that the tabs that I am copying over have the overlapping/same tab names in both workbooks.
I have tried to copy/paste data with all three methods described in this video. https://www.excelcampus.com/vba/copy-paste-cells-vba-macros/
Still can't get it to work.
`Sub CopyPasteData()
Dim ProvisionFile As String 'String File Path of Provision File
Dim UpdatedFile As String 'String File Path of Updated OneSource Files
Dim ws As Worksheet 'Used to Loop Though WS Tabs in Updated OneSource Files
Dim wsName As String 'Name of Tab of OneSource File
Dim lastRow As Long
Dim lastColumn As Long
'Open Dialog Box that allows you to Select the Provision File
MsgBox "Select your provision file, which is the destination for the updated OneSource Reports. Please ensure this file is closed before opening."
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Select file"
.InitialFileName = "C:\"
If .Show = -1 Then
'ok clicked
ProvisionFile = .SelectedItems(1)
Workbooks.Open(ProvisionFile).Activate
'Worksheets("Control").Activate
Else
'cancel clicked
End If
End With
'Get updated Reports
MsgBox "Select the file that contains the updated OneSource Reports."
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Select file"
.InitialFileName = "C:\"
If .Show = -1 Then
'ok clicked
UpdatedFile = .SelectedItems(1)
Workbooks.Open(UpdatedFile).Activate
Else
'cancel clicked
End If
End With
'Loop through Each tab in Updated File
For Each ws In Worksheets
wsName = ws.Name
lastRow = Sheets(wsName).Cells(Rows.Count, 1).End(xlUp).Row
lastColumn = Sheets(wsName).Cells(7, Columns.Count).End(xlToLeft).Column
'Debug.Print ("Yes")
Workbooks(ProvisionFile).Worksheets(wsName).Range(Workbooks(ProvisionFile).Worksheets(wsName).Cells(1, 1), Workbooks(ProvisionFile).Worksheets(wsName).Cells(lastRow, lastColumn)) = Sheets(wsName).Range(Sheets(wsName).Cells(1, 1), Sheets(wsName).Cells(lastRow, lastColumn))
Next ws
End Sub`
Expected result is to finish the copy/paste loop.
Error I am recieving = Run-time error'9': Subscript out of range