vba 如何更改excel的默认“另存为”目录路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26645665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:03:56  来源:igfitidea点击:

How to change default "save as" directory path for excel?

excelvbaexcel-vba

提问by kappu

Hi I just embed this code into my vba macro, but how to change default directory when I use this macro.. for example when I click it is going to D:/myfolder

嗨,我只是将此代码嵌入到我的 vba 宏中,但是当我使用此宏时如何更改默认目录.. 例如,当我单击它时将转到 D:/myfolder

I found this code at google :

我在谷歌找到了这段代码:

Sub Copy_ActiveSheet_2()
'Working in Excel 2000-2013
Dim fname As Variant
Dim NewWb As Workbook
Dim FileFormatValue As Long

'Check the Excel version
If Val(Application.Version) < 9 Then Exit Sub
If Val(Application.Version) < 12 Then

    'Only choice in the "Save as type" dropdown is Excel files(xls)
    'because the Excel version is 2000-2003
    fname = Application.GetSaveAsFilename(InitialFileName:="", _
    filefilter:="Excel Files (*.xls), *.xls", _
    Title:="This example copies the ActiveSheet to a new workbook")

    If fname <> False Then
        'Copy the ActiveSheet to new workbook
        ActiveSheet.Copy
        Set NewWb = ActiveWorkbook

        'We use the 2000-2003 format xlWorkbookNormal here to save as xls
        NewWb.SaveAs fname, FileFormat:=-4143, CreateBackup:=False
        NewWb.Close False
        Set NewWb = Nothing

    End If
Else
    'Give the user the choice to save in 2000-2003 format or in one of the
    'new formats. Use the "Save as type" dropdown to make a choice,Default =
    'Excel Macro Enabled Workbook. You can add or remove formats to/from the list

    fname = Application.GetSaveAsFilename(InitialFileName:="", filefilter:= _
        " Excel Macro Free Workbook (*.xlsx), *.xlsx," & _
        " Excel Macro Enabled Workbook (*.xlsm), *.xlsm," & _
        " Excel 2000-2003 Workbook (*.xls), *.xls," & _
        " Excel Binary Workbook (*.xlsb), *.xlsb", _
        FilterIndex:=2, Title:="This example copies the ActiveSheet to a new workbook")

    'Find the correct FileFormat that match the choice in the "Save as type" list
    If fname <> False Then
        Select Case LCase(Right(fname, Len(fname) - InStrRev(fname, ".", , 1)))
        Case "xls": FileFormatValue = 56
        Case "xlsx": FileFormatValue = 51
        Case "xlsm": FileFormatValue = 52
        Case "xlsb": FileFormatValue = 50
        Case Else: FileFormatValue = 0
        End Select

        'Now we can create/Save the file with the xlFileFormat parameter
        'value that match the file extension
        If FileFormatValue = 0 Then
            MsgBox "Sorry, unknown file extension"
        Else
            'Copies the ActiveSheet to new workbook
            ActiveSheet.Copy
            Set NewWb = ActiveWorkbook

            'Save the file in the format you choose in the "Save as type" dropdown
            NewWb.SaveAs fname, FileFormat:= _
                         FileFormatValue, CreateBackup:=False
            NewWb.Close False
            Set NewWb = Nothing

        End If
    End If
End If
End Sub

回答by Cheesenbranston

Change this part of the code

更改这部分代码

fname = Application.GetSaveAsFilename(InitialFileName:=""

to include the default save path you would like

包含您想要的默认保存路径

fname = Application.GetSaveAsFilename(InitialFileName:=""C:\My Documents\"

Make sure you leave the trailing backslash, otherwise a default file will be suggested with a filename equal to the the path you have provided eg.

确保留下尾随反斜杠,否则将建议默认文件,其文件名等于您提供的路径,例如。

fname = Application.GetSaveAsFilename(InitialFileName:=""C:\My Documents"

Will result in a dialog where the default file named "My Documents" is saved in the location "C:\"

将导致一个对话框,其中名为“我的文档”的默认文件保存在“C:\”位置