vba - finding column title in row 1 and using it for reference

borntorun75

Board Regular
Joined
Jul 12, 2010
Messages
57
Hi there,

I'm dipping my toes into VBA after many years using Excel. I have a question about referencing a cell - I expect it'll probably be a straight forward reply for those familiar in VBA. However, question aside, I want to see if my approach is sensible or would there be a better way of working.

My macro will be running in a spreadsheet where the column positions will generally be fixed. e.g. In column M, (and specifically cell M1), the text / column heading will be "Production Description". My column headings will always be in row 1.

However, I want to have some flexibility in my VBA referencing where I can find "Production Description" in any of the column headings within the spreadsheet. It's just in case there's any changes in the format of the spreadsheet, (e.g. columns added or dropped), that would otherwise upset the referencing.

The column headings will always be in row 1, but (for the sake of this exercise), the heading will be in any of the columns A to XFD (I'm using Office 2007).

Once I've got the column reference, I'd then continue to reference cells down that column.

Hope I'm making sense (!).

Best regards.
borntorun75
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
See if this helps

Code:
Sub atest()
Dim Found As Range, iCol As Long
Set Found = Rows(1).Find(what:="Production Description", LookIn:=xlValues, lookat:=xlWhole)
If Found Is Nothing Then
    MsgBox "not found", vbExclamation
Else
    iCol = Found.Column
    MsgBox iCol, vbInformation
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,183
Members
452,893
Latest member
denay

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