Macro to export all sheets from BTE ME 80214 to last sheet and save as Sales Cheque advices in C:\My documents

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,563
Office Version
  1. 2021
Platform
  1. Windows
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

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
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try:
VBA Code:
Sub ExportSheetsAsWorkbook()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, fileName As String, filePath As String, wb As Workbook, i As Integer, srcWB As Workbook
    Set srcWB = ThisWorkbook
    filePath = "C:\My Documents\"
    fileName = "Sales Cheque advices.xlsx"
    Workbooks.Add
    For Each ws In srcWB.Sheets
        If ws.Index >= srcWB.Worksheets("BTE ME 80214").Index Then
            ws.Copy After:=Sheets(Sheets.Count)
        End If
    Next ws
    With ActiveWorkbook
        .SaveAs fileName:=filePath & fileName
        .Close False
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Many Thanls for the help mumps

Your code works perfectly
 
Upvote 0

Forum statistics

Threads
1,215,049
Messages
6,122,864
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top