VBA - Loop Through Tabs, Find Header by Name, Replace Values

alexb523

Board Regular
Joined
Dec 12, 2013
Messages
115
i have 4 tabs in a workbook that i would like to loop through, find the header "ClaimName", which resides in different location IF it exists at all, and then replace the values "Claim1Score", "Claim12Score", "Claim3Score", "Claim4Score" with "Claim 1", "Claim 2" ... etc.
I am rusty with vba. I have found similar questions around, but cannot seem to quite get them to work out.
Hope you can help! Thanks!
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
This assumes you have, at most, one header "ClaimName" on any worksheet in your workbook. Note: You used an inconsistent format in your OP - sometimes showing a space between Claim and the first numeral (e.g. Claim 1Score) and sometimes no space (e.g. Claim4Score), but with the end result always showing a space. I assumed here the space is what you actually have.
Code:
Sub ChangeClaim()
Dim Sht As Worksheet, R As Range
For Each Sht In ThisWorkbook.Sheets
    Set R = Sht.Cells.Find("ClaimName", , xlValues, xlWhole)
    If R Is Nothing Then
        GoTo Nx
    Else
        Sht.Range(R, Sht.Cells(Sht.Rows.Count, R.Column).End(xlUp)).Replace "Score", "", xlPart
    End If
Nx:
Next Sht
End Sub
 
Upvote 0
thanks JoeMo. this works great. i actually do not have a space between 'claim' and first numeral. I have added the following to the for loop to get the space (sh2 = Sht for consistency in other parts of code). Please let me know if there is a more elegant solution.

Code:
sh2.Range(R, sh2.Cells(sh2.Rows.Count, R.Column).End(xlUp)).Replace "Claim", "Claim ", xlPart

thanks, alex
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,047
Members
448,940
Latest member
mdusw

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