我已经获得了部分代码:
Workbooks.Add
ActiveWorkbook.SaveAs filename:=Application.GetSaveAsFilename(fileFilter:="Excel Workbook (*.xlsx), *.xlsx")
因此,用户选择工作簿应具有的位置和名称。但是,如果他点击"取消"或者尝试关闭窗口,工作簿仍然以名称" False"保存。怎么阻止?如果用户点击"取消"最好的解决方案是退出sub。或关闭窗口
答案 0 :(得分:0)
Dim s as Variant
s = Application.GetSaveAsFilename(fileFilter:="Excel Workbook (*.xlsx), *.xlsx")
if s = False then
else
ActiveWorkbook.SaveAs filename:=s
end if
答案 1 :(得分:0)
当用户点击false
时,该方法会返回Cancel
,因此您必须检查此案例。这样的事情对你有用:
fileSaveName = Application.GetSaveAsFilename(fileFilter:="Text Files (*.txt), *.txt")
If fileSaveName <> False Then
ActiveWorkbook.SaveAs filename := fileSaveName
End If
答案 2 :(得分:0)
我建议不要使用ActiveWorkbook
。而是直接引用新添加的工作簿。
Option Explicit
Public Sub SaveEample()
Dim NewWb As Workbook
Set NewWb = Workbooks.Add 'set the new added workbook to a variable so we can access it later
Dim FileLocation As Variant
FileLocation = Application.GetSaveAsFilename(fileFilter:="Excel Workbook (*.xlsx), *.xlsx")
If Not FileLocation = False Then
NewWb.SaveAs FileName:=FileLocation
End If
End Sub
答案 3 :(得分:0)
使用@Pᴇʜ代码我修改了我的工作,就像我想要的那样:
Set NewWb = Workbooks.Add
genpath = Application.GetSaveAsFilename(fileFilter:="Excel Workbook (*.xlsx), *.xlsx")
If Not genpath = False Then
NewWb.SaveAs filename:=genpath
Else
NewWb.Close
Exit Sub
End If