Hallo,
kann ich eine Formel mit deren zugehörigen Werten ausgeben?
Als Beispiel:
Geht sowas?
Per VERKETTEN ist das natürlich machbar, aber immer recht viel Aufwand bei langen Formeln.
Gruß
Antwort:
kann ich eine Formel mit deren zugehörigen Werten ausgeben?
Als Beispiel:
Geht sowas?
Per VERKETTEN ist das natürlich machbar, aber immer recht viel Aufwand bei langen Formeln.
Gruß
Antwort:
Visual Basic-Quellcode
- 'by sandy666 (http://www.excelforum.com/showthread.php?t=1128613&p=4326610&viewfull=1#post4326610)
- Const debugMode As Boolean = False
- Public Function LiteralFormula(ByVal rng As Excel.Range, _
- Optional extraParens As Boolean = False) _
- As String
- Const maxAddressLength As Integer = 12
- Dim inQuotes As Boolean
- Dim parseString As String, tempString As String
- Dim addressLength As Long, parsePosition As Long
- Dim rangeLength As Long
- Dim cursor As Long
- Dim returnValue As String
- Dim formulaString As String
- If (rng.HasFormula) Then
- formulaString = VBA.Right$(rng.Formula, _
- VBA.Len(rng.Formula) - 1)
- Else
- Exit Function
- End If
- If debugMode Then
- Debug.Print rng.Address & " has a formula: " & rng.Formula
- End If
- If Not hasReference(rng) Then
- 'no references in range, exiting
- LiteralFormula = formulaString
- If debugMode Then
- Debug.Print "Returning " & formulaString
- End If
- Exit Function
- End If
- For cursor = 1 To VBA.Len(formulaString)
- parseString = VBA.Mid$(formulaString, cursor, maxAddressLength)
- If debugMode Then
- Debug.Print "parsing string: " & parseString
- ' Stop
- End If
- If (VBA.Left$(parseString, 1) = VBA.Chr(34)) Then
- inQuotes = Not inQuotes
- End If
- If Not inQuotes Then
- addressLength = 0
- For parsePosition = 2 To maxAddressLength
- If isAddress(VBA.Left$(parseString, parsePosition)) Then
- addressLength = parsePosition
- Debug.Print "Parse progress: " & VBA.Left$(parseString, parsePosition), _
- "Address length: " & addressLength
- ' Stop
- Else
- Exit For
- End If
- Next parsePosition
- If addressLength > 0 Then
- returnValue = returnValue & rng.Parent.Range(VBA.Left$(parseString, addressLength)).Value
- cursor = cursor + addressLength - 1
- Else
- returnValue = returnValue & VBA.Left$(parseString, 1)
- End If
- Else
- returnValue = returnValue & VBA.Left$(parseString, 1)
- End If
- Next cursor
- LiteralFormula = returnValue
- End Function
- Private Function hasReference(ByVal rng As Range) _
- As Boolean
- If Not rng.HasFormula Then Exit Function
- If isAddress(rng.Formula, False) Then
- hasReference = True
- End If
- End Function
- Private Function isAddress(strTest As String, _
- Optional wholestring As Boolean = True) _
- As Boolean
- Dim re As Object
- Dim strPattern As String
- Set re = CreateObject("VBScript.RegExp")
- If (wholestring) Then strPattern = "^"
- strPattern = strPattern & _
- "[\$]{0,1}[A-Z]{1,3}[\$]{0,1}[1-9][0-9]{0,6}"
- If (wholestring) Then strPattern = strPattern & "$"
- re.Pattern = strPattern
- re.IgnoreCase = True
- isAddress = re.test(strTest)
- End Function
Für ein Mindestmaß an Rechtschreibung, Interpunktion und Majuskeln!
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „bla“ ()