简体   繁体   English

Excel VBA将数据从一个工作簿复制到另一工作簿

[英]Excel VBA copy data from one workbook to another workbook

I have a workbook that has four worksheets with data in each. 我有一个工作簿,其中有四个工作表,每个工作表中都有数据。 I need to open another workbook and copy from there into each of the original files worksheets. 我需要打开另一个工作簿,然后从那里复制到每个原始文件工作表中。

The data is setup as tables so I need to leave the first row headers in place. 数据设置为表格,因此我需要保留第一行标题。

This is what I'm using now (see below), but I read that there is a better way to do it with something like this. 这就是我现在正在使用的(请参阅下文),但是我读到有一种更好的方法可以执行此操作。

Workbooks("File1.xls").Sheets("Sheet1").range("A1").Copy Workbooks("File2.xls").Sheets("Sheet2").range("A1") Workbooks(“ File1.xls”)。Sheets(“ Sheet1”)。range(“ A1”)。Copy Workbooks(“ File2.xls”)。Sheets(“ Sheet2”)。range(“ A1”)

The problem I have is I don't know how to copy everything except the first row. 我的问题是我不知道如何复制除第一行以外的所有内容。 With the code I'm currently using I recorded a macro that goes to cell A2 and uses CMD+SHF+END to grab all the data. 使用当前正在使用的代码,我记录了一个宏,该宏转到单元格A2,并使用CMD + SHF + END捕获所有数据。

Thanks in advance for any help you can give me. 预先感谢您可以给我的任何帮助。

Sub UpdateData()
'
' UpdateData Macro
Application.ScreenUpdating = False
' Clear current data.
    Sheets("ClientInfo").Select
    Rows("2:" & Rows.Count).ClearContents
    Sheets("Quotes").Select
    Rows("2:" & Rows.Count).ClearContents
    Sheets("PolicyPlanData").Select
    Rows("2:" & Rows.Count).ClearContents
    Sheets("EstimatedPremium").Select
    Rows("2:" & Rows.Count).ClearContents

'Open Data file.
    Workbooks.Open Filename:= _
        "W:\My File Cabinet\cndjrdn\BGA\ClientBio\ClientData.xls"
'Copy data into each worksheet.
Application.CutCopyMode = False
     Windows("ClientData.xls").Activate
        Application.GoTo Sheets("ClientInfo").Range("A2")
        Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
        Selection.Copy
    Windows("BGA Client Bio May2016v4.xlsx.xlsm").Activate
        Application.GoTo Sheets("ClientInfo").Range("A2")
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Range("A2").Select
    Windows("ClientData.xls").Activate
        Application.GoTo Sheets("Quotes").Range("A2")
        Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
        Selection.Copy
    Windows("BGA Client Bio May2016v4.xlsx.xlsm").Activate
        Application.GoTo Sheets("Quotes").Range("A2")
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Range("A2").Select
    Windows("ClientData.xls").Activate
        Application.GoTo Sheets("PolicyPlanData").Range("A2")
        Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
        Selection.Copy
    Windows("BGA Client Bio May2016v4.xlsx.xlsm").Activate
        Application.GoTo Sheets("PolicyPlanData").Range("A2")
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Range("A2").Select
    Windows("ClientData.xls").Activate
        Application.GoTo Sheets("EstimatedPremium").Range("A2")
        Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
        Selection.Copy
    Windows("BGA Client Bio May2016v4.xlsx.xlsm").Activate
        Application.GoTo Sheets("EstimatedPremium").Range("A2")
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Range("A2").Select
'Refresh PivotTable(s)
    ThisWorkbook.RefreshAll
'Close Data File
    Windows("ClientData.xls").Activate
        ActiveWorkbook.Close SaveChanges:=False

End Sub

Try using named ranges for the tables that you want to duplicate. 尝试为要复制的表使用命名范围。 Dynamic named ranges allow the range to automatically resize if you table changes width or length. 如果表更改宽度或长度,则动态命名范围允许范围自动调整大小。 Drop named ranges into Excel Arrays and then drop the array into the new location. 将命名范围拖放到Excel数组中,然后将数组拖放到新位置。 It's much faster than copy and paste and it allows you to do all the copies without needing to switch back and forth between the worksheets. 它比复制和粘贴要快得多,它使您可以进行所有复制而无需在工作表之间来回切换。 Working in arrays for manipulating data and making calculations is much faster than using the cells of the spreadsheet to do the same. 与使用电子表格的单元格进行操作相比,在数组中处理数据和进行计算要快得多。

As another advisor said, get rid of the .selects for worksheets and ranges. 正如另一位顾问所说,摆脱工作表和范围的.selects。 To clear a range, just use something like: 要清除范围,只需使用以下命令:

Range("A1:Y240").ClearContents

Or to do this with an table of unknown width and height starting at A1: 或者使用从A1开始的宽度和高度未知的表格来执行此操作:

Sheets("ClientInfo").Range("A1").Resize(Cells(Rows.Count, "A").End(xlUp).Row, Cells(1, columns.Count).End(xlToLeft).Column).ClearContents

The only requirement is that the Column A and Row 1 have no blanks from beginning to end. 唯一的要求是,列A和行1从头到尾都没有空白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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