I am trying to copy worksheets within a workbook to new workbooks based on the value of the sheet name. So there could be any number of worksheets in the file, say 5 sheets start with the name Allegiance, 2 start with Express, and 3 start with McCollum. I want to copy, either a sheet at a time or loop through the sheets and based on the left 5 characters of the sheet name, copy the sheet into a workbook opened and saved with say 08.27.22 Allegiance or 08.27.22 Express. So I can't hard code the new workbook name because next week it will be 09.03.22 Allegiance. I keep getting runtime errors no matter what way I try. Can someone please get me going in the right direction.
Here is where I am currently at. I commented out a lot of it trying to get one portion to work.
Here is where I am currently at. I commented out a lot of it trying to get one portion to work.
VBA Code:
Sub CreateFiles_TempLabor()
Dim ws As Worksheet
Dim origwb As Workbook
Dim strMonth As String
Dim strDay As String
Dim strYear As String
Fpath = ActiveWorkbook.Path
Set origwb = ActiveWorkbook
Workbooks.Add
Fpath = "J:\Temp Employment Agencies\Allegiance"
dPreviousSaturday = Date - Weekday(Date)
strMonth = Format(dPreviousSaturday, "mm")
strDay = Format(dPreviousSaturday, "dd")
strYear = Format(dPreviousSaturday, "yy")
Fname = strMonth & "." & strDay & "." & strYear & " Allegiance"
strFileName = Fpath & "\" & Fname & ".xlsx"
strFileExists = Dir(strFileName)
If strFileExists = "" Then
ActiveWorkbook.SaveAs Filename:=Fpath & "\" & Fname & ".xlsx"
Else
ActiveWorkbook.SaveAs Filename:=Fpath & "\" & Fname & "_2.xlsx"
End If
wbAlleg = ActiveWorkbook.Name
' Workbooks.Add
' Fpath = "J:\Temp Employment Agencies\Express"
' dPreviousSaturday = Date - Weekday(Date)
' strMonth = Format(dPreviousSaturday, "mm")
' strDay = Format(dPreviousSaturday, "dd")
' strYear = Format(dPreviousSaturday, "yy")
' Fname = strMonth & "." & strDay & "." & strYear & " Express"
' strFileName = Fpath & "\" & Fname & ".xlsx"
' strFileExists = Dir(strFileName)
'
' If strFileExists = "" Then
' ActiveWorkbook.SaveAs Filename:=Fpath & "\" & Fname & ".xlsx"
' Else
' ActiveWorkbook.SaveAs Filename:=Fpath & "\" & Fname & "_2.xlsx"
' End If
'
' wbExpre = ActiveWorkbook.Name
'
' Workbooks.Add
' Fpath = "J:\Temp Employment Agencies\McCollum"
' dPreviousSaturday = Date - Weekday(Date)
' strMonth = Format(dPreviousSaturday, "mm")
' strDay = Format(dPreviousSaturday, "dd")
' strYear = Format(dPreviousSaturday, "yy")
' Fname = strMonth & "." & strDay & "." & strYear & " McCollum"
' strFileName = Fpath & "\" & Fname & ".xlsx"
' strFileExists = Dir(strFileName)
'
' If strFileExists = "" Then
' ActiveWorkbook.SaveAs Filename:=Fpath & "\" & Fname & ".xlsx"
' Else
' ActiveWorkbook.SaveAs Filename:=Fpath & "\" & Fname & "_2.xlsx"
' End If
'
' wbMcCol = ActiveWorkbook.Name
'
' Workbooks.Add
' Fpath = "J:\Temp Employment Agencies\Staffmark"
' dPreviousSaturday = Date - Weekday(Date)
' strMonth = Format(dPreviousSaturday, "mm")
' strDay = Format(dPreviousSaturday, "dd")
' strYear = Format(dPreviousSaturday, "yy")
' Fname = strMonth & "." & strDay & "." & strYear & " Staffmark"
' strFileName = Fpath & "\" & Fname & ".xlsx"
' strFileExists = Dir(strFileName)
'
' If strFileExists = "" Then
' ActiveWorkbook.SaveAs Filename:=Fpath & "\" & Fname & ".xlsx"
' Else
' ActiveWorkbook.SaveAs Filename:=Fpath & "\" & Fname & "_2.xlsx"
' End If
'
' wbStaff = ActiveWorkbook.Name
origwb.Activate
For Each ws In ThisWorkbook.Worksheets
agency = Left(ws.Name, 5)
Select Case agency
Case "Alleg"
ws.Select
ws.Copy After:=Workbooks(""", wbAlleg, """).Sheets(Sheet.count)
' Case "Expre"
' ws.Select
' ws.Copy After:=workbookExpre.Sheets(Sheet.count)
' Case "McCol"
' ws.Select
' ws.Copy After:=workbookMcCol.Sheets(Sheet.count)
' Case "Staff"
' ws.Select
' ws.Copy After:=workbookStaff.Sheets(Sheet.count)
Case Else
End Select
Next ws
End Sub