Macro Help - Parse Worksheet Name

jacodri

New Member
Joined
Nov 23, 2015
Messages
1
I'm Trying to create a macro that will save each worksheet as the tab name instead of saving the file as "Sheet#". What am I missing in the code below?

Code:
Sub SaveShtsAsBook()
    Dim Sheet As Worksheet, Sheet$, MyFilePath$, N&
    MyFilePath$ = ActiveWorkbook.Path & "U:\Clients\Current\Fresenius\4996_FMCNA_2016_Clincial Managers Conference\Lists_Registration\Rooming List\Master Rooming List" & _
    Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 9)
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
         '      End With
        On Error Resume Next '<< a folder exists
        MkDir MyFilePath '<< create a folder
        For N = 1 To Sheets.Count
            Sheets(N).Activate
            SheetName = ActiveSheet.Name
            Cells.Copy
            Workbooks.Add (xlWBATWorksheet)
            With ActiveWorkbook
            With .ActiveSheet
                    .Paste
                    .Name = SheetName
                    [A1].Select
                End With
                 'save book in this folder
                .SaveAs Filename:=MyFilePath _
                & "U:\Clients\Current\Fresenius\4996_FMCNA_2016_Clincial Managers Conference\Lists_Registration\Rooming List\Master Rooming List" & SheetName & ".xls"
                .Close SaveChanges:=True
            End With
            .CutCopyMode = False
        Next
    End With
    Sheet1.Activate
End Sub
 
Last edited by a moderator:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi and welcome to the MrExcel Message Board.

This line does not look right:
Code:
.SaveAs Filename:=MyFilePath _
                & "U:\Clients\Current\Fresenius\4996_FMCNA_2016_Clincial Managers Conference\Lists_Registration\Rooming List\Master Rooming List" & SheetName & ".xls"
MyFilePath is already set to a long string at the start of the code:
Code:
ActiveWorkbook.Path & "U:\Clients\Current\Fresenius\4996_FMCNA_2016_Clincial Managers Conference\Lists_Registration\Rooming List\Master Rooming List" & _
    Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 9)

In fact, that already has a path joined to a path.

It will help if you replace:
Code:
        On Error Resume Next '<< a folder exists
        MkDir MyFilePath '<< create a folder
with this:
Code:
        On Error Resume Next '<< a folder exists
        MkDir MyFilePath '<< create a folder
        On Error GoTo 0 '<< cancel error override
It won't make your program run but it will show you where the errors are. :)

Regards,
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,175
Members
449,071
Latest member
cdnMech

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