VBA Excel Macro Copying One Column into Another Workbook

excel, vba

Solution

EDIT : Just realized what's probably missing from that piece of code you have. Add a ":" in there! Change to `destination:=Workbooks("....` and it should work fine.

Extra details : When working with function parameters, you have to add the ":" in order to specify to the computer you're not evaluating an equality, but doing a parameter assignment.

(Old, flashy answer) As I assume this is what you're trying to do; this script will probably do what you want. The style will NOT be preserved however.

Sub yourSub()

Application.ScreenUpdating = False 'Disables "Screen flashing" between 2 workbooks

Dim colA As Integer, colB As Integer
Dim rowA As Integer, rowB As Integer
Dim wbA As Workbook, wbB As Workbook

Set wbA = ThisWorkbook
Set wbB = Workbooks.Open("C:/YourFilePath/YourFile.xls")

colA = 1 'Replace "1" with the number of the column FROM which you're copying
colB = 1 'Replace "1" with the number of the column TO which you're copying

rowA = 1 'Replace "1" with the number of the starting row of the column FROM which you're copying
rowB = 1 'Replace "1" with the number of the row of the column TO which you're copying

wbA.Activate
lastA = Cells(Rows.Count, colA).End(xlUp).Row 'This finds the last row of the data of the column FROM which you're copying
For x = rowA To lastA 'Loops through all the rows of A
    wbA.Activate
    yourData = Cells(x, colA)
    wbB.Activate
    Cells(rowB, colB) = yourData
    rowB = rowB + 1 'Increments the current line of destination workbook
Next x 'Skips to next row

Application.ScreenUpdating = True 'Re-enables Screen Updating

End Sub

I didn't have time to test this yet. Will do as soon as possible.

Problem

I have 2 workbooks and am trying to find a way to copy a column from wb1 into wb2. I am aware I could just copy/paste, but the idea is to make something so my boss can click the macro and everything populates. I have been trying a code I came across in another question: ``` Sub Import() Dim sourceColumn As Range Dim destColumn As Range Set sourceColumn = Workbooks("C:\Documents and Settings\********\My Documents\*********.xlsm").Worksheets(2).Columns("BL") Set destColumn = Workbooks("C:\Documents and Settings\********\My Documents\*********.xlsm").Worksheets(2).Columns("A") sourceColumn.Copy Destination = destColumn End Sub ``` When I run this, I get a "Subscript Out Of Range" Error. The source column contains a formula relying on other columns and the destination column is empty, but even when I ran this on dummy workbooks with small digits I got the same error. Is there something super basic I am missing here?

Original source

Related problems