Copying different columns from one workbook to another VBA

ccalkins

New Member
Joined
Jul 13, 2016
Messages
13
Office Version
  1. 365
Platform
  1. MacOS
I am trying to write a code to open a workbook titled 64A, copy columns a, c:f, close and then paste the data into the workbook with the macro in it. I am comfortable using formulas in Excel but am just starting to learn VBA. I have come up with the script below through many internet searches. I can get it to copy one column but when I try to make it copy more than one column it gives me error messages. Any help would be appreciated.
Thanks,
Craig

Sub Macro2()

'Assign variable name to Target workbook
Var1 = ActiveWorkbook.Name
'Assign variable name to Target range
Var1R = "A1"

'Open Source WorkBook
Application.Workbooks.Open ("C:........\64A.xlsx")

'Assign variable name to Source workbook
Var2 = ActiveWorkbook.Name
Var2R = 1

'Copy from Source to Target
Sheets(Var2R).Columns("C").EntireColumn.Copy _
Destination:=Workbooks(Var1).Sheets("Sheet1").Range(Var1R)

'Close Source WorkBook wo/Save
Workbooks(Var2).Close False
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi
Welcome to the board

This is an example similar to what you need. Examine it and adapt for your case.

Notes:
- you don't use strings to store the names of what you need. Instead you set references to the objects you need
- you should first declare all the variables that you are going to use, which means that you'll think about what you need to do before writing the code.


Code:
Sub Test()
Dim wbkSrc As Workbook ' source workbook
Dim wbkDest As Workbook ' destination workbook
Dim rSrc As Range ' source range
Dim rDest As Range ' destination range

' set references to the workbooks
Set wbkDest = ActiveWorkbook
Set wbkSrc = Application.Workbooks.Open("c:\tmp\book1.xlsm")

' set references to the ranges
Set rDest = wbkDest.Worksheets("Sheet1").Range("A1")
Set rSrc = wbkSrc.Worksheets(1).Columns("C")

'Copy from Source to Target
rSrc.Copy Destination:=rDest

'Close Source WorkBook wo/Save
wbkSrc.Close False
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,628
Members
449,241
Latest member
NoniJ

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