如何使用vba将文件保存到原始文件所在的位置?

时间:2016-04-06 17:52:26

标签: excel vba excel-vba macros

我正在编写一个简单的宏来执行以下操作: 1.删​​除所有格式 2.取消隐藏所有隐藏的列 3.保留四个指定的选项卡:“Forms”,“Fields”,“DataDictionaries”和“DataDictionaryEntries” 4.将文件(带有这四个选项卡)保存在原始文件所在的位置下。

这是我的代码:

Sub removeformat()

Dim sheetlist As Variant
Dim sheet As Variant
sheetlist = Array("Forms", "Fields", "DataDictionaries", "DataDictionaryEntries")

For Each sheet In sheetlist
    Sheets(sheet).Activate
    Sheets(sheet).Cells.Select
    Selection.ClearFormats
    Sheets(sheet).Cells(1, 1).Select
    Sheets(sheet).Columns("A:IV").Hidden = False

Next sheet

Sheets(Array("Forms", "Fields", "DataDictionaries", "DataDictionaryEntries")).Copy
ActiveWorkbook.SaveAs Filename:="File to Upload", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close

End Sub

问题是第四步。它不会保存到原始文件位置,而是保存到excel默认位置。你能帮我解决这个问题吗?非常感谢!

1 个答案:

答案 0 :(得分:1)

如果要保存在与ActiveWorkbook相同的位置,则由ActiveWorkBook.Path提供

ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & "\" & "File to Upload", _
                      FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False