Benutzer:Gerold Broser/ZeichenZählen

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
'Note: Put this in a NEW module, not in the workbook or table modules!

Option Explicit
'

' ZeichenZaehlen()
' ================

' 2009-06-24 von Geri Broser

' Zählt die Anzahl der Vorkommen von 'zähleDies' in 'Text'.
' Groß-/Kleinbuchstaben werden standardmäßig unterschieden, also nicht zusammen gezählt.

Function ZeichenZaehlen(Text As String, zähleDies As String, Optional großKleinUnterschiedlich As Boolean = True) As Long

  ZeichenZaehlen = CountCharacters(Text, zähleDies, großKleinUnterschiedlich)

End Function
'

' CountCharacters()
' =================

' 2009-06-24 by Geri Broser

' Counts the number of occurrences of 'countThis' in 'Text'.
' Default is case sensitivity, i.e. lower and upper case letters are not counted together.

Function CountCharacters(Text As String, countThis As String, Optional caseSensitive As Boolean = True) As Long

  Dim position As Long
  
  For position = 1 To Len(Text)

    If caseSensitive Then
      If Mid$(Text, position, Len(countThis)) = countThis Then _
        CountCharacters = CountCharacters + 1
    Else
      If UCase$(Mid$(Text, position, Len(countThis))) = UCase$(countThis) Then _
        CountCharacters = CountCharacters + 1
    End If
    
  Next position
  
End Function