Copy paste column based on range from another column

Aviles

Board Regular
Joined
Dec 17, 2008
Messages
167
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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try this :
VBA Code:
Range(rngFound, Cells(Rows.Count, rngFound.Column).End(xlUp)).Copy Destination:=Sheets("Scheduler").Cells(1, i)
 
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

Forum statistics

Threads
1,223,996
Messages
6,175,862
Members
452,676
Latest member
woodyp

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