简体   繁体   English

在Excel-Vba中复制多列

[英]Copying Multiple columns in Excel-Vba

Hi I am trying to copy multiple columns from one workbook to another, and below is the code how I copied one and need help in making the code more optimized as I don't want to write same code for all the columns. 嗨,我正在尝试将多个列从一个工作簿复制到另一个工作簿,下面是我如何复制一个工作簿的代码,并且由于我不想为所有列编写相同的代码而需要帮助使代码更优化。 below is the code. 下面是代码。

Sub Copymc()

Dim x As Workbook
Dim y As Workbook

Set x = Workbooks.Open("H:\testing\demo\test2.xlsx")
Set y = Workbooks.Open("H:\testing\demo\test1.xlsx")
Dim LastRow As Long
Dim NextRow As Long

' determine where the data ends on Column B Sheet1

x.Worksheets("Sheet1").Activate
Range("A65536").Select
ActiveCell.End(xlUp).Select
LastRow = ActiveCell.Row

' copy the data from Column B in Sheet 1

Range("A2:A" & LastRow).Copy

' Determine where to add the new data in Column C Sheet 2

y.Worksheets("Sheet1").Activate
Range("A65536").Select
ActiveCell.End(xlUp).Offset(1, 0).Select
NextRow = ActiveCell.Row

' paste the data to Column C Sheet 2

y.Worksheets("Sheet1").Range("A" & NextRow).Select

ActiveSheet.Paste

Application.CutCopyMode = False

Range("A1").Select

End Sub

I tried to put all columns in range statement but problem I found was how to paste? 我试图将所有列都放在range语句中,但是发现的问题是如何粘贴? How can I do it for multiple columns without repeating the code? 如何在不重复代码的情况下针对多个列执行此操作? Thanks in advance. 提前致谢。

Let's say you want to copy columns AD: 假设您要复制广告列:

Sub Copymc()

Dim x As Workbook
Dim y As Workbook

Set x = Workbooks.Open("H:\testing\demo\test2.xlsx")
Set y = Workbooks.Open("H:\testing\demo\test1.xlsx")
Dim LastRow As Long
Dim NextRow As Long

' determine where the data ends on Column B Sheet1

x.Worksheets("Sheet1").Activate
Range("A65536").Select
ActiveCell.End(xlUp).Select
LastRow = ActiveCell.Row

' copy the data from Column B in Sheet 1

Range("A2:D" & LastRow).Copy y.worksheets("Sheet1").range("a65536").end(xlup).offset(1,0)

' Determine where to add the new data in Column C Sheet 2

'y.Worksheets("Sheet1").Activate
'Range("A65536").Select
'ActiveCell.End(xlUp).Offset(1, 0).Select
'NextRow = ActiveCell.Row

' paste the data to Column C Sheet 2

'y.Worksheets("Sheet1").Range("A" & NextRow).Select

'ActiveSheet.Paste

Application.CutCopyMode = False

Range("A1").Select

End Sub

I try to avoid the copy and paste functions as much as possible. 我尽量避免复制和粘贴功能。 To get around this I would loop through all of the values in the column and move them to your other workbook as such: 为了解决这个问题,我将遍历该列中的所有值,并将它们按如下方式移动到您的其他工作簿中:

Sub test()

Dim x As Workbook
Dim y As Workbook

Set x = Workbooks.Open("H:\testing\demo\test2.xlsx")
Set y = Workbooks.Open("H:\testing\demo\test1.xlsx")
Dim LastRow As Long

LastRow = x.Sheets("Sheet1").Range("A65536").End(xlUp).Row

For i = 1 To LastRow
    CopyVal = x.Sheets("Sheet1").Range("A1").Offset(i, 0).Value
    CopyVal2 = x.Sheets("Sheet1").Range("A1").Offset(i, 1).Value
    CopyVal3 = x.Sheets("Sheet1").Range("A1").Offset(i, 2).Value
    CopyVal4 = x.Sheets("Sheet1").Range("A1").Offset(i, 3).Value

    y.Sheets("Sheet1").Range("A65536").End(xlUp).Offset(1, 3).Value = CopyVal4
    y.Sheets("Sheet1").Range("A65536").End(xlUp).Offset(1, 2).Value = CopyVal3
    y.Sheets("Sheet1").Range("A65536").End(xlUp).Offset(1, 1).Value = CopyVal2
    y.Sheets("Sheet1").Range("A65536").End(xlUp).Offset(1, 0).Value = CopyVal

Next

End Sub

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

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