Hello, I am trying to copy worksheets, all named "Sheet 1" from multiple workbooks into my current workbook onto a named worksheet for each. All the files I would like to import from are under one folder R:\HC Data.
I found this code at this site:
This works great but it copies each sheet from workbooks under a folder into the the current workbook to Sheet 1, Sheet 2, etc... Instead of sheet 1, I would like it to copy to "HC Data", "Termination Data", etc...
Not sure if I could use the loop or if I had to call each separately in the code.
Sub ImportSheets()
Dim Path As String
Dim filename As String
Dim sht As Worksheet
Dim wkB As Workbook
Dim i As Integer
Path = "C:\LOGCALL"
filename = Dir(Path & "\*.xls")
Application.ScreenUpdating = False
i = 1
Do While filename <> ""
'CHECK SHEET NAME TO BE CREATED DOES NOT EXIST
Do While SheetExists("Sheet" & i)
i = i + 1
If i = 20 Then
MsgBox "Too many sheets !", vbExclamation, "ERROR !!"
Application.ScreenUpdating = True
Exit Sub
End If
Loop
Set sht = ActiveSheet
Workbooks.Open filename:=Path & "\" & filename
If SheetExists("Sheet1") Then
Set wkB = ActiveWorkbook
Sheets("Sheet1").Copy After:=sht
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
Application.ScreenUpdating = True
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
Any help is greatly appreciated!!
I found this code at this site:
This works great but it copies each sheet from workbooks under a folder into the the current workbook to Sheet 1, Sheet 2, etc... Instead of sheet 1, I would like it to copy to "HC Data", "Termination Data", etc...
Not sure if I could use the loop or if I had to call each separately in the code.
Sub ImportSheets()
Dim Path As String
Dim filename As String
Dim sht As Worksheet
Dim wkB As Workbook
Dim i As Integer
Path = "C:\LOGCALL"
filename = Dir(Path & "\*.xls")
Application.ScreenUpdating = False
i = 1
Do While filename <> ""
'CHECK SHEET NAME TO BE CREATED DOES NOT EXIST
Do While SheetExists("Sheet" & i)
i = i + 1
If i = 20 Then
MsgBox "Too many sheets !", vbExclamation, "ERROR !!"
Application.ScreenUpdating = True
Exit Sub
End If
Loop
Set sht = ActiveSheet
Workbooks.Open filename:=Path & "\" & filename
If SheetExists("Sheet1") Then
Set wkB = ActiveWorkbook
Sheets("Sheet1").Copy After:=sht
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
Application.ScreenUpdating = True
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
Any help is greatly appreciated!!