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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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