简体   繁体   中英

How to “SaveAs” a specific range with VBA Excel?

I have a query in my sheet. I need to export the query range into a new .xlsx workbook to a variable folder (SaveAs).

How can I SaveAs a specific range?

I tried the Application.FileDialog(msoFileDialogSaveAs) , which does save the entire workbook, but I just want to save a specific range of the workbook.

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

You are looking for something like this:

  • Copy and paste the data in a new Workbook and Save it

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM