按单元格值保存Excel文件并保存在打开文件的当前目录中

时间:2012-10-16 08:15:58

标签: excel excel-vba vba

我正在尝试将此代码保存到打开文件的目录中。目前它设法通过cel文本保存文件,但默认情况下要保存在c:/ my documents中。

这是我到目前为止所不知道在哪里添加ThisWorkbook.Path

Sub Save()

 Dim flToSave As Variant
 Dim flName As String
 Dim flFormat As Long

 flFormat = ActiveWorkbook.FileFormat


 flName = Range("A1") & Range("A2").Text
 flToSave = Application.GetSaveAsFilename _
 (flName, filefilter:="Excel Files (*.xlsm), *.xlsm", _
 Title:="Save FileAs...")


 If flToSave = False Then
 Exit Sub
 Else

 Thisworkbook.SaveAs Filename:=flToSave, FileFormat:=flFormat
 End If

 End Sub

1 个答案:

答案 0 :(得分:1)

GetSaveAsFilename更改为

flToSave = Application.GetSaveAsFilename _
(ThisWorkbook.Path & "\" & flName, filefilter:="Excel Files (*.xlsm), *.xlsm", _
Title:="Save FileAs...")

这将启动指定目录中的SaveAs