Determine if a formula contains absolute, relative or mixed references

ttt123

Board Regular
Joined
May 31, 2006
Messages
120
Office Version
  1. 365
Platform
  1. Windows
I would like to make some functions to determine the different reference types in a formula. I have code that functions correctly if the formula only has a single reference type, but not if it has more than one reference type.

So for example:

VBA Code:
HasAbsoluteReference("=$A$1*$B$1") 'correctly returns True

but

VBA Code:
HasAbsoluteReference("=$A$1*B1") 'incorrectly returns False since the formula has both an absolute reference and a relative reference


Does anyone have any suggestions for modifying these formulas so that we can check each reference type in the formula individually instead of the entire formula as a whole?

VBA Code:
Public Function HasAbsoluteReference(formulaA1 As String) As Boolean
    Dim convertedFormula As String
    convertedFormula = Application.ConvertFormula(formula:=formulaA1, FromReferenceStyle:=xlA1, ToReferenceStyle:=xlA1, ToAbsolute:=xlAbsolute)
    HasAbsoluteReference = StrComp(convertedFormula, formulaA1, vbTextCompare) = 0
End Function

Public Function HasRelativeReference(formulaA1 As String) As Boolean
    Dim convertedFormula As String
    convertedFormula = Application.ConvertFormula(formula:=formulaA1, FromReferenceStyle:=xlA1, ToReferenceStyle:=xlA1, ToAbsolute:=xlRelative)
    HasRelativeReference = StrComp(convertedFormula, formulaA1, vbTextCompare) = 0
End Function

Public Function HasMixedReference(formulaA1 As String) As Boolean
    Dim convertedFormula1 As String, convertedFormula2 As String
    convertedFormula1 = Application.ConvertFormula(formula:=formulaA1, FromReferenceStyle:=xlA1, ToReferenceStyle:=xlA1, ToAbsolute:=xlRelRowAbsColumn)
    convertedFormula2 = Application.ConvertFormula(formula:=formulaA1, FromReferenceStyle:=xlA1, ToReferenceStyle:=xlA1, ToAbsolute:=xlAbsRowRelColumn)
    HasMixedReference = StrComp(convertedFormula1, formulaA1, vbTextCompare) = 0 Or StrComp(convertedFormula2, formulaA1, vbTextCompare) = 0
End Function
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try this.

Book1
ABCDE
115Absolute6
215Mixed9
315Relative
Sheet2
Cell Formulas
RangeFormula
A1A1=$E$1+$E$2
B1:B3B1=ref(A1)
A2A2=$E$1+E2
A3A3=E1+E2


VBA Code:
Function REF(r As Range)
Dim FX As String:       FX = r.Formula
Dim xAny As Boolean:    xAny = False
Dim xAll As Boolean:    xAll = True

With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "[$?A-Z]+\d+"
    Set matches = .Execute(FX)
    For Each m In matches
        If InStr(m.Value, "$") Then
            xAny = True
        Else
            xAll = False
        End If
    Next m
End With

If xAny And xAll Then REF = "Absolute"
If xAny And Not (xAll) Then REF = "Mixed"
If Not xAny And Not xAll Then REF = "Relative"
    
End Function
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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