how to return column where header value is located...

sadams1

Board Regular
Joined
Aug 19, 2006
Messages
221
I have a large table with many columns that have corresponding headers. Is it possible to search for a specific name in the header row & return it's corresponding column? Using xlookup, vlookup, or hlookup doesn't seem to do this & I've been unable to find any code as an example but a quick data set follows...

A B C
this that other

how to search from anywhere in the table what column "other" belongs to? It would return "C".
Thanks!
Steve
 
Sub Maybe()
MsgBox Replace(Replace(Rows(1).Find("Header").Address, "$", ""), "1", "") '<---- Change the word "Header" to the actual Header
End Sub
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try:
VBA Code:
Sub Test()
    Dim col As Long
    col = Rows(1).Find("other", LookIn:=xlValues, lookat:=xlWhole).Column
    MsgBox Replace(Cells(1, col).Address(0, 0), "1", "")
End Sub

Quick question...I'm limited in coding so tell me to jump in a lake if this too much but is it much more of a stretch to have this start with a message box to input what to search for? In this case, what to replace "other" with?
 
Upvote 0
VBA Code:
Sub Maybe()
Dim msg As String
msg = InputBox(Prompt:="What is the Header?", Title:="Find Column")
    MsgBox Replace(Replace(Rows(1).Find(msg, , , 1).Address, "$", ""), "1", "")
End Sub
You can do the same for other macros.
 
Upvote 0
Solution
VBA Code:
Sub Maybe()
Dim msg As String
msg = InputBox(Prompt:="What is the Header?", Title:="Find Column")
    MsgBox Replace(Replace(Rows(1).Find(msg, , , 1).Address, "$", ""), "1", "")
End Sub
You can do the same for other macros.


that is so COOL! many thanks!
 
Upvote 0
Another way
VBA Code:
Sub sadams()
   Dim Res As String
   Res = InputBox("Look for")
   MsgBox Split(Range("1:1").Find(Res, , , xlWhole, , , False, , False).Address(1, 0), "$")(0)
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,838
Members
449,471
Latest member
lachbee

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