我对编程知之甚少,我的回答也不是很优雅。作为未来的工程师,我需要Excel中的一个可以向我显示公式的函数。我在Indrek解决方案中发现的问题是,他的函数向我返回了这样的公式:“ C9 * C9 * pi()”,我希望将公式(C9)的显示中的单元格名称更改为的值。这些细胞;并且只要在原始C9单元中更改了这些数字,它就会自动更改。
我的代码具有非常基本的公式。我从没有在其他地方找到过这样的东西,它可能对某人有帮助。也许有人可以将其提高到一个不错的水平。就像我说的那样,它对我有用,而且我是一个编程技巧很少的实用人。
第一个功能根本不是我的。它是从以下位置检索到的:
/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