我正在寻找一个宏,它在运行时将获取单元格中的值并使用该名称保存它,并提示保存文件的路径。以下是我的尝试:
Sub SelectFolder()
Dim diaFolder As FileDialog
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.Show
ActiveSheet.Copy
ActiveWorkbook.SaveAs Filename:= _
diaFolder & ActiveSheet.[d2] & ".xlsx", FileFormat:= _
xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
Set diaFolder = Nothing
End Sub
此处的值取自单元格,并提示要求保存文件的文件夹。但该文件未存储在文件夹中并存储在单独的文件夹中。我想这里需要做一些小修正。
同时在保存时,它会向我发出警告,指出97-2003文件类型可能存在兼容性问题。我想在这一行中需要纠正一些事情:
diaFolder & ActiveSheet.[d2] & ".xlsx", FileFormat:= _
xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False
但不知道要改变什么。我希望该文件位于.xlsx中,并且与所有设备兼容。
有人可以帮我做这两个吗?
答案 0 :(得分:1)
这是一个有效的代码:
Sub SelectFolder()
Dim diaFolder As FileDialog
Dim fileName, filePath As String
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.Show
ActiveSheet.Copy
fileName = ActiveSheet.Range("D2").Value
'You need to use diaFolder.SelectedItems(1) and not just diaFolder
filePath = diaFolder.SelectedItems(1) & "\" & fileName & ".xlsx"
ActiveWorkbook.SaveAs fileName:= _
filePath, FileFormat:=xlWorkbookNormal, Password:="", WriteResPassword:="" _
, ReadOnlyRecommended:=False, CreateBackup:=False
Set diaFolder = Nothing
End Sub
注意:强>
我已经将FileFormat从Excel8更改为xlWorkbookNormal
您的代码中的问题:
1. FileFormat Excel 8表示 97-2003 format in Excel 2007-2013, xls
,您将其保存为 .xlsx
2.您仅使用 diaFolder
,而不是 diaFolder.SelectedItems(1)