简体   繁体   中英

Save active sheet to new workbook

I want a button to save the active sheet to a new workbook prompting the user for filename and path. It needs to save as a .xlsm

I have this but have this issue.

  1. If filename already exist it prompts to debug and will open the vba code.

     Private Sub SaveBarList_Click() ActiveSheet.Copy With ActiveSheet.UsedRange .Copy .PasteSpecial xlValues .PasteSpecial xlFormats End With Application.CutCopyMode = False Dim DTAddress As String DTAddress = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator ActiveWorkbook.SaveAs Filename:= Application.GetSaveAsFilename ActiveWorkbook.Close End Sub 

Here's my idea:

Sub SaveAsMacro()

    Dim strDir As String

    'Show standard save as dialogue box
    With Application.FileDialog(msoFileDialogSaveAs)
        If .Show = -1 Then
            strDir = .SelectedItems(1)
        End If
    End With

    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:=strDir
    Application.DisplayAlerts = True

End Sub

Basically gets the file name first (and a warning pops up if overwriting), then bulldozers ahead and saves no matter what (after switching warnings off).

Alternatively if you don't care for the warning and just wanted to suppress it, you can more simply use your original code with warnings switched off:

Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:= Application.GetSaveAsFilename
Application.DisplayAlerts = True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM