我的工作表中有一个查询。我需要将查询范围导出到新的.xlsx工作簿中,并将其导出到变量文件夹(SaveAs)。
如何保存为特定范围?
我尝试了Application.FileDialog(msoFileDialogSaveAs)
,它确实保存了整个工作簿,但我只想保存工作簿的特定范围。
Sub SaveAsDialog()
On Error Resume Next
With Application.FileDialog(msoFileDialogSaveAs)
If .Show = 0 Then
Exit Sub
End If
Application.DisplayAlerts = False
.Execute
Application.DisplayAlerts = True
End With
End Sub
答案 0 :(得分:1)
您正在寻找这样的东西:
Sub SaveAsDialog()
Dim od As Workbook, nod As Workbook
Set od = ThisWorkbook
'Copy data that you want to save
od.Worksheets("Sheet1").Range("A1:B10").Copy
' Add a new workbook
Set nod = Workbooks.Add
nod.Worksheets(1).Range("A1").PasteSpecial xlPasteValues
'Save the new workbook
nod.SaveAs od.Path & "\New_Book.xlsx"
nod.Close True
End Sub