VBA find column by name and copy all data t the last row

Addictions

Board Regular
Joined
May 27, 2018
Messages
60
Office Version
  1. 365
Hello,
Please can someone possibly help.
What changes should I make to code below so it can find the column by name instead of specific range and copy all data to the last row

VBA Code:
LastRow = .Range("T" & .Rows.Count).End(xlUp).Row

Sheets("Sheet1").Range("A2:A" & LastRow).Copy
Sheets("Sheet2").Range("1:48").Find("Full name").Offset(1, 0).PasteSpecial xlPasteValues

Many thanks,
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
How do you know the name of the column you are copying it to?
Can you show us an example of what the data you are copying looks like, and where you are pasting it?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
How do you know the name of the column you are copying it to?
Can you show us an example of what the data you are copying looks like, and where you are pasting it?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
Hi,
It is just a table with columns and data and I would rather want to use the name of column instead of specific range.
In this case it would be "Full name" column.

The column "T" always has data populated to last row.
LastRow = .Range("T" & .Rows.Count).End(xlUp).Row
 
Upvote 0
Here is a little demo to show you how you can find the column number that contains the title you are looking for:
VBA Code:
Sub FindColumn()

    Dim r As Long
    Dim lc As Long
    Dim c As Long
    Dim str As String
    Dim f As Long
    
'   Designate header row number to search
    r = 1

'   Designate column title to find
    str = "Full name"
    
'   Find last column in header row
    lc = Cells(r, Columns.Count).End(xlToLeft).Column
        
'   Find column name in header row
    For c = 1 To lc
'       Check each column for desired column title
        If Cells(r, c) = str Then
            f = c
            Exit For
        End If
    Next c

'   Return column number of column title
    If f > 0 Then
        MsgBox "'" & str & "' found in column number " & f, vbOKOnly, "Column Title Found!"
    Else
        MsgBox "'" & str & "' not found in row " & r, vbOKOnly, "Column Title NOT Found!"
    End If

End Sub
Note that this code can be shortened (i.e. you actually don't need the message boxes and to set all the values equal to variables, I just did that for demonstration purposes - the important part is the loop).
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
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