Exporting tab to new book error message

Billdub417

New Member
Joined
Nov 5, 2019
Messages
37
Hello,

Please see code below - I currently use this to export a hidden tab from a master document.

However, there are 2 main issues with it, as follows:

1) Is it possible to set a name that this exported book saves as, i.e. "Book1", and if so, can this be set so that next time it will be "Book2" etc? (it currently comes up blank)
2) If this data is exported and someone clicks "cancel" rather than "save" when choosing the location for this new document, the debug error message comes up, highlighting the "Sheets("Data").Visible = False" line (and not closing the exported tab) - is it possible to get it to close the exported document and still hide the "Data" tab on the master document?

Thanks in advance!

VBA Code:
Sub new book()

        Sheets("Data").Visible = True

        Dim IntialName As String
        Dim sFileSaveName As Variant
        IntialName = "Sample Output"
        sFileSaveName = Application.GetSaveAsFilename(InitialFileName:=InitialName, fileFilter:="Excel Files (*.xlsx), *.xlsx")

        If sFileSaveName <> False Then
         ActiveWorkbook.SaveAs sFileSaveName
     
        End If

        ActiveWorkbook.Close
        Sheets("Data").Visible = False

End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,903
I don't understand why you're using GetSaveAsFilename if you want the file to be saved with a fixed name, i.e. Book1.xlsx, then Book2.xlsx, etc.

Try this macro:
VBA Code:
Public Sub Save_Data_Sheet()

    Dim folder As String
    Dim xlsxFileName As String
    
    xlsxFileName = "Book<n>.xlsx"
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select destination folder"
        .InitialFileName = ThisWorkbook.Path & "\"
        If Not .Show Then Exit Sub
        folder = .SelectedItems(1) & "\"
    End With
    
    xlsxFileName = GetNextFileName(folder & xlsxFileName)
    
    With Sheets("Data")
        .Visible = True
        .Copy
        ActiveWorkbook.SaveCopyAs xlsxFileName
        ActiveWorkbook.Close SaveChanges:=False
        .Visible = False
    End With
            
    MsgBox "Data sheet saved as " & xlsxFileName
            
End Sub


Private Function GetNextFileName(filePathTemplate As String) As String

    Dim n As Integer
    
    n = 1
    GetNextFileName = Replace(filePathTemplate, "<n>", n)
    While Dir(GetNextFileName) <> vbNullString
        n = n + 1
        GetNextFileName = Replace(filePathTemplate, "<n>", n)
    Wend
    
End Function
 

Billdub417

New Member
Joined
Nov 5, 2019
Messages
37
Hello John w

Thanks, that's brilliant.

Just one thing - I should have included the bit of code prior to what I posted - could your code be adjusted to copy and paste values on the sheet being saved? and could it allow for the password to be added?

Thanks again,

VBA Code:
    ThisWorkbook.Sheets("Data").Copy
    Cells.Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Range("A1").Select
    ' PW protect sheet
        ActiveSheet.Protect Password:="abc123", DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,903
VBA Code:
Public Sub Save_Data_Sheet()

    Dim folder As String
    Dim xlsxFileName As String
    
    xlsxFileName = "Book<n>.xlsx"
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select destination folder"
        .InitialFileName = ThisWorkbook.Path & "\"
        If Not .Show Then Exit Sub
        folder = .SelectedItems(1) & "\"
    End With
    
    xlsxFileName = GetNextFileName(folder & xlsxFileName)
    
    With ThisWorkbook.Worksheets("Data")
        .Visible = True
        .Copy
        With ActiveWorkbook
            .Worksheets("Data").UsedRange.Value = .Worksheets("Data").UsedRange.Value
            .Worksheets("Data").Protect Password:="abc123", DrawingObjects:=True, Contents:=True, Scenarios:=True, _
                                        AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
            .SaveCopyAs xlsxFileName
            .Close SaveChanges:=False
        End With
        .Visible = False
    End With
            
    MsgBox "Data sheet saved as " & xlsxFileName
            
End Sub

       
Private Function GetNextFileName(filePathTemplate As String) As String

    Dim n As Integer
    
    n = 0
    Do
        n = n + 1
        GetNextFileName = Replace(filePathTemplate, "<n>", n)
    Loop Until Dir(GetNextFileName) = vbNullString
    
End Function
 
Solution

Billdub417

New Member
Joined
Nov 5, 2019
Messages
37
Excellent, that's perfect!
using this code, do you need to make the sheet visible to export it? or could you just exclude ".Visible = True / False"?

Thanks,
 

Forum statistics

Threads
1,136,909
Messages
5,678,516
Members
419,768
Latest member
eguechi09x

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
Top