Macro to copy specific columns from one workbook to another workbook

whoosh

New Member
Joined
May 16, 2018
Messages
17
Hi,

Hope someone can help me out on this. I'm trying to copy several columns from 1 workbook (Source File.xls - "sheet1") to another workbook (Target File.xlsm - worksheet ("final").

I need to run a macro to copy specific columns (A, B, C, F, G) from "Source" (but omitting the last 1 row "Not req") to (a, b, c, f, g) on "Target" without overwriting the existing data.

Source File
ABCDEFG
1111222233334444555566667777
fdsdfddacdgddsfafsdhgfghxdvs
qezcczgdfvxbbdbfvdsvsdvsdadsa
Not reqNot reqNot reqNot reqNot reqNot reqNot req

<tbody>
</tbody>

Target File
aa1a2bcc1defg
existing dataexisting dataexisting dataexisting dataexisting dataexisting dataexisting dataexisting dataexisting dataexisting data
existing dataexisting dataexisting dataexisting dataexisting dataexisting dataexisting dataexisting dataexisting dataexisting data
existing dataexisting dataexisting dataexisting dataexisting dataexisting dataexisting dataexisting dataexisting dataexisting data
1111222233334444555566667777
fdsdfddacdgddsfafsdhgfghxdvs
qezcczgdfvxbbdbfvdsvsdvsdadsa

<tbody>
</tbody>

I have tried some codes but it doesn't deliver what I want. My apology, don't know how to attach files.
Code:
Sub CopySource()


    Dim SourceWB As Workbook: Set SourceWB = Workbooks("Source File.xlsx")
    Dim TargetWB As Workbook: Set TargetWB = Workbooks("Target File.xlsm")
    Dim lr As Long: lr = SourceWB.Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
    Dim lastr As Long: lastr = TargetWB.Sheets("Final").Cells(Rows.Count, "A").End(xlUp).Row
    
    SourceWB.Sheets("Sheet1").Range("A2:H5000").Copy Destination:=TargetWB.Sheets("Final").Range("A" & lr)


End Sub



Thanks in advance
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
the code could be modified to
either
- allow user to navigate to correct file
or
- let VBA determine the folder based on week number provided by user
(add input box or get from cell in worksheet, requires "rule" to generate folder path)
or
- use your present method with both files open at the start


Thanks Yongle.

Still learning.. and consider a noob when coding is concern. :p
How I wish i can code like you guys.
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,684
Members
449,048
Latest member
81jamesacct

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top