Help declaring columns based on location of value

shane_aldrich

Board Regular
Joined
Oct 23, 2009
Messages
148
Right now in cell AE1 the value = "SUM", but the word "SUM" floats in row 1

I'd like a macro that looks through row1, and gives me the column letter that the word "SUM" resides in

sCol_1 As String

sCol1 = (Where ever the word "SUM" is located in row 1)

not sure if this is the best way

Thanks
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

davesweep

Well-known Member
Joined
Apr 30, 2007
Messages
510
Hi,

How's this for you?

Code:
Sub FindSum()
Dim iCol_1 As Integer, sCol_1 As String

iCol_1 = Application.WorksheetFunction.Match("SUM", Range("1:1"), 0)
sCol_1 = Left(Cells(1, iCol_1).Address(1, 0), InStr(1, Cells(1, iCol_1).Address(1, 0), "$") - 1)

End Sub
 

Jerry Sullivan

MrExcel MVP
Joined
Mar 18, 2010
Messages
8,787
Hi Dave,

Are you sure the task you're doing requires you to have the Column Letter?

Sometimes people ask how to convert Column Numbers to Letters, because they want to use the Letter to build a Range Address Reference. That typically isn't necessary though since within VBA you can use the Column Number instead of the letter.

As a simplified example, if the text "SUM" is in H1 and your task is to read the value below it in H2,
you could use VBA code to build the string "H2" for: Range(sCol_1 & 2).Value
but you would save some steps by simply using: Cells(2, iCol_1).Value
 

Watch MrExcel Video

Forum statistics

Threads
1,130,112
Messages
5,640,160
Members
417,128
Latest member
Xianter

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
Top