I have the following code below export all sheets from BTE ME 80214 to last sheet and save as Sales Cheque advices in C:\My documents
Hover not all the sheets are being exported
It would be appreciated if someone could kinfdly check and amend my code
Hover not all the sheets are being exported
It would be appreciated if someone could kinfdly check and amend my code
Code:
Sub ExportSheetsAsWorkbook()
Dim ws As Worksheet
Dim lastRow As Long
Dim fileName As String
Dim filePath As String
Dim wb As Workbook
Dim i As Integer
' Set the file path and name where you want to save the new workbook
filePath = "C:\My Documents\"
fileName = "Sales Cheque advices.xlsx"
' Create a new workbook
Set wb = Workbooks.Add
' Loop through all sheets from BTE ME 80214 to the last sheet
For Each ws In ThisWorkbook.Worksheets
If ws.Index >= ThisWorkbook.Worksheets("BTE ME 80214").Index Then
' Copy the sheet to the new workbook
ws.Copy After:=wb.Sheets(wb.Sheets.Count)
' Rename the sheet in the new workbook
wb.Sheets(wb.Sheets.Count).name = ws.name
End If
Next ws
' Save the new workbook with the specified file path and name
wb.SaveAs fileName:=filePath & fileName
' Close the new workbook
wb.Close
End Sub