Dear Excel Community!
I hope everyone is keeping well!
I have been having a serious issue with the following VBA code. If someone could very kindly help I would be extremely grateful!! Thank you all.
Objective:
[Copy] Data 'usedrange' from cell A2 (Down to last row and across to last column of data) from source workbook in worksheet with name [Sheet1] > and then > [Paste] in target workbook Masterfile with the same name [Sheet1] under the latest data in that table.
[Copy] Data 'usedrange' from cell A2 (Down to last row and across to last column of data) from source workbook in worksheet with name [Sheet2] > and then > [Paste] in target workbook Masterfile with the same name [Sheet2] under the latest data in that table.
...
[Copy] Data 'usedrange' from cell A2 (Down to last row and across to last column of data) from source workbook in worksheet with name [SheetN] > and then > [Paste] in target workbook Masterfile with the same name [SheetN] under the latest data in that table.
I need to loop this for all matching sheet names in source file to Masterfile.
So the sheet name will be the unique identifier.
Workbook Structure:
I have multiple sheets in the source workbook which are named: Sheet1, Sheet2, Sheet3, ..., SheetN. There may be more or fewer sheets in this workbook.
I have around 50 sheets in the Target Masterfile Workbook which are named: Sheet1, Sheet2, Sheet3, ..., Sheet40.
The sheets of data I want to copy from source WB will have exactly the same name as the Sheets in the Masterfile WB. So this should make things simple.
My VBA code works superbly to do exactly the above, but only if the source workbook has the same number of sheets in the same order as the Masterfile workbook.
But what I need is for the Macro to 'Lookup and MATCH' the worksheet name and match this with the worksheet name in Masterfile and then copy the data across under the last data in Masterfile sheet corresponding to source sheet from source workbook.
Please let me know if anyone has some ideas? This has been challenging me for the whole weekend!!
If you need more information or clarification, please let me know!
Thank you and I hope to hear from the community!!
Manerlao
I hope everyone is keeping well!
I have been having a serious issue with the following VBA code. If someone could very kindly help I would be extremely grateful!! Thank you all.
Objective:
[Copy] Data 'usedrange' from cell A2 (Down to last row and across to last column of data) from source workbook in worksheet with name [Sheet1] > and then > [Paste] in target workbook Masterfile with the same name [Sheet1] under the latest data in that table.
[Copy] Data 'usedrange' from cell A2 (Down to last row and across to last column of data) from source workbook in worksheet with name [Sheet2] > and then > [Paste] in target workbook Masterfile with the same name [Sheet2] under the latest data in that table.
...
[Copy] Data 'usedrange' from cell A2 (Down to last row and across to last column of data) from source workbook in worksheet with name [SheetN] > and then > [Paste] in target workbook Masterfile with the same name [SheetN] under the latest data in that table.
I need to loop this for all matching sheet names in source file to Masterfile.
So the sheet name will be the unique identifier.
Workbook Structure:
I have multiple sheets in the source workbook which are named: Sheet1, Sheet2, Sheet3, ..., SheetN. There may be more or fewer sheets in this workbook.
I have around 50 sheets in the Target Masterfile Workbook which are named: Sheet1, Sheet2, Sheet3, ..., Sheet40.
The sheets of data I want to copy from source WB will have exactly the same name as the Sheets in the Masterfile WB. So this should make things simple.
My VBA code works superbly to do exactly the above, but only if the source workbook has the same number of sheets in the same order as the Masterfile workbook.
But what I need is for the Macro to 'Lookup and MATCH' the worksheet name and match this with the worksheet name in Masterfile and then copy the data across under the last data in Masterfile sheet corresponding to source sheet from source workbook.
VBA Code:
Sub AddDataToMasterfile()
Dim fName As Variant, sh As Worksheet, wb As Workbook
CYCLE:
fName = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", Title:="Please select a file")
If fName = False Then Exit Sub
Set wb = Workbooks.Open(fName)
For Each sh In ThisWorkbook.Sheets
wb.Sheets(sh.Name).UsedRange.Offset(1).Copy sh.Cells(Rows.Count, 1).End(xlUp)(2)
Next
ans = MsgBox("Workbook " & Mid(fName, InStrRev(fName, "\") + 1) & " is incorporated. Do you want to add more files?", _
vbYesNo, "Add more?")
wb.Close False
If ans = vbYes Then GoTo CYCLE:
Worksheets("Sheet1").Select
MsgBox ("Workbook is now Ready.")
End Sub
Please let me know if anyone has some ideas? This has been challenging me for the whole weekend!!
If you need more information or clarification, please let me know!
Thank you and I hope to hear from the community!!
Manerlao