need macro to copy and paste a column in one worksheet into the first blank column in another

bcselect

Board Regular
Joined
May 13, 2021
Messages
80
Office Version
  1. 365
Platform
  1. Windows
need macro to copy and paste a column from one worksheet into the first blank column in another
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
VBA Code:
Sub bcselect()
'assume paste col C from sheet1 to first blank col in sheet2
Dim lCol As Long
lCol = Sheets("Sheet2").Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
With Sheets("sheet1")
    .Range("C:C").Copy Destination:=Sheets("Sheet2").Columns(lCol + 1)
End With
End Sub
 
Upvote 0
spoke to soon. not working. doesn't transfer column to 2nd sheet
 
Upvote 0
spoke to soon. not working. doesn't transfer column to 2nd sheet
What happens when you run the code? Is there a runtime error? If so, what's the error message and what line is highlighted? Are you using the correct name for the second sheet in the code? Is the second sheet blank prior to running the code? Is the column to be transferred empty?
 
Upvote 0
@JoeMo my testing of your code, results = your code copies the column to the end of the used column range, not the first blank column encountered.
 
Upvote 0
@JoeMo my testing of your code, results = your code copies the column to the end of the used column range, not the first blank column encountered.
This revision will paste to first blank column in Sheet2 used range if there is one, and to the first column to the right of the used range if there is not a blank column within the used range of Sheet2.
VBA Code:
Sub bcselect()
'assume paste col C from sheet1 to first blank col in sheet2
Dim col As Range, lCol As Long
For Each col In Sheets("Sheet2").UsedRange.Columns
    If Application.CountA(col) = 0 Then
        With Sheets("sheet1")
            .Range("C:C").Copy Destination:=col
            GoTo Nx
        End With
    End If
Next col
lCol = Sheets("Sheet2").Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
With Sheets("sheet1")
    .Range("C:C").Copy Destination:=Sheets("Sheet2").Columns(lCol + 1)
End With
Nx:
End Sub
 
Upvote 0
This revision will paste to first blank column in Sheet2 used range if there is one, and to the first column to the right of the used range if there is not a blank column within the used range of Sheet2.
@JoeMo No need to vaguely word it, just say 'whoops, here you go, now it copies the column to the first blank column found on the other sheet" :) !!!

The way you stated it, it sounds like it could copy the column twice to the other sheet. And vs Or basically.
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,454
Members
449,083
Latest member
Ava19

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