I have an excel workbook broken out with multiple worksheets, 1 per customer. In my code, I am trying to save each individual customer worksheet as its own excel file. However, the .SaveAs command breaks the second time it triggers in the loop. Any pointers would be fantastic.
Dim SchedWorksheet As Worksheet
Dim SchedWorkbook As Workbook
Dim SchedName As String
Set SchedWorkbook = ActiveWorkbook
Set SchedWorksheet = ActiveSheet
Application.DisplayAlerts = False
For Each Worksheet In SchedWorkbook.Sheets
If Worksheet.Name = "Instructions" Or Worksheet.Name = "Invoice_Items"
Or Worksheet.Name = "Customers" Or _
Worksheet.Name = "Terms" Or Worksheet.Name = "Dilution_Type" Or
Worksheet.Name = "Approval_Status" Or _
Worksheet.Name = "Carriers" Then
GoTo NextSched
End If
If Worksheet.Name = "Invoices" Then
'basicScheduleFileName is global set at beginning of program
SchedName = basicScheduleFileName & "ALL"
Else
SchedName = Worksheet.Name
End If
'payoutFileName is global set at beginning of program
Worksheet.SaveAs Application.ActiveWorkbook.Path & "\" & payoutFileName
& "\Basic Schedule" & "\" & SchedName, xlOpenXMLWorkbook
NextSched:
Next Worksheet
The error on the second iteration is as follows: Run-time error 1004 'Application-defined or object-defined error'
I have also attempted to run this loop using the SchedWorksheet object in lieu of Worksheet and get the error "method .SaveAs of object _Worksheet failed" on the second iteration.
Question I have code extremely similar to his code earlier in my program that takes a similar dataset and uses an exportAsFixedFormat call to save each worksheet as a PDF. Is there an equivalent for .xlsx? (.csv would be fine as well)
payoutFileName. Also, what'sSchedNamethe first time it's run, and the second? - BruceWaynebasicScheduleFileName? - Tim Williams