I know verry little about programming and my answer is not elegant. As a future engineer I needed a function in Excel that could display to me the formula. The problem I found in Indrek solution is that his function returns to me a formula like this: "C9*C9*pi()" and I wanted the cell's names in the display of the formula (C9) to be changed to the values of those cells; and that would automatically change whenever those numbers are changed in the original C9 cell.
My code sort of works with very basic formulas. I never found something like this anywhere else, It might help someone or not. Someone might be able to improve this to a decent level. As I said, it works for me and I'm a practical guy with little skills in programming.
The first function isn´t mine at all. It was retrieved from:
https://stackoverflow.com/questions/10951687/how-to-search-for-string-in-an-array
Function IsInArray(ar, item$) As Boolean
Dim delimiter$, list$
' Chr(7) is the ASCII 'Bell' Character.
' It was chosen for being unlikely to be found in a normal array.
delimiter = Chr(7)
' Create a list string containing all the items in the array separated by the delimiter.
list = delimiter & Join(ar, delimiter) & delimiter
IsInArray = InStr(list, delimiter & item & delimiter) > 0
End Function
Function GETFORMULA(cell)
Dim alfabeto As String
Dim alfabetos() As String
Dim formula As String
Dim celda As String
Dim cutter As String
Dim cutterarray() As String
'The formula is taken from the cell
formula = cell.formula
'And alphabet is declared to be compared with the formula
alfabeto = "A,B,C,D,E,F,G,H,I,J,K,L,M,O,P,Q,R,S,T,U,V,W,X,Y,Z"
'An array is declared with the elements of the alphabet string
alfabetos = Split(alfabeto, ",")
'Replacing common functions with a symbol, otherwise the code will crash
formula = Replace(formula, "LOG", "#")
formula = Replace(formula, "SQRT", "?")
formula = Replace(formula, "PI", "ñ")
'MsgBox (formula)
Debug.Print formula
'Symbols to identify where the name of a cell might end are declared
cutter = "*,/,+,-,^,(,)"
'An array is declared with the symbols from the string that has been just declared
cutterarray = Split(cutter, ",")
Dim nuevaformula As String
'For the i position in the lenght of the formula
Dim index As Integer
For i = 1 To Len(formula)
Debug.Print "Para i iterativo=", i
Debug.Print "Se tiene el digito", Mid(formula, i, 1)
'if the i element is not a letter, then add it to the chain of characters
If IsInArray(alfabetos, Mid(formula, i, 1)) = False Then
Debug.Print "Que es un numero"
nuevaformula = nuevaformula + Mid(formula, i, 1)
Debug.Print nuevaformula
'if the element is a letter, then it´s necessary to get the name of the cell, found all the numbers
'of the cell until a symbol to cut appears, like a: "*","/","+","-"
Else
Debug.Print "Encontramos una letra"
'Empezamos a buscar más números
'Numbers in the cell name are going to be find to isolate the cell number
For s = 2 To 7
celda = Mid(formula, i, s)
Debug.Print "Incrementamos una posición quedando", celda
If (i + s - 1 = Len(formula)) = False Then
'if a symbol to cut is not found in the last increment the last number hasn´t been reached
'and it´s necessary to keep loking for it
If IsInArray(cutterarray, Right(celda, 1)) = False Then
celda = Mid(formula, i, s)
Debug.Print "En el incremento no se encontró cutter. La i correcta es", i + s - 1
Else
'if it´s found the name of the cell is
celda = Mid(formula, i, s - 1)
Debug.Print "Se encontró un cutter la celda es", celda
'the search cycle has to be broken
Debug.Print s, i
i = i + s - 2
Debug.Print "Daremos salto a i=", i
Debug.Print celda
nuevaformula = nuevaformula + CStr(Range(celda).Value)
Exit For
End If
Else
Debug.Print "se alcanzó la máxima cantidad de iteraciones posibles"
celda = Mid(formula, i, s)
Debug.Print "La celda encontrada fue", celda
nuevaformula = nuevaformula + CStr(Range(celda).Value)
Debug.Print nuevaformula
nuevaformula = Replace(nuevaformula, "#", "LOG10")
nuevaformula = Replace(nuevaformula, "?", "raiz")
nuevaformula = Replace(nuevaformula, "ñ", "pi")
ISAAC = nuevaformula
Debug.Print (nuevaformula)
Exit Function
End If
'MsgBox (nuevaformula)
Next s
End If
Next i
nuevaformula = Replace(nuevaformula, "#", "LOG10")
nuevaformula = Replace(nuevaformula, "?", "raiz")
nuevaformula = Replace(nuevaformula, "ñ", "pi")
GETFORMULA = nuevaformula
Debug.Print (nuevaformula)
End Function