VBA - copy coulmn with value from cell in row 1

nutratroy

New Member
Joined
Jan 11, 2016
Messages
2
This seems straightforward so I thought I'd be able to find it, but I've been searching for an hour now without any luck. I'm new to VBA so I appreciate any help!

Sheet2 has a bunch of data organized in columns, with each column identified by a unique number in Row 1. If I type a number in a cell (say A1) on Sheet1, I need a script that will copy the column that has that number in Row 1.

Long time lurker, first time poster. You guys are amazing, thanks in advance!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Care to share any thoughts you might have about what to do with the copy once its on the clipboard?
 
Upvote 0
You were not clear on how you wanted to trigger the code to run, ie. manually, by button or event, so I made it manually. Copy the code to the standard code module 1. See below for access intstructions and how to run the code from Excel.
Code:
Sub findNCopy()
Dim sh1 As Worksheet, sh2 As Worksheet, f As Range
Set sh1 = Sheets(1) 'Replace the index numbers with sheet names enclosed in Quote marks.
Set sh2 = Sheets(2) 'same as above
    Set f = sh2.Range("1:1").Find(sh1.Range("A1").Value, , xlValues, xlWhole)
        If Not f Is Nothing Then
            f.EntireColumn.Copy sh1.Cells(1, Columns.Count).End(xlToLeft).Offset(, 1)
        End If
End Sub
 
Upvote 0
Here is my longwinded attempt.
Similar as previous code.

Code:
Sub Maybe()
    Dim a As Long, sh1 As Worksheet, sh2 As Worksheet
    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")
    a = sh2.Rows(1).Find(sh1.Range("A1").Value, , , 1).Column

    With sh2
        .Range(.Cells(1, a), .Cells(.Cells(.Rows.Count, a).End(xlUp).Row, a)).Copy sh1.Range("A1")
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,874
Messages
6,122,034
Members
449,061
Latest member
TheRealJoaquin

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