I recently put together a UDF function similar to the native FORMULATEXT() function in excel. Instead of displaying the formula with the referenced cell addresses, the UDF displays the formula with the referenced cell values.
So far, it seems to be functioning like I want it to, for now (only works for single column names - "D", NOT "DA", etc. - and rows between 1-999).
However there's something weird going on when working with TRIGONOMETRY functions (sin, cos, radians, etc.). The UDF is working, until it gets to the last cell reference and it seems to be jumping out of the loop and just displays the last cell address in the function. (will post picture)
Does anyone know if there's some property/behavior of Excel's native trig functions that would be causing this?
Thanks!
So far, it seems to be functioning like I want it to, for now (only works for single column names - "D", NOT "DA", etc. - and rows between 1-999).
However there's something weird going on when working with TRIGONOMETRY functions (sin, cos, radians, etc.). The UDF is working, until it gets to the last cell reference and it seems to be jumping out of the loop and just displays the last cell address in the function. (will post picture)
Does anyone know if there's some property/behavior of Excel's native trig functions that would be causing this?
Thanks!
Code:
Public Function AddrToVal(CellRng As Range) As String
'==================================================================
' This UDF is similar to the standard Excel FORMULATEXT() function,
' but it displays the cell values instead of the cell addresses.
' version 001: August 2017
'==================================================================
Dim i As Integer, j As Integer, k As Integer
Dim xChar1 As Integer, xChar2 As Integer
Dim xChar3 As Integer, xChar4 As Integer, xChar5 As Integer
Dim Form As String
Dim CellAddr As String
'Application.Volatile
Form = CellRng.Formula
Form = Replace(Form, "$", "")
For i = 1 To Len(Form)
xChar1 = 0: xChar2 = 0: xChar3 = 0: xChar4 = 0: xChar5 = 0
If i < Len(Form) Then 'IF01==========
xChar1 = Asc(Mid(Form, i, 1))
xChar2 = Asc(Mid(Form, i + 1, 1)): On Error Resume Next
xChar3 = Asc(Mid(Form, i + 2, 1)): On Error Resume Next
xChar4 = Asc(Mid(Form, i + 3, 1)): On Error Resume Next
xChar5 = Asc(Mid(Form, i + 4, 1)): On Error Resume Next
If xChar1 < 65 Or xChar1 > 90 Then 'IF02==========
GoTo GoToHere01
ElseIf 3 = 3 Then
If xChar2 >= 48 And xChar2 <= 57 _
And xChar3 >= 48 And xChar3 <= 57 _
And xChar4 >= 48 And xChar4 <= 57 Then
CellAddr = Chr(xChar1) & Chr(xChar2) & Chr(xChar3) & Chr(xChar4)
Form = Replace(Form, CellAddr, Range(CellAddr).Text)
i = 1
ElseIf 2 = 2 Then
If xChar2 >= 48 And xChar2 <= 57 _
And xChar3 >= 48 And xChar3 <= 57 Then
CellAddr = Chr(xChar1) & Chr(xChar2) & Chr(xChar3)
Form = Replace(Form, CellAddr, Range(CellAddr).Text)
i = 1
ElseIf xChar2 >= 48 And xChar2 <= 57 Then
CellAddr = Chr(xChar1) & Chr(xChar2)
Form = Replace(Form, CellAddr, Range(CellAddr).Text)
i = 1
End If
End If
End If 'IF02==========
End If 'IF01==========
GoToHere01:
'Debug.Print Form
Next i
AddrToVal = Form
End Function