Formel als Text mit Werten ausgeben

  • Excel

    Formel als Text mit Werten ausgeben

    Hallo,

    kann ich eine Formel mit deren zugehörigen Werten ausgeben?

    Als Beispiel:

    Quellcode

    1. A1: 1
    2. A2: 2
    3. A3: 3
    4. B1: =A1*A2*A3 (ergibt natürlich 6)
    5. jetzt möchte ich aber, dass diese Formel als Text mit zugehörigen Werten ausgegeben wird. Das Ergebnis müsste also so aussehen:
    6. B1: 1*2*3 (anstatt A1*A2*A3, das wäre ja über FORMELTEXT einfach zu machen)


    Geht sowas?
    Per VERKETTEN ist das natürlich machbar, aber immer recht viel Aufwand bei langen Formeln.

    Gruß


    Antwort:

    Visual Basic-Quellcode

    1. 'by sandy666 (http://www.excelforum.com/showthread.php?t=1128613&p=4326610&viewfull=1#post4326610)
    2. Const debugMode As Boolean = False
    3. Public Function LiteralFormula(ByVal rng As Excel.Range, _
    4. Optional extraParens As Boolean = False) _
    5. As String
    6. Const maxAddressLength As Integer = 12
    7. Dim inQuotes As Boolean
    8. Dim parseString As String, tempString As String
    9. Dim addressLength As Long, parsePosition As Long
    10. Dim rangeLength As Long
    11. Dim cursor As Long
    12. Dim returnValue As String
    13. Dim formulaString As String
    14. If (rng.HasFormula) Then
    15. formulaString = VBA.Right$(rng.Formula, _
    16. VBA.Len(rng.Formula) - 1)
    17. Else
    18. Exit Function
    19. End If
    20. If debugMode Then
    21. Debug.Print rng.Address & " has a formula: " & rng.Formula
    22. End If
    23. If Not hasReference(rng) Then
    24. 'no references in range, exiting
    25. LiteralFormula = formulaString
    26. If debugMode Then
    27. Debug.Print "Returning " & formulaString
    28. End If
    29. Exit Function
    30. End If
    31. For cursor = 1 To VBA.Len(formulaString)
    32. parseString = VBA.Mid$(formulaString, cursor, maxAddressLength)
    33. If debugMode Then
    34. Debug.Print "parsing string: " & parseString
    35. ' Stop
    36. End If
    37. If (VBA.Left$(parseString, 1) = VBA.Chr(34)) Then
    38. inQuotes = Not inQuotes
    39. End If
    40. If Not inQuotes Then
    41. addressLength = 0
    42. For parsePosition = 2 To maxAddressLength
    43. If isAddress(VBA.Left$(parseString, parsePosition)) Then
    44. addressLength = parsePosition
    45. Debug.Print "Parse progress: " & VBA.Left$(parseString, parsePosition), _
    46. "Address length: " & addressLength
    47. ' Stop
    48. Else
    49. Exit For
    50. End If
    51. Next parsePosition
    52. If addressLength > 0 Then
    53. returnValue = returnValue & rng.Parent.Range(VBA.Left$(parseString, addressLength)).Value
    54. cursor = cursor + addressLength - 1
    55. Else
    56. returnValue = returnValue & VBA.Left$(parseString, 1)
    57. End If
    58. Else
    59. returnValue = returnValue & VBA.Left$(parseString, 1)
    60. End If
    61. Next cursor
    62. LiteralFormula = returnValue
    63. End Function
    64. Private Function hasReference(ByVal rng As Range) _
    65. As Boolean
    66. If Not rng.HasFormula Then Exit Function
    67. If isAddress(rng.Formula, False) Then
    68. hasReference = True
    69. End If
    70. End Function
    71. Private Function isAddress(strTest As String, _
    72. Optional wholestring As Boolean = True) _
    73. As Boolean
    74. Dim re As Object
    75. Dim strPattern As String
    76. Set re = CreateObject("VBScript.RegExp")
    77. If (wholestring) Then strPattern = "^"
    78. strPattern = strPattern & _
    79. "[\$]{0,1}[A-Z]{1,3}[\$]{0,1}[1-9][0-9]{0,6}"
    80. If (wholestring) Then strPattern = strPattern & "$"
    81. re.Pattern = strPattern
    82. re.IgnoreCase = True
    83. isAddress = re.test(strTest)
    84. End Function

    Für ein Mindestmaß an Rechtschreibung, Interpunktion und Majuskeln!

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „bla“ ()