The_Accountant
New Member
- Joined
- Sep 23, 2021
- Messages
- 5
- Office Version
- 2019
- Platform
- Windows
I have a workbook with 25-30 worksheets and every single cell that would have a cell reference uses a named range instead. I am trying to replace them all with cell references using the following macro (found on google):
Sub AbsoleteNamesWithRelativeRefs()
Dim Rng As Range
Dim WorkRng As Range
Dim xName As Name
On Error Resume Next
xTitleId = "Input_Range"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Set WorkRng = WorkRng.SpecialCells(xlCellTypeFormulas)
For Each Rng In WorkRng
For Each xName In ThisWorkbook.Names
If InStr(Rng.Formula, xName.Name) > 0 Then
Rng.Formula = VBA.Replace(Rng.Formula, xName.Name, VBA.Replace(VBA.Replace(xName.RefersTo, "=", ""), "$", ""))
End If
Next
Next
End Sub
The named ranges are based on dates - some examples are:
=_12stdjan1
=_12stdjan14
=_12stdjan15
For any ranges that have a two digit day (e.g. 2nd and 3rd example above), the macro is only recognizing the name through the tens place and leaving the final digit at the end of the resulting cell reference. So it pulls the cell reference for the Jan 1 date and makes the resulting cell reference have an extra digit.
For example, if the name "_12stdjan14" refers to cell $A$15 and the name "_12stdjan1" refers to cell $B$15, running the macro for "_12stdjan14" would result in a cell reference of $B$154.
Is there any way to fix this issue? I am unfortunately very inexperienced with VBA if that isn't already apparent.
Sub AbsoleteNamesWithRelativeRefs()
Dim Rng As Range
Dim WorkRng As Range
Dim xName As Name
On Error Resume Next
xTitleId = "Input_Range"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Set WorkRng = WorkRng.SpecialCells(xlCellTypeFormulas)
For Each Rng In WorkRng
For Each xName In ThisWorkbook.Names
If InStr(Rng.Formula, xName.Name) > 0 Then
Rng.Formula = VBA.Replace(Rng.Formula, xName.Name, VBA.Replace(VBA.Replace(xName.RefersTo, "=", ""), "$", ""))
End If
Next
Next
End Sub
The named ranges are based on dates - some examples are:
=_12stdjan1
=_12stdjan14
=_12stdjan15
For any ranges that have a two digit day (e.g. 2nd and 3rd example above), the macro is only recognizing the name through the tens place and leaving the final digit at the end of the resulting cell reference. So it pulls the cell reference for the Jan 1 date and makes the resulting cell reference have an extra digit.
For example, if the name "_12stdjan14" refers to cell $A$15 and the name "_12stdjan1" refers to cell $B$15, running the macro for "_12stdjan14" would result in a cell reference of $B$154.
Is there any way to fix this issue? I am unfortunately very inexperienced with VBA if that isn't already apparent.