How to copy the contents of the active sheet to a new workbook?

后端 未结 4 1951
星月不相逢
星月不相逢 2020-12-06 17:37

I\'m trying to copy the contents of the active sheet to a new workbook.

Sub new_workbook()

    Dim ExtBk As Workbook
    Dim ExtFile As String

    Columns(         


        
4条回答
  •  广开言路
    2020-12-06 18:26

    Don't use Copy method at all if you're only concerned with saving the Values.

    Sub new_workbook()
    Dim wbMe As Workbook: Set wbMe = ThisWorkbook
    Dim ws As Worksheet: Set ws = wbMe.ActiveSheet
    Dim ExtBk As Workbook
    
    Set ExtBk = Workbooks.Add
    ExtBk.SaveAs Filename:=wbMe.Path & "\output.xls"
    
    ExtBk.Worksheets("Sheet1").Range("A:N").Value = ws.Range("A:N").Value
    
    Application.DisplayAlerts = False
    ExtBk.Save
    Application.DisplayAlerts = True
    
    End Sub
    

    Note: this will fail (and so will your code, previously) if your ThisWorkbook is unsaved.

提交回复
热议问题