vba 如何将活动工作表的内容复制到新工作簿?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17281872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:57:36  来源:igfitidea点击:

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

excelvba

提问by yatici

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("A:N").Copy

    Workbooks.Add.SaveAs Filename:="output.xls"
    ExtFile = ThisWorkbook.Path & "\output.xls"

    Set ExtBk = Workbooks(Dir(ExtFile))
    ExtBk.Worksheets("Sheet1").Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone

    Application.DisplayAlerts = False
    ExtBk.Save
    Application.DisplayAlerts = True

End Sub

I'm getting an error at the PasteSpecialline with the error specified at the subject. I'm a bit confused since this works if I direct it to the source workbook.

PasteSpecial在与主题中指定的错误的行中收到错误。我有点困惑,因为如果我将它定向到源工作簿,这会起作用。

Maybe I need to use Windows(output.xls)?

也许我需要使用 Windows(output.xls)?

回答by David Zemens

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

Copy如果您只关心保存值,则根本不要使用方法。

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 ThisWorkbookis unsaved.

注意:如果您ThisWorkbook未保存,这将失败(之前的代码也会失败)。

回答by yatici

I made it work:

我让它工作:

Sub cp2NewWb()
    Dim ExtFile As String
    ExtFile = ThisWorkbook.Path & "output.xls"
    Workbooks.Add.SaveAs Filename:="output.xls"

    Windows("test1.xlsm").Activate
    Range("A1:AA100").Copy
    Windows("output.xls").Activate
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Worksheets(Worksheets.Count).Columns("A:AA").EntireColumn.AutoFit
    Range("A1").Select

    Windows("test1.xlsm").Activate
    Application.CutCopyMode = False
    Range("A1").Select
End Sub

I need to do it between activating windows or it doesn't work.

我需要在激活窗口之间执行此操作,否则它不起作用。

回答by Andy G

If you are copying the entire area, then copy the worksheets:

如果要复制整个区域,请复制工作表:

Worksheets("Sheet1").Copy Workbooks(2).Worksheets(1)

If it copies a couple of columns that you don't need then you could delete this afterwards.

如果它复制了您不需要的几列,那么您可以在之后删除它。

If you are copying from .xlsx to .xls then you'll need to use Copy/Paste:

如果您要从 .xlsx 复制到 .xls,则需要使用复制/粘贴:

Worksheets("Sheet1").UsedRange.Copy Workbooks(2).Worksheets(1).Range("A1")

If pasting values is required:

如果需要粘贴值:

Workbooks(2).Worksheets(1).UsedRange.Copy
Workbooks(2).Worksheets(1).Range("A1").PasteSpecial xlPasteValues

Be aware that UsedRangewon't start from A1 unless this cell has some content. In which case, you'll have to define a Rangeobject that starts at A1 and extends to the last used cell.

请注意,UsedRange除非此单元格有一些内容,否则不会从 A1 开始。在这种情况下,您必须定义一个Range从 A1 开始并延伸到最后使用的单元格的对象。

回答by artis_meditari

Private Sub ExceltoExcel()
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    'Input Data
     Sheets("Sheet1").Cells(1, 1).Select
     col = Sheets("Sheet1").Cells(2, 2)
     Dim exlApp As Excel.Application
     Dim ExtBk As Excel.Workbook
     Dim exlWs As Excel.Worksheet
     ExtFile = ThisWorkbook.Path & "\output.xls"
     Set exlApp = CreateObject("Excel.Application")
     Set ExtBk = exlApp.Workbooks.Open(ExtFile)
     Set exlWs = exlWb.Sheets("Sheet1")
     ExtBk.Activate
     exlWs.Cells(2, 2) = col
     'Output Data
     exlWs.Range("A1").Select
     exlWb.Close savechanges:=True
     Set ecxlWs = Nothing
     Set exlWb = Nothing
     exlApp.Quit
     Set exlApp = Nothing
     Application.EnableEvents = True
     Application.DisplayAlerts = True
End Sub