Hello,
I am trying to use VBA to copy the entire contents (data) of "Sheet1" from multiple workbooks located under a single folder to a master workbook named Headcount Rollup Template. Each sheet should be imported to a specific tab in the master workbook.
I have set up a sheet within my master called "Tab Names" that contains the table I am referencing to map the file to the sheet tab name. It is set up like this:
Column A
Wbk Name
Active HC - US Data.xls
Active HC - UK Data.xls
Active HC - France Data.xls
Column B
Sht Name
US HC
UK HC
FRA HC
Example: all data in "Sheet1" in Active HC - US Data.xls should be copied to tab "US HC" in master workbook
I have been working with the code below but getting an "Object Required" error on this line: "Wbk.Sheets("Sheet1").Cells.Copy Destination:=sht.[A1]" when it runs.
Here is my current code that returns an object required error:
Sub ImportSheets()
Dim Path As String
Dim filename As String
Dim sht As Worksheet
Dim wkB As Workbook
Dim i As Integer
Dim r As Range
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
'Import the first worksheet for each file under R:\HC Data
Path = "R:\HR Ops Team Folders\Reporting & Analysis\Headcount\HC Data"
filename = Dir(Path & "\*.xls")
i = 1
Do While filename <> ""
For Each r In Worksheets("Tab Names").Range("A2:A9")
If r.Value = filename Then
'this assumes that the sheet name is in the next column
Set sht = ThisWorkbook.Sheets(r.Offset(0, 1).Value)
Exit For
End If
Next
Workbooks.Open filename:=Path & "\" & filename
If SheetExists("Sheet1") Then
Set wkB = ActiveWorkbook
Wbk.Sheets("Sheet1").Cells.Copy Destination:=sht.[A1]
'ActiveSheet.Name = "Sheet" & i
i = i + 1
wkB.Close savechanges:=False
Else
MsgBox "Sheets1 does not exist in file '" & filename & "'", vbExclamation, "ERROR !!"
ActiveWorkbook.Close savechanges:=False
End If
filename = Dir
Loop
Sheets("Tracking #").Select
MsgBox "All files have been imported successfully!"
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
__________________________________________________ _________
Function SheetExists(ByVal sName As String)
Dim sht As Worksheet
SheetExists = True
For Each sht In ActiveWorkbook.Sheets
If sht.Name = sName Then Exit Function
Next sht
SheetExists = False
End Function
I am trying to get the code to loop through and map the file name to the tab name and copy the data in the tab location of the mast worksheet.
Any help is greatly appreciated!!
<!-- / message -->
I am trying to use VBA to copy the entire contents (data) of "Sheet1" from multiple workbooks located under a single folder to a master workbook named Headcount Rollup Template. Each sheet should be imported to a specific tab in the master workbook.
I have set up a sheet within my master called "Tab Names" that contains the table I am referencing to map the file to the sheet tab name. It is set up like this:
Column A
Wbk Name
Active HC - US Data.xls
Active HC - UK Data.xls
Active HC - France Data.xls
Column B
Sht Name
US HC
UK HC
FRA HC
Example: all data in "Sheet1" in Active HC - US Data.xls should be copied to tab "US HC" in master workbook
I have been working with the code below but getting an "Object Required" error on this line: "Wbk.Sheets("Sheet1").Cells.Copy Destination:=sht.[A1]" when it runs.
Here is my current code that returns an object required error:
Sub ImportSheets()
Dim Path As String
Dim filename As String
Dim sht As Worksheet
Dim wkB As Workbook
Dim i As Integer
Dim r As Range
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
'Import the first worksheet for each file under R:\HC Data
Path = "R:\HR Ops Team Folders\Reporting & Analysis\Headcount\HC Data"
filename = Dir(Path & "\*.xls")
i = 1
Do While filename <> ""
For Each r In Worksheets("Tab Names").Range("A2:A9")
If r.Value = filename Then
'this assumes that the sheet name is in the next column
Set sht = ThisWorkbook.Sheets(r.Offset(0, 1).Value)
Exit For
End If
Next
Workbooks.Open filename:=Path & "\" & filename
If SheetExists("Sheet1") Then
Set wkB = ActiveWorkbook
Wbk.Sheets("Sheet1").Cells.Copy Destination:=sht.[A1]
'ActiveSheet.Name = "Sheet" & i
i = i + 1
wkB.Close savechanges:=False
Else
MsgBox "Sheets1 does not exist in file '" & filename & "'", vbExclamation, "ERROR !!"
ActiveWorkbook.Close savechanges:=False
End If
filename = Dir
Loop
Sheets("Tracking #").Select
MsgBox "All files have been imported successfully!"
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
__________________________________________________ _________
Function SheetExists(ByVal sName As String)
Dim sht As Worksheet
SheetExists = True
For Each sht In ActiveWorkbook.Sheets
If sht.Name = sName Then Exit Function
Next sht
SheetExists = False
End Function
I am trying to get the code to loop through and map the file name to the tab name and copy the data in the tab location of the mast worksheet.
Any help is greatly appreciated!!
<!-- / message -->