Search through column headers to find the right Column to copy and paste

HeyHey89

New Member
Joined
Jun 22, 2018
Messages
5
I am trying to look for a column name then select the data below to copy to another sheet HELP!

Sub Trial()

Dim rngFound As Range

ActiveCell.AutoFilterMode = True

Set rngFound = Rows(1).Find("List", Cells(1, Columns.Count), xlValues, xlWhole)

If Not rngFound Is Nothing Then

Columns(rngFound.Column).Copy Destination:=Worksheets("Sheet2").Cells("A2")

Application.CutCopyMode = False

End If

EsleIf rngFound Is Nothing Then

End If

End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try this:
Code:
Sub Copy_Column()
'Modified 6/23/2018 12:15 AM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim LastrowColumn As Long
LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 1 To LastColumn
        If Cells(1, i).Value = "List" Then
            Columns(i).Copy Sheets(2).Cells(1, 1): Exit Sub
        End If
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
And another possibility. No Looping.
Code:
Sub AAAAA()
Dim colCopy As Long, lr As Long
    If Application.WorksheetFunction.CountIf(Sheets("Sheet1").Rows(1), "List") > 0 Then
        colCopy = Rows(1).Find("List", , , 1).Column
            lr = Cells(Rows.Count, colCopy).End(xlUp).Row
        Range(Cells(2, colCopy), Cells(lr, colCopy)).Copy Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1)
    Else
        MsgBox "No such value in the first row."
    End If
End Sub
BTW HeyHey, please use code tags around your code.
 
Last edited:
Upvote 0
Another option
Code:
Sub Trial()
   Dim rngFound As Range
   
   Set rngFound = Rows(1).Find("List", , xlValues, xlWhole, , , False, , False)
   If Not rngFound Is Nothing Then
      Columns(rngFound.Column).Copy Worksheets("Sheet2").Cells("A1")
   End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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