VBA copy based on column header

DrH100

Board Regular
Joined
Dec 30, 2011
Messages
78
I've no idea where to start with this so any help appreciated

What I would like to do is search through an active worksheet until I find a specific column header ("Origin") in row 1, copy the entire column contents (minus the row 1 header) and paste it to a specific cell (D50) on another worksheet ("Track").

The active worksheet pulls it's data from elsewhere and the base data keeps changing so the column for "Origin" keeps changing, hence the need to search for that column header

Hope that makes some sense and any thoughts greatly appreciated.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try this:
VBA Code:
Sub MyMoveMacro()

    Dim fCell As Range
    Dim col As Long
    Dim lr As Long
    Dim rng As Range

'   Find cell in row 1 equal to "Origin"
    Set fCell = Rows("1:1").Find(What:="Origin", After:=Range("A1"), LookIn:=xlFormulas2, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
        
'   Check to see if found
    If fCell Is Nothing Then
        MsgBox "Value of 'Origin' not found in row 1 on sheet " & ActiveSheet.Name, vbOKOnly, "ERROR!"
        Exit Sub
    End If

'   Find last row with data in "Origin" column
    col = fCell.Column
    lr = Cells(Rows.Count, col).End(xlUp).Row

'   Build range to copy
    Set rng = Range(Cells(2, col), Cells(lr, col))
    
'   Copy range to Track sheet
    rng.Copy Sheets("Track").Range("D50")
    
End Sub
 
Upvote 0
Solution
Try this:
VBA Code:
Sub MyMoveMacro()

    Dim fCell As Range
    Dim col As Long
    Dim lr As Long
    Dim rng As Range

'   Find cell in row 1 equal to "Origin"
    Set fCell = Rows("1:1").Find(What:="Origin", After:=Range("A1"), LookIn:=xlFormulas2, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
       
'   Check to see if found
    If fCell Is Nothing Then
        MsgBox "Value of 'Origin' not found in row 1 on sheet " & ActiveSheet.Name, vbOKOnly, "ERROR!"
        Exit Sub
    End If

'   Find last row with data in "Origin" column
    col = fCell.Column
    lr = Cells(Rows.Count, col).End(xlUp).Row

'   Build range to copy
    Set rng = Range(Cells(2, col), Cells(lr, col))
   
'   Copy range to Track sheet
    rng.Copy Sheets("Track").Range("D50")
   
End Sub
Thanks very much. I couldn't get it to paste into the other sheet but just changed the last Line or 2 and it works perfectly.

Would never have got anywhere near that.

Thanks
 
Upvote 0
You are welcome.
 
Upvote 1

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

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