问题描述
我创建了一个Excel模板,该模板从另一个Excel文件中获取数据并将其导入到此模板中。数据放入模板后,我需要有一个显示“另存为”窗口的代码,以便用户可以在保持原始模板干净的同时将文件(现在具有数据)保存在他们的计算机中。
对于Microsoft用户而言,代码是这样的,并且可以很好地工作(另存为文档,在保存新文件后清理模板中的数据,并在保持新保存文件打开的同时关闭模板文件):
Workbooks.Open Filename:=templatepath
Set Template2 = Workbooks.Open(templatepath)
Set Econometrics2 = Template2.Worksheets("Econometrics")
Database.Close SaveChanges:=False
Template2.Activate
Econometrics2.Activate
Econometrics2.Range("B6:P50000").ClearContents
Template2.Close SaveChanges:=True
Unload Userform2
MsgBox "Import successful.",vb@R_110_4045@ion
问题是Mac用户遇到问题-提示用户“另存为”对话框,但文件未保存,Excel仅关闭。我创建了以下代码,以允许MOS和Mac用户使用:
If Not Application.OperatingSystem Like "*Mac*" Then
filedialog2 = Application.GetSaveAsFilename(InitialFileName:="C:\",Title:="Save new Econometrics file.",Filefilter:="Excel Macro-Enabled Workbook (*.xlsm),*xlsm")
If filedialog2 <> "False" Then
Template.SaveAs filedialog2
Else
MsgBox "No file was saved,please Save As the file yourself.",vbCritical,"Error"
Database.Close SaveChanges:=False
Unload Userform2
Exit Sub
Workbooks.Open Filename:=filedialog2
Set NewFile = Workbooks.Open(filedialog2)
End If
Else
If Val(Application.Version) > 14 Then
Call MacGetSaveAsFilenameExcel("","xlsm")
End If
End If
On Error GoTo ErrorHandler
Application.ScreenUpdating = True
Workbooks.Open Filename:=templatepath
Set Template2 = Workbooks.Open(templatepath)
Set Econometrics2 = Template2.Worksheets("Econometrics")
Database.Close SaveChanges:=False
Template2.Activate
Econometrics2.Activate
Econometrics2.Range("B6:P50000").ClearContents
Template2.Close SaveChanges:=True
Unload Userform2
MsgBox "Import successful.",vb@R_110_4045@ion
函数为:
Function MacGetSaveAsFilenameExcel(MyInitialFilename As String,FileExtension As String)
Dim FName As Variant
Dim FileFormatValue As Long
Dim TestIfOpen As Workbook
Dim FileExtGetSaveAsFilename As String
Again: FName = False
FName = Application.GetSaveAsFilename(InitialFileName:=MyInitialFilename)
If FName <> False Then
FileExtGetSaveAsFilename = LCase(Right(FName,Len(FName) - InStrRev(FName,".",1)))
If FileExtension <> "" Then
If FileExtension <> FileExtGetSaveAsFilename Then
MsgBox "Sorry you must save the file in this format : " & FileExtension
GoTo Again
End If
If ActiveWorkbook.HasVBProject = True And LCase(FileExtension) = "xlsx" Then
MsgBox "Your workbook have VBA code,please save in xlsm format"
Exit Function
End If
Else
If ActiveWorkbook.HasVBProject = True And LCase(FileExtGetSaveAsFilename) = "xlsx" Then
MsgBox "Your workbook have VBA code,please save in xlsm format"
GoTo Again
End If
End If
Select Case FileExtGetSaveAsFilename
Case "xlsm": FileFormatValue = 53
Case Else: FileFormatValue = 0
End Select
If FileFormatValue = 0 Then
MsgBox "Sorry,FileFormat not allowed"
GoTo Again
Else
'Error check if there is a file open with that name
Set TestIfOpen = nothing
On Error Resume Next
Set TestIfOpen = Workbooks(LCase(Right(FName,_
Application.PathSeparator,1))))
On Error GoTo 0
If Not TestIfOpen Is nothing Then
MsgBox "You are not allowed to overwrite a file that is open with the same name," & _
"use a different name or close the file with the same name first."
GoTo Again
End If
End If
ActiveWorkbook.SaveAs FName,FileFormat:=FileFormatValue
End If
End Function
有什么想法为什么不能在Mac中保存文件,以及我是否可以执行相同的步骤(在关闭具有清除内容的模板文件的同时保持打开的保存文件)?
谢谢!!任何帮助表示赞赏。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)