Conosco molto poco la programmazione e la mia risposta non è elegante. Come futuro ingegnere avevo bisogno di una funzione in Excel che potesse mostrarmi la formula. Il problema che ho riscontrato nella soluzione Indrek è che la sua funzione mi restituisce una formula come questa: "C9 * C9 * pi ()" e volevo che i nomi delle celle nella visualizzazione della formula (C9) fossero cambiati con i valori di quelle cellule; e questo cambierebbe automaticamente ogni volta che quei numeri vengono cambiati nella cella C9 originale.
Il mio codice funziona con formule molto semplici. Non ho mai trovato qualcosa del genere da nessun'altra parte, potrebbe aiutare qualcuno o no. Qualcuno potrebbe essere in grado di migliorare questo a un livello decente. Come ho detto, funziona per me e sono un ragazzo pratico con poche abilità nella programmazione.
La prima funzione non è affatto mia. È stato recuperato da:
/programming/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