5,139
社区成员
发帖
与我相关
我的任务
分享
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim Name As String
With ThisWorkbook
Name = Worksheets("Sheet1").Range("B7").Value
Dim dlgSaveAs As FileDialog
'Set the dialog type
Set dlgSaveAs = Application.FileDialog(msoFileDialogSaveAs)
With dlgSaveAs
.InitialFileName = Name
.FilterIndex = 2
'Display the dialog
.Show
End With
End With
End Sub
Dim Name As String
With ThisWorkbook
Name = Worksheets("Sheet1").Range("B7").Value
Dim dlgSaveAs As FileDialog
'Set the dialog type
Set dlgSaveAs = Application.FileDialog(msoFileDialogSaveAs)
With dlgSaveAs
.InitialFileName = Name
.FilterIndex = 2
'Display the dialog
.Show
End With
End With
是因为没有Execute而没有保存
添加Execute后 可以保存
但是有个问题 执行Execute时文件也进行了保存动作
还会触发Workbook_BeforeSave这个函数
所以文件出错想不出错的话
把代码移动到普通的函数中
Sub b()
Dim Name As String
With ThisWorkbook
Name = Worksheets("Sheet1").Range("B7").Value
Dim dlgSaveAs As FileDialog
'Set the dialog type
Set dlgSaveAs = Application.FileDialog(msoFileDialogSaveAs)
With dlgSaveAs
.InitialFileName = Name
.FilterIndex = 2
'Display the dialog
.Show
.Execute
End With
End With
End Sub