简体   繁体   English

将单元格从打开的工作簿复制到另一个

[英]Copy cells from open workbook into another

In my first active workbook, column A has a list of numbers, starting in cell A1, and listing down a varied number of rows each time.在我的第一个活动工作簿中,A 列有一个数字列表,从单元格 A1 开始,每次列出不同数量的行。

I would like to copy all of the column A cells containing information into another workbook (if the second workbook could remain closed during the process that would be preferable).我想将所有包含信息的 A 列单元格复制到另一个工作簿中(如果第二个工作簿在此过程中可以保持关闭状态,那将是可取的)。

My desired paste location in the second workbook would be column A in the first empty row available.我在第二个工作簿中所需的粘贴位置将是第一个可用空行中的 A 列。 ie I want this second workbook to be a list of all of the data column A of the first workbook has ever had.即我希望这第二个工作簿是第一个工作簿的所有数据列 A 的列表。

Paste this Code into a new Module in the VBA Editor of the Workbook which has the Source Data (Open VBA Editor: ALT+F11) and run the Macro "CopyToAnotherWorkbook".将此代码粘贴到具有源数据的工作簿的 VBA 编辑器中的新模块中(打开 VBA 编辑器:ALT+F11)并运行宏“CopyToAnotherWorkbook”。

before running, specify the Destination Workbook Path, hint is in the code.运行前,指定目标工作簿路径,提示在代码中。

Sub CopyToAnotherWorkbook()

Application.ScreenUpdating = False

Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.ActiveSheet
Dim LastRow_wb%: LastRow_wb = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Dim arr() As Variant
ReDim arr(0 To LastRow_wb - 1)

For i = 1 To LastRow_wb
    arr(i - 1) = ws.Cells(i, 1)
Next i

Dim wb2 As Workbook: Set wb2 = Workbooks.Open("C:\Book2.xlsx") ' <- Paste your Link to the Workbook here!

Dim ws2 As Worksheet: Set ws2 = wb2.Sheets(1)
Dim LastRow_wb2%: LastRow_wb2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row + 1

If LastRow_wb2 = 2 Then
    LastRow_wb2 = 1
End If

ws2.Range("A" & LastRow_wb2 & ":A" & LastRow_wb2 + UBound(arr)).Value = WorksheetFunction.Transpose(arr)

Application.ScreenUpdating = True
wb2.Close True
Set ws2 = Nothing
Set wb2 = Nothing
Set ws = Nothing
Set wb = Nothing

End Sub

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

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