Copy paste column based on range from another column

Aviles

Board Regular
Joined
Dec 17, 2008
Messages
173
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have the below code which looks up column headings from one sheet and pastes the columns into another sheet. It works well, apart from situations where one column has cells that are blank, in which case it only does a partial copy/paste because it stops copying when it reaches a blank cell.

In this example, the 'Date' column will never have any blank cells. Is there a way to have this code look up the range for the 'Date' column and base the copy/paste from that column range?

Thanks.

VBA Code:
Dim vHeader As Variant, rngFound As Range, i As Long
    For Each vHeader In Array("Date", "Time", "C", "Country/Region", "Event", "S")
        Set rngFound = Sheets("BBGExport").Cells.Find(vHeader, , xlValues, xlWhole, 1, 1, 0)
        i = i + 1
        If Not rngFound Is Nothing Then
            Range(rngFound, rngFound.End(xlDown)).Copy Destination:=Sheets("Scheduler").Cells(1, i)
        End If
    Next
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try this :
VBA Code:
Range(rngFound, Cells(Rows.Count, rngFound.Column).End(xlUp)).Copy Destination:=Sheets("Scheduler").Cells(1, i)
 
Upvote 0
Try this :
VBA Code:
Range(rngFound, Cells(Rows.Count, rngFound.Column).End(xlUp)).Copy Destination:=Sheets("Scheduler").Cells(1, i)
Hi @footoo thanks for the quick reply, but I'm getting the following error:

Run-time error '1004':
Method 'Range' of object'_Global' failed
 
Upvote 0
Hi Aviles,

See how this goes:

VBA Code:
Option Explicit
Sub Macro1()

    Dim vHeader As Variant, rngFound As Range, i As Long, lngLastRow As Long
    Dim strCol As String
    
    Application.ScreenUpdating = False
    
    For Each vHeader In Array("Date", "Time", "C", "Country/Region", "Event", "S")
        Set rngFound = Sheets("BBGExport").Cells.Find(vHeader, , xlValues, xlWhole, 1, 1, 0)
        i = i + 1
        If Not rngFound Is Nothing Then
            strCol = Split(rngFound.Address, "$")(1) 'Though we don't need to use the column letter I find it much easier to work with
            lngLastRow = Sheets("BBGExport").Cells(Rows.Count, strCol).End(xlUp).Row
            'Range(rngFound, rngFound.End(xlDown)).Copy Destination:=Sheets("Scheduler").Cells(1, i)
            Sheets("BBGExport").Range(strCol & "1:" & strCol & lngLastRow).Copy Destination:=Sheets("Scheduler").Cells(1, i)
        End If
    Next vHeader
    
    Application.ScreenUpdating = True
    
End Sub

Regards,

Robert
 
Upvote 0
Hi @footoo thanks for the quick reply, but I'm getting the following error:

Run-time error '1004':
Method 'Range' of object'_Global' failed

Change this line...

VBA Code:
Range(rngFound, Cells(Rows.Count, rngFound.Column).End(xlUp)).Copy Destination:=Sheets("Scheduler").Cells(1, i)

...to this:

VBA Code:
Range(rngFound, Sheets("BBGExport").Cells(Rows.Count, rngFound.Column).End(xlUp)).Copy Destination:=Sheets("Scheduler").Cells(1, i)
 
Upvote 0
Change this line...

VBA Code:
Range(rngFound, Cells(Rows.Count, rngFound.Column).End(xlUp)).Copy Destination:=Sheets("Scheduler").Cells(1, i)

...to this:

VBA Code:
Range(rngFound, Sheets("BBGExport").Cells(Rows.Count, rngFound.Column).End(xlUp)).Copy Destination:=Sheets("Scheduler").Cells(1, i)

Thanks @Trebor76 that worked great!

If you don't mind, for my knowledge, can you tell me which part of this code picks up the correct range?
 
Upvote 0
Thanks @Trebor76 that worked great!

That's great and you're welcome although footoo should really be thanked

This picks up the range:

VBA Code:
Range(rngFound, Sheets("BBGExport").Cells(Rows.Count, rngFound.Column).End(xlUp))

The only issue with the original code was that it must have been run on a different tab than where the source data resides (i.e. BBGExport) so prefixing the range with it made the code run as expected.

Thanks,

Robert
 
Upvote 0
That's great and you're welcome although footoo should really be thanked

This picks up the range:

VBA Code:
Range(rngFound, Sheets("BBGExport").Cells(Rows.Count, rngFound.Column).End(xlUp))

The only issue with the original code was that it must have been run on a different tab than where the source data resides (i.e. BBGExport) so prefixing the range with it made the code run as expected.

Thanks,

Robert

Thanks for clarifying, and yes, thank you @footoo also
 
Upvote 0

Forum statistics

Threads
1,224,895
Messages
6,181,620
Members
453,057
Latest member
LE102024

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