Change Column Header that Contains a Word

bmkelly

Board Regular
Joined
Mar 26, 2020
Messages
172
Office Version
  1. 365
Platform
  1. Windows
Hello,

I thought a simple google search would do but seems I can't find a specific example for what I am looking for. I have column headers all in row 1. We have multiple templates that all have a header that contains FY21 Impact, FY22 Impact, CY21 Impact, CY22 Impact, OY21 Impact, OY22 Impact and so on. I would like for a code to look in the column headers for the word "Impact" and then change that column Header to "Effective Price"
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi bmkelly
Give this a go, make sure your on the sheet you want to effect, or to run from a different sheet please reference the sheet instead of ActiveSheet
VBA Code:
Sub ChangingStuff()
    For Each cell In ActiveSheet.Range("1:1").Cells
        If InStr(1, cell.Value, "Impact", vbTextCompare) > 0 Then cell.Value = "Effective Price"
    Next cell
End Sub
 
Upvote 0
You can simply just use Find/Replace to do all that at once.

If you want the VBA code for that, it would look like this (no looping required!):
VBA Code:
Sub ReplaceHeaderMacro()
    Rows("1:1").Replace What:="Impact", Replacement:="Effective Price", LookAt _
        :=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
End Sub
 
Upvote 0
You can simply just use Find/Replace to do all that at once.

If you want the VBA code for that, it would look like this (no looping required!):
VBA Code:
Sub ReplaceHeaderMacro()
    Rows("1:1").Replace What:="Impact", Replacement:="Effective Price", LookAt _
        :=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
End Sub
Perfect! I ran it and it did indeed add "Effective Price" as the Header but is there a way to have it remove everything in that header that was previously there and just have it say "Effective Price"? I ran it and for example I have FY22 Impact as the original header, after the code it is now FY22 Effective Price. Sorry I wasn't super clear in my original post!
 
Upvote 0
Just add wildcards around "Impact", i.e.
VBA Code:
Sub ReplaceHeaderMacro()
    Rows("1:1").Replace What:="*Impact*", Replacement:="Effective Price", LookAt _
        :=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
End Sub
 
Upvote 0
Solution
Just add wildcards around "Impact", i.e.
VBA Code:
Sub ReplaceHeaderMacro()
    Rows("1:1").Replace What:="*Impact*", Replacement:="Effective Price", LookAt _
        :=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
End Sub
Thanks, sorry I thought I had those already in ?
 
Upvote 0
No worries!

Glad it works for you!
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,855
Members
449,096
Latest member
Erald

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