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:
but
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?
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