VBA to find my headers and use them instead of range

Vivelake

New Member
Joined
Feb 17, 2022
Messages
10
Hi,

I have a spreadsheet where the columns changed at times (more are added or some are deleted). It's too much work to edit the ranges in all my macros every time. Is there a way for the VBA to figure out what column Q certain header is in and adjust the letters in the range accordingly? I need quite a simple solution to this.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
If the header name remains consistent then have VBA search for it and set the column number/letter from that result
 
Upvote 0
You could write a function that returns the column that has a specified header

VBA Code:
Function NamesDataRange() as Range
    With Sheets("Sheet1")
        Set NamesDataRange = .Columns(Application.Match("Name", .Rows(1), 0)).EntireColumn
    End With
End Function

And use NamesDataRange throughout your code as you would a range variable. Or set a variable to its value.
 
Upvote 0
You could write a function that returns the column that has a specified header

VBA Code:
Function NamesDataRange() as Range
    With Sheets("Sheet1")
        Set NamesDataRange = .Columns(Application.Match("Name", .Rows(1), 0)).EntireColumn
    End With
End Function

And use NamesDataRange throughout your code as you would a range variable. Or set a variable to its value.
How would that work if I for example wanted to select the column that had "Apple" as a header, and highlight it in yellow or something like that?
 
Upvote 0
You would first rewrite NamesDataRange, either as a hardcoded AppleDataRange function or as a function that takes a header as an argument.

Then the code turning things yellow would be AppleDataRange.Color = vbYellow

Or HeaderDataRange("apple").Color = vbYellow depending on how you wanted to alter the UDF.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

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