【发布时间】:2016-04-06 02:39:49
【问题描述】:
我正在尝试以 .xlsx 文件格式自动将 .xls 文件保存在硬编码位置。我希望 SaveAs 对话框显示硬编码的位置,以及在“文件名:”字段中编码的文件名。因此,我需要做的就是单击“保存”按钮。
但是,当我想将文件保存在 H 盘中时,“另存为”对话框总是显示 C 盘。
以下是我的代码:
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
【问题讨论】:
-
您是否要保存工作簿或工作表?
Dialogs(xlDialogSaveAs)只会在先前未保存工作簿时在初始文件夹中启动。 -
我正在尝试保存工作簿。该工作簿是从在线资源中导出的,之前没有保存在任何文件夹中。这就是 SaveAs 对话框一直向我显示 C 驱动器的原因吗?