Wednesday, May 25, 2016

Utilities: Documenting spreadsheet formulas

All the elements presented are not warranted to be correct or free from defects.
Please report any errors found to  afstblogs@gmail.com


Function xlGetFormula( _
    cell As Range, _
    Optional tp As Integer = 1, _
    Optional shw As Boolean = True _
) As Variant

  Dim _
  st(1 To 2) As String, _
  sst As String
 
  st(1) = cell.Address(0, 0)

  If cell.HasArray = False Then
    st(2) = cell.Formula
  Else
    st(2) = "{" & cell.Formula & "}"
  End If

    If shw = False Then
        xlGetFormula = ""
    ElseIf tp = 1 Then
        sst = st(2)
        xlGetFormula = sst
    ElseIf tp = 2 Then
        sst = st(1) & ": " & st(2)
        xlGetFormula = sst
    ElseIf tp = 3 Then
        xlGetFormula = st
    ElseIf tp <> 1 And tp <> 2 And tp <> 3 Then
        xlGetFormula = "ERR_type"
    End If
 
End Function




NOTES: 
  1. The FORMULATEXT is a spreadsheet function that cannot be used in VBA and supports returns the name of the function in the idiom active in the spreadsheet.
  2. The xlGetFormula function always returns the function name in English 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.