Excel VBA在指定位置保存文件

以前我问过关于如何使用XLDialogaveAs将Excel文件保存到指定位置的问题(对尚未保存的文件起作用) – Excel VBA XLDialogSaveAs函数不起作用 。 但是,我正在尝试对已经保存在计算机中的Excel文件执行相同的操作,但改为改变位置。

我有下面的代码:

Option Explicit Sub externalRatingChangeFile() 'Declare the data type of the variables Dim wks As Worksheet Dim sFilename As String 'Set wks to the current active worksheet Set wks = ActiveWorkbook.ActiveSheet 'Set the location to save the file to a variable sFilename = "H:\testing file" 'Save as .xlsx file in the specific location stated earlier 'If there are errors in the code, set wks to nothing and end the process On Error GoTo err_handler ChDrive sFilename ChDir sFilename Application.Dialogs(xlDialogSaveAs).Show (sFilename & "\TestingFile - " & Format(Date, "YYYYMMDD") & ".xlsx") 'System to/not display alerts to notify Users that they are replacing an existing file. Application.DisplayAlerts = True err_handler: 'Set Wks to its default value Set wks = Nothing End Sub 

有谁知道哪个excel VBA函数可以用来改变Excel文件的保存位置,并在保存之前在对话框中显示指定的位置? 谢谢!

我设法解决这个问题与下面的代码。

 Set fdlg = Application.FileDialog(msoFileDialogSaveAs) With fdlg .InitialFileName = sFilename .Show 'If there are errors in the code, set wks to nothing and end the process On Error GoTo err_handler wks.SaveAs (fdlg.SelectedItems(1)) End With 

谢谢!