Exporting tab to new book error message

Billdub417

New Member
Joined
Nov 5, 2019
Messages
45
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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
Solution
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,
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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