我是VBA的新手但正在编写一些报告,创建一个宏按钮以自动将定义的范围导出到新的.xlsx文档会更简单。
我到目前为止:
Sub ExportTest()
Dim WS As Worksheet
Dim Rng As Range
Set Rng = Range("A1:K10")
Application.Workbooks.Add
Set WS = Application.ActiveSheet
Rng.Copy Destination:=WS.Range("A1")
WS.SaveAs "test", ".xlsx"
End Sub
我收到错误1004,这似乎与SaveAs部分有关。
但无法弄清楚如何将其自动保存到预定义的文件夹中。 如果可能,是否可以将文件名作为原始工作簿中的单元格引用?
答案 0 :(得分:0)
SaveAs语法中有错误。试试这个
WS.SaveAs "test", xlOpenXMLWorkbook
答案 1 :(得分:0)
我想我现在解决了,谢谢你!
Sub Test()
Dim WS As Worksheet
Dim Rng As Range
Set Rng = Range("A1:K10")
Application.Workbooks.Add
Set WS = Application.ActiveSheet
Rng.Copy Destination:=WS.Range("A1")
Dim Path As String
Dim filename As String
Path = "C:\test\"
filename = Range("A1")
WS.SaveAs filename:=Path & filename & ".xlsx", FileFormat:=xlOpenXMLWorkbook
End Sub