简体   繁体   中英

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? 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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