Loop Thru Worksheet to move and save it as New Workbook

lancerj017

Active Member
Joined
Jan 25, 2012
Messages
318
Guys I need help here :D can anyone check my code if i did something wrong? i can't figure it out

how to loop thru all worksheet and save it to a new workbook with the same path of original workbook.

here's my code:

Code:
Sub WorkbookSaveAs()
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim SourceWb As Workbook
    Dim DestWb As Workbook
    Dim FilePath As String
    Dim FileName As String
    Dim FolderPath As String
    Dim Current As Worksheet
    Dim WBName As String
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    FolderPath = Left(ActiveWorkbook.FullName, InStrRev(ActiveWorkbook.FullName, "\")) & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) & " " & Format(Now, "yyyymmdd hh-mm-ss")
    MkDir FolderPath
    FilePath = FolderPath & "\"
    Set SourceWb = ActiveWorkbook
    Set DestWb = ActiveWorkbook
        FileName = Left(SourceWb.Name, Len(SourceWb.Name) - 4)
        Fileeextstr = ".xlsx"
        FileFormatNum = 51
    With DestWb
        .SaveAs FilePath & FileName & FileExtStr, FileFormat:=FileFormatNum
        For Each Current In .Worksheets
            WBName = Left(.Name, Len(.Name) - 5)
            If Current.Name <> WBName Or Left(Current.Name, Len(WBName)) <> WBName Then
                If .Sheets(Current.Name).Cells(Rows.Count, 1).End(xlUp).Row = 1 Then
                    Application.DisplayAlerts = False
                    Current.Delete
                    Application.DisplayAlerts = True
                Else
                    Current.Move
                    ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & Current.Name & ".xlsx"
                    ActiveWorkbook.Close savechanges:=False
                End If
            End If
        Next
    End With
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
no error but when my macro do the loop it won't work on creating workbook and save it to the same path of original workbook. can anyone may suggest a dynamic code that will move the worksheet and save it to the same location of original workbook?
 
Upvote 0

Forum statistics

Threads
1,202,908
Messages
6,052,488
Members
444,587
Latest member
ezza59

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