Alant
  1. Aktuelle Seite:  
  2. Startseite
  3. Tips und Tricks
  4. MS Office
  5. Excel

Eigene Excel-Funktion (Makros) allgemein zur Verfügung stellen

Soll eine selbst erstellte Funktion in allen Tabellen zur Verfügung stehen ist das recht einfach mittels eines AddIns zu verwirklichen.

Ein paar Schritte müssen dafür getan werden

1. Excel-Datei mit Makros erstellen mit der/den Funktion(en)
    Der Dateiname ist frei wählbar. In diesem Bsp. wurde Funktionensammlung.xlsm gewählt
    Die Excel-Datei enthält 2 Funktionen. Es werden durch eine zusätliche Funktion beim Start die beiden Funktionen registriert.
    Dies hat zur Folge dass die Funktionen eine eigene Kategorie bekommen und die einzelnen Felder einen Hinweistext bekommen.
    Folgende Funktionen werden zur Verfügung gestellt
    -  RegEx-Funktion aus dem VBA-Bereich in den normalen Excel-Funktionen zur Verfügung stellen.
    -  Datum in englischer Schreibweise darstellen.  

2. Excel-Datei als .xla in den Ordner C:\Users\[USER]\AppData\Roaming\Microsoft\AddIns speichern

3. Datei > Optionen => Menüband anpassen
    Häckchen bei Entwicklertools setzen und mit OK abschließen

4. In Excel auf das Register Entwicklertool wechseln und Add-Ins anklicken. In dem Dialog-Fenster Funktionensammlung aktivieren und mit OK abschließen.    addin aktivieren

Ab jetzt stehen in jeder Excel Datei die beiden Funktionen zur Verfügung.

 

eigene funktionen

  function regex 

 

Download: Funktionensammlung.xlsm


Der VBA-Code der in ein Modul gespeichert werden muss.

Option Explicit
'Funtion liefert die Treffer zurück
' https://www.vba-tutorial.de/applikation/regexp.htm


' Es muss ein Verweis auf folgende Bibliothek vorhanden sein "Microsoft VBScript Regular Expression 5.5"
' Aufrufen via VBA-Menü -> Extras -> Verweise.., nach unten scrollen und anklicken
' c:\Windows\ysWOW64\vbscript.dll

' Text       -> Der Text der durchsucht werden muss
' Suchmuster -> Ein Regex-Ausdruck als Suchmuster
' (|) ODER Ausdruck
' {x,y} Länge des Suchmusters minimum, maximum
' [0-9] Zahl von 0 bis 9
' [a-zA-Z] Buchstaben a bis z, Groß und Kleinschreibung

' Email RegEx-Pattern als voreingestelltes Suchmuster
Const cDefautlPattern = "([a-z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})"

Const cCategory = "AddIns-Funktionen"

Const cMacroName1 = "RegEx"
Const cMacroName2 = "ConvertDate"

Function RegEx(Txt$, Optional ByVal SearchPattern$ = cDefautlPattern, Optional All = False, Optional MatchSeparator$ = "|", Optional IgnoreCase = True, Optional Multiline = False)
  Dim RegularExpression As New RegExp
  Dim TrefferSammlung As MatchCollection, Treffer As Match
  Dim sTemp$
  
  With RegularExpression
    .Global = All
    .Multiline = Multiline
    .Pattern = SearchPattern
    .IgnoreCase = IgnoreCase
    Set TrefferSammlung = .Execute(Txt)
  End With
    
  For Each Treffer In TrefferSammlung
    'Debug.Print Treffer
    sTemp = sTemp & IIf(sTemp = vbNullString, vbNullString, MatchSeparator) & Treffer
  Next
  RegEx = sTemp
  
  Set TrefferSammlung = Nothing
  Set RegularExpression = Nothing
End Function

Function ConvertDate(Datum, Optional shortMonth As Boolean) As String
  Dim arr, arrMonth
  If Datum <> vbNullString Then
    arr = Split(Datum, ".")
    arr(0) = CInt(arr(0))
    Select Case arr(0)
        Case 1, 21, 31
          arr(0) = arr(0) & "st"
        Case 2, 22
          arr(0) = arr(0) & "nd"
        Case 3, 23
          arr(0) = arr(0) & "rd"
        Case Else
          arr(0) = arr(0) & "th"
    End Select
    ' Monat
    If shortMonth Then
      arrMonth = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    Else
      arrMonth = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
    End If
    arr(1) = arrMonth(arr(1) - 1) & ", "
    ' Zurück packen
    ConvertDate = Join(arr, " ")
  Else
    ConvertDate = Datum
  End If
End Function


' Wird beim öffnen dieser Arbeitsmappe geladen
Public Sub RegisterMyFunction()
  Application.MacroOptions _
    Macro:=cMacroName1, _
    Description:="Per Suchmuster Textteile auslesen", _
    Category:=cCategory, _
    ArgumentDescriptions:=Array( _
      "Feld welches durchsucht werden soll", _
      "Suchmuster (default): " & cDefautlPattern, _
      "Alle Treffer anzeigen (default = FALSCH)", _
      "Trennzeichen bei mehreren Treffern (default = |)", _
      "Groß-/Kleinschreibung ignorieren (default = WAHR)", _
      "Text ist mehrzeilig (default = FALSCH)")
            
  Application.MacroOptions _
    Macro:=cMacroName2, _
    Description:="Datum als Text in englisher Schreibweise", _
    Category:=cCategory, _
    ArgumentDescriptions:=Array( _
      "Feld mit dem Datum", _
      "Monat Kurzschreibweise  (default = FALSCH)")
End Sub

 

Damit die Funktionen korrekt dargestellt werden muss in DieseArbeitsmappe noch die Funktion RegisterMyFunction eingetragen werden damit diese beim Start aufgerufen werden.

Private Sub Workbook_Open()
  RegisterMyFunction
End Sub

 

Erstes, x-tes Vorkommen eines Zeichens/Zeichenkette in einem Text in Excel

<p>Excel bietet zwei Funktion das erste Vorkommen eines Zeichens zu finden.<br />mit der Funktion <strong>Finden</strong> wird die Groß- und Kleinschreibung unterschieden mit der Funktion <strong>Suchen</strong> nicht.<br />Wenn jetzt aber das 2., 3. etc. Vorkommen des Zeichens gesucht wird es deutlich schwieriger wenn es Variable gehalten werden soll.<br />Das Prinzip wird im Artikel "<a href="/index.php/tips-und-tricks.html?amp;view=article&amp;id=321:letztes-vorkommen-eines-zeichens-zeichenkette-in-einem-text-in-excel&amp;catid=74:excel_tt">Letztes Vorkommen eines Zeichens im Text</a>" erklärt. Im Feld A1 ist der zu durchsuchende Text. In Feld A2 die zu suchende Zeichenfolge. In A3 steht wie weit in der Trefferliste gesprungen werden soll. Leer entspricht 0. Wird ein Wert größer des maximalen Treffers eingetragen so wird der letzte Treffer genommen.</p>
<p><span style="color: #ff0000;">A1</span> = "Gut<span style="background-color: #ffff99;">en</span> Morg<span style="background-color: #ffff99;">en</span> liebe Sorg<span style="background-color: #ffff99;">en</span>"<br /><span style="color: #0000ff;">A2</span> = "<span style="background-color: #ffff99;">en</span>"<br /><span style="color: #33cccc;">A3</span> = 2</p>
<div style="background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #ce5c00; font-weight: bold;">11 </span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #ce5c00;">=</span></span></span>WENNFEHLER(FINDEN("|";WECHSELN(KLEIN(<strong><span style="color: #0000ff;">A1</span></strong>);KLEIN(<span style="color: #000000;"><span style="color: #000000;"><span style="color: #ff0000;">A2</span></span></span><span style="color: #ce5c00;"></span>);"|";WENN(WENN(<span style="color: #000000;"><span style="color: #33cccc;">A3</span></span>=0;1;<span style="color: #000000;"><span style="color: #33cccc;">A3</span></span>)&gt;(LÄNGE(<strong><span style="color: #0000ff;">A1</span></strong>)-LÄNGE(WECHSELN(KLEIN(<strong><span style="color: #0000ff;">A1</span></strong>);KLEIN(<span style="color: #000000;"><span style="color: #000000;"><span style="color: #ff0000;">A2</span></span></span>);"")))/LÄNGE(<span style="color: #000000;"><span style="color: #000000;"><span style="color: #ff0000;">A2</span></span></span>);(LÄNGE(<span style="color: #0000ff;">A1</span>)-LÄNGE(WECHSELN(KLEIN(<strong><span style="color: #0000ff;">A1</span></strong><span style="color: #000000; font-weight: bold;"></span>);KLEIN(<span style="color: #000000;"><span style="color: #000000;"><span style="color: #ff0000;">A</span></span></span><span style="color: #ce5c00;">2</span>);"")))/LÄNGE(<span style="color: #000000;"><span style="color: #000000;"><span style="color: #ff0000;">A2</span></span></span>);WENN(<span style="color: #000000;"><span style="color: #33cccc;">A3</span></span><span style="color: #ce5c00;"></span>=0;1;<span style="color: #000000;"><span style="color: #33cccc;">A3</span></span><span style="color: #ce5c00;"></span>))));0)
</pre>
</div>
<p>&nbsp;</p>

Letztes Vorkommen eines Zeichens/Zeichenkette in einem Text in Excel

<p>Leider bietet Excel keine Funktion um die letzte Position eines Zeichens/Zeichenkette zu ermitteln. Als Lösung gibt es 2 Möglichkeiten</p>
<p>Dieser Text steht in der Zelle <strong><span style="color: #0000ff;">A1</span></strong> -&gt;<strong>"Guten Morgen liebe Sorgen"</strong>. <br />in <strong><span style="color: #ff0000;">A2</span></strong> steht die gesuchte Zeichenkette z.B. "<strong>en</strong>"</p>
<hr />
<h3>Mit internen Funktionen:</h3>
<p>Hierfür werden 4 Funktionen benötigt. Es geht darum das letzte Vorkommen des gesuchten Zeichens/Zeichenfolge durch ein neues Zeichen zu Ersetzen.<br />Dieses Ersatzzeichen darf nicht im Text vorkommen! Dann wird nach diesem "neuen" Zeichen gesucht.<span style="color: #ffcc00;"><strong> <br /></strong></span></p>
<div style="background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #ce5c00; font-weight: bold;">23 </span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #ce5c00;">=</span></span></span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #000000;">WENNFEHLER(FINDEN</span></span></span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #000000;">(</span><span style="color: #000000;"><span style="color: #4e9a06;">"|"</span><span style="color: #000000;"></span>;</span><span style="color: #4e9a06;"><span style="color: #000000;"><span style="color: #000000;">WECHSELN</span><span style="color: #000000;">(KLEIN(</span><span style="color: #0000ff;">A1</span><span style="color: #000000;">);<span style="color: #000000;">KLEIN(</span><span style="color: #ff0000;">A2</span></span><span style="color: #4e9a06;"><span style="color: #000000;">);</span><span style="color: #4e9a06;">"|"</span><span style="color: #000000;">;</span><span style="color: #000000;">(LÄNGE(</span><span style="color: #000000;"><span style="color: #0000ff;">A1</span>)-LÄNGE(WECHSELN(<span style="color: #000000;">KLEIN(</span><span style="color: #ce5c00;"></span><span style="color: #0000ff;">A1</span><span style="color: #000000;">);<span style="color: #000000;"><span style="color: #000000;"><span style="color: #000000;">KLEIN(</span><span style="color: #ff0000;">A2</span></span><span style="color: #4e9a06;"><span style="color: #000000;">)</span></span></span></span><span style="color: #4e9a06;"></span><span style="color: #000000;">;</span><span style="color: #4e9a06;">""</span></span><span style="color: #000000;">))</span></span><span style="color: #000000;">)/LÄNGE(<span style="color: #ff0000;">A2</span><span style="color: #ce5c00;"></span>)));<span style="color: #ffcc00;"><strong>0</strong></span>)</span></span></span></span></span><span style="color: #000000;"></span>
</pre>
</div>
<p><br />Weiter unten gibt es eine <a href="#ff">Erweiterung </a>der Funktion um den Treffer auf das vorletzte, drittletzte etc. Vorkommen des Zeichens/-folge zu setzten</p>
<hr />
<p>Die Funktionsweise wird in mehreren Schritten erklärt.<br />Als Basis zur Erläuterung wird nach einem LEERZEICHEN " " gesucht. Später ab 4. können auch beliebige Zeichen gesucht werden.</p>
<p>1.) Anzahl des gesuchten Zeichens im Text zählen. Hierfür die Original-Textlänge und die Textlänge ohne das gesuchte Zeichen von einander abgezogen</p>
<p>Mit der Funktion <strong>Wechseln</strong> kann das gesuchte Zeichen aus der Zeichenkette gelöscht werden. die Funktion LÄNGE liefert die Anzahl an Zeichen des Textes.<br /><!-- HTML generated using hilite.me --></p>
<div style="background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #ce5c00; font-weight: bold;">GutenMorgenliebeSorgen </span><span style="color: #ce5c00;">=</span><span style="color: #000000;">WECHSELN</span><span style="color: #000000; font-weight: bold;">(</span><strong><span style="color: #0000ff;">A1</span></strong><span style="color: #000000; font-weight: bold;">;</span><span style="color: #4e9a06;">" "</span><span style="color: #000000; font-weight: bold;">;</span><span style="color: #4e9a06;">""</span><span style="color: #000000; font-weight: bold;">)</span>
</pre>
</div>
<p><br />Wird jetzt die Länge des gekürzten Textes vom Original-Textes abgezogen ergibt das die Anzahl der gesuchten Zeichen</p>
<p><!-- HTML generated using hilite.me --></p>
<div style="background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre><span style="color: #ce5c00; font-weight: bold;">3</span> <span style="color: #ce5c00;">=</span>LÄNGE("<span style="color: #ce5c00; font-weight: bold;">Guten Morgen liebe Sorgen</span>")-LÄNGE(WECHSELN("<span style="color: #0000ff;"><span style="color: #ce5c00; font-weight: bold;">Guten Morgen liebe Sorgen<span style="color: #000000; font-weight: bold;">";</span></span></span><span style="color: #000000; font-weight: bold;"></span><span style="color: #4e9a06;">" "</span><span style="color: #000000; font-weight: bold;">;</span><span style="color: #4e9a06;">""</span>))</pre>
<pre style="margin: 0; line-height: 125%;"><span style="color: #ce5c00; font-weight: bold;">3</span> <span style="color: #ce5c00;">=</span>LÄNGE(<span style="color: #0000ff;">A1</span>)-LÄNGE(WECHSELN(<span style="color: #0000ff;">A1</span><span style="color: #000000; font-weight: bold;">;</span><span style="color: #4e9a06;">" "</span><span style="color: #000000; font-weight: bold;">;</span><span style="color: #4e9a06;">""</span>))
</pre>
</div>
<p>&nbsp;</p>
<p>2.) Das letzte Vorkommen des gesuchten Zeichens duch ein neues ersetzen. Hier im Bsp. wird die Pipe<strong> |</strong> verwendet. Kann aber mit jedem Zeichen das nicht im Text vorkommt erledigt werden.<br />Die Funktion <strong>Wechseln</strong> bietet als 4. Parameter die Option beim wievielten Vorkommen des Zeichens dieses durch ein neues ersetzt wird.</p>
<div style="background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #ce5c00; font-weight: bold;">Guten Morgen liebe|Sorgen </span><span style="color: #ce5c00;">=</span><span style="color: #000000;">WECHSELN</span><span style="color: #000000; font-weight: bold;">(</span><strong><span style="color: #0000ff;">A1</span></strong><span style="color: #000000; font-weight: bold;">;</span><span style="color: #4e9a06;">" "</span><span style="color: #4e9a06;"><span style="color: #000000; font-weight: bold;">;</span><span style="color: #4e9a06;">"|"</span><span style="color: #000000; font-weight: bold;">;3</span><span style="color: #4e9a06;"></span></span><span style="color: #000000; font-weight: bold;">)<br /></span></pre>
<pre><span style="color: #ce5c00; font-weight: bold;">Guten Morgen liebe|Sorgen </span><span style="color: #ce5c00;">=</span><span style="color: #000000;">WECHSELN</span><span style="color: #000000; font-weight: bold;">(</span><strong><span style="color: #0000ff;">A1</span></strong><span style="color: #000000; font-weight: bold;">;</span><span style="color: #4e9a06;">" "</span><span style="color: #4e9a06;"><span style="color: #000000; font-weight: bold;">;</span><span style="color: #4e9a06;">"|"</span><span style="color: #000000; font-weight: bold;">;</span><span style="color: #000000;">LÄNGE(</span><span style="color: #000000;"><span style="color: #0000ff;">A1</span>)-LÄNGE(WECHSELN(<span style="color: #0000ff;">A1</span><span style="color: #000000;">;</span><span style="color: #4e9a06;">" "</span><span style="color: #000000;">;</span><span style="color: #4e9a06;">""</span></span><span style="color: #000000;">))</span><span style="color: #4e9a06;"></span></span><span style="color: #000000; font-weight: bold;">)</span></pre>
</div>
<p>&nbsp;<br />3.) Jetzt ist es ein leichtes nach dem neuen Zeichen "|" mit der Funktion <strong>FINDEN</strong> zu suchen.</p>
<div style="background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #ce5c00; font-weight: bold;">19 </span><span style="color: #000000;"><span style="color: #ce5c00;">=</span><span style="color: #000000;">FINDEN</span><span style="color: #000000;">(</span><span style="color: #000000;"></span><span style="color: #4e9a06;">"|"</span><span style="color: #4e9a06;"><span style="color: #000000;">;</span><span style="color: #4e9a06;">"Guten Morgen liebe|Sorgen"</span><span style="color: #4e9a06;"></span></span><span style="color: #000000;">)</span></span></pre>
<pre><span style="color: #ce5c00; font-weight: bold;">19 </span><span style="color: #ce5c00;"><span style="color: #000000; font-weight: bold;"><span style="color: #ce5c00; font-weight: bold;">=</span></span></span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #000000;">FINDEN</span></span></span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #000000;">(</span><span style="color: #000000;"><span style="color: #4e9a06;">"|"</span><span style="color: #000000;"></span>;</span><span style="color: #4e9a06;"><span style="color: #000000;"><span style="color: #000000;">WECHSELN</span><span style="color: #000000;">(</span><span style="color: #0000ff;">A1</span><span style="color: #000000;">;</span><span style="color: #4e9a06;">" "</span><span style="color: #4e9a06;"><span style="color: #000000;">;</span><span style="color: #4e9a06;">"|"</span><span style="color: #000000;">;</span><span style="color: #000000;">LÄNGE(</span><span style="color: #000000;"><span style="color: #0000ff;">A1</span>)-LÄNGE(WECHSELN(<span style="color: #0000ff;">A1</span><span style="color: #000000;">;</span><span style="color: #4e9a06;">" "</span><span style="color: #000000;">;</span><span style="color: #4e9a06;">""</span></span><span style="color: #000000;">))</span></span><span style="color: #000000;">)</span></span></span><span style="color: #000000;">)</span></span></span><span style="color: #000000;"></span></pre>
</div>
<p>&nbsp;</p>
<p>4.) Wird jetzt nach einem Buchstaben gesucht tritt das Problem der Groß- und Kleinschreibung auf. Mit der Funktion <strong>KLEIN</strong> kann das behoben werden.<br />Zusätzlich wird jetzt noch das Suchzeichen aus der Zelle <strong><span style="color: #ff0000;">A2</span></strong> verwendet. In A2 steht hier im Bsp "M"</p>
<div style="background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #ce5c00; font-weight: bold;">20 </span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #ce5c00;">=</span></span></span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #000000;">FINDEN</span></span></span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #000000;">(</span><span style="color: #000000;"><span style="color: #4e9a06;">"|"</span><span style="color: #000000;"></span>;</span><span style="color: #4e9a06;"><span style="color: #000000;"><span style="color: #000000;">WECHSELN</span><span style="color: #000000;">(KLEIN(</span><span style="color: #0000ff;">A1</span><span style="color: #000000;">);<span style="color: #000000;">KLEIN(</span><span style="color: #ce5c00;"></span><span style="color: #ff0000;">A2</span></span><span style="color: #4e9a06;"><span style="color: #000000;">);</span><span style="color: #4e9a06;">"|"</span><span style="color: #000000;">;</span><span style="color: #000000;">LÄNGE(</span><span style="color: #000000;"><span style="color: #0000ff;">A1</span>)-LÄNGE(WECHSELN(<span style="color: #000000;">KLEIN(</span><span style="color: #ce5c00;"></span><span style="color: #0000ff;">A1</span><span style="color: #000000;">);<span style="color: #000000;"><span style="color: #000000;"><span style="color: #000000;">KLEIN(</span><span style="color: #ff0000;">A2</span></span><span style="color: #4e9a06;"><span style="color: #000000;">)</span></span></span></span><span style="color: #4e9a06;"></span><span style="color: #000000;">;</span><span style="color: #4e9a06;">""</span></span><span style="color: #000000;">))</span></span><span style="color: #000000;">)</span></span></span><span style="color: #000000;">)</span></span></span><span style="color: #000000;"></span>
</pre>
</div>
<p><br />5.) Wird nach mehreren Zeichen gesucht muss die Differenz noch durch die Anzahl der Suchzeichen geteilt werden.<br />In <strong><span style="color: #ff0000;">A2</span></strong> steht z.B. "<strong>or</strong>"<br /><!-- HTML generated using hilite.me --></p>
<div style="background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #ce5c00; font-weight: bold;">23 </span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #ce5c00;">=</span></span></span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #000000;">FINDEN</span></span></span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #000000;">(</span><span style="color: #000000;"><span style="color: #4e9a06;">"|"</span><span style="color: #000000;"></span>;</span><span style="color: #4e9a06;"><span style="color: #000000;"><span style="color: #000000;">WECHSELN</span><span style="color: #000000;">(KLEIN(</span><span style="color: #0000ff;">A1</span><span style="color: #000000;">);<span style="color: #000000;">KLEIN(</span><span style="color: #ff0000;">A2</span></span><span style="color: #4e9a06;"><span style="color: #000000;">);</span><span style="color: #4e9a06;">"|"</span><span style="color: #000000;">;</span><span style="color: #000000;">(LÄNGE(</span><span style="color: #000000;"><span style="color: #0000ff;">A1</span>)-LÄNGE(WECHSELN(<span style="color: #000000;">KLEIN(</span><span style="color: #ce5c00;"></span><span style="color: #0000ff;">A1</span><span style="color: #000000;">);<span style="color: #000000;"><span style="color: #000000;"><span style="color: #000000;">KLEIN(</span><span style="color: #ff0000;">A2</span></span><span style="color: #4e9a06;"><span style="color: #000000;">)</span></span></span></span><span style="color: #4e9a06;"></span><span style="color: #000000;">;</span><span style="color: #4e9a06;">""</span></span><span style="color: #000000;">))</span></span><span style="color: #000000;">)/LÄNGE(<span style="color: #ff0000;">A2</span><span style="color: #ce5c00;"></span>)))</span></span></span></span></span><span style="color: #000000;"></span>
</pre>
</div>
<p>&nbsp;</p>
<p>6.) <strong><span style="color: #ff0000;">Fehler abfangen</span></strong>. Wird im Suchenfeld ein Zeichen/Zeichengruppe eingegeben die nicht vorkommt wird <strong>#WERT!</strong> angezeigt. Dies hat zur Folge dass darauf basierende Berechnungen ebenfalls diesen Fehler ausgeben. Über die Funktion <strong>WENNFEHLER</strong> kann das recht einfach abgefangen werden. Der 2. Parameter dieser Funktion bietet die Möglichkeit etwas eigenes im Fehlerfall auszugeben. hier im Bsp ist es einen Null <span style="color: #ffcc00;"><strong>0<br /></strong></span>(Hier mit Bsp. wird nach "en" gesucht)<span style="color: #ffcc00;"><strong><br /></strong></span></p>
<div style="background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #ce5c00; font-weight: bold;">23 </span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #ce5c00;">=</span></span></span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #000000;">WENNFEHLER(FINDEN</span></span></span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #000000;">(</span><span style="color: #000000;"><span style="color: #4e9a06;">"|"</span><span style="color: #000000;"></span>;</span><span style="color: #4e9a06;"><span style="color: #000000;"><span style="color: #000000;">WECHSELN</span><span style="color: #000000;">(KLEIN(</span><span style="color: #0000ff;">A1</span><span style="color: #000000;">);<span style="color: #000000;">KLEIN(</span><span style="color: #ff0000;">A2</span></span><span style="color: #4e9a06;"><span style="color: #000000;">);</span><span style="color: #4e9a06;">"|"</span><span style="color: #000000;">;</span><span style="color: #000000;">(LÄNGE(</span><span style="color: #000000;"><span style="color: #0000ff;">A1</span>)-LÄNGE(WECHSELN(<span style="color: #000000;">KLEIN(</span><span style="color: #0000ff;">A1</span><span style="color: #000000;">);<span style="color: #000000;"><span style="color: #000000;"><span style="color: #000000;">KLEIN(</span><span style="color: #ff0000;">A2</span></span><span style="color: #4e9a06;"><span style="color: #000000;">)</span></span></span></span><span style="color: #4e9a06;"></span><span style="color: #000000;">;</span><span style="color: #4e9a06;">""</span></span><span style="color: #000000;">))</span></span><span style="color: #000000;">)/LÄNGE(<span style="color: #ff0000;">A2</span><span style="color: #ce5c00;"></span>)));<span style="color: #ffcc00;"><strong>0</strong></span>)</span></span></span></span></span><span style="color: #000000;"></span>
</pre>
</div>
<p>&nbsp;</p>
<hr />
<p><strong>Jetzt noch eine kleine Erweiterung und die Trefferposition zu verändern:</strong></p>
<p>A.) Eine Erweiterung um das vorletzte, drittletzte etc. Vorkommen des Zeichens/-folge zu ermitteln. Hierfür wurde eine 3 Zelle für die Berechnung hinzugefügt. Darin steht wie viele Treffer zurück gezählt werden soll von letzten Vorkommen des Zeichens/-folge. <strong><span style="color: #33cccc;">A3</span></strong> z.B. "<strong>1</strong>"<span style="color: #ffcc00;"><strong> <br /></strong></span></p>
<div style="background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #ce5c00; font-weight: bold;">11 </span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #ce5c00;">=</span></span></span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #000000;">WENNFEHLER(FINDEN</span></span></span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #000000;">(</span><span style="color: #000000;"><span style="color: #4e9a06;">"|"</span><span style="color: #000000;"></span>;</span><span style="color: #4e9a06;"><span style="color: #000000;"><span style="color: #000000;">WECHSELN</span><span style="color: #000000;">(KLEIN(</span><span style="color: #0000ff;">A1</span><span style="color: #000000;">);<span style="color: #000000;">KLEIN(</span><span style="color: #ff0000;">A2</span></span><span style="color: #4e9a06;"><span style="color: #000000;">);</span><span style="color: #4e9a06;">"|"</span><span style="color: #000000;">;</span><span style="color: #000000;">(LÄNGE(</span><span style="color: #000000;"><span style="color: #0000ff;">A1</span>)-LÄNGE(WECHSELN(<span style="color: #000000;">KLEIN(</span><span style="color: #0000ff;">A1</span><span style="color: #000000;">);<span style="color: #000000;"><span style="color: #000000;"><span style="color: #000000;">KLEIN(</span><span style="color: #ff0000;">A2</span></span><span style="color: #4e9a06;"><span style="color: #000000;">)</span></span></span></span><span style="color: #4e9a06;"></span><span style="color: #000000;">;</span><span style="color: #4e9a06;">""</span></span><span style="color: #000000;">))</span></span><span style="color: #000000;">)/LÄNGE(<span style="color: #ff0000;">A2</span><span style="color: #ce5c00;"></span>)-<span style="color: #33cccc;">A3</span>));<span style="color: #ffcc00;"><strong>0</strong></span>)</span></span></span></span></span><span style="color: #000000;"></span>
</pre>
</div>
<p>&nbsp;<br />B.) Final noch prüfen wenn das Zurücksetzen der Treffer zu groß gewählt wurde. Dann erste vorkommen des Zeichens/-folge verwenden.. Die folgende Funktion liefert die Treffer-Anzahl für die Wechseln-Funktion<span style="color: #ffcc00;"><strong><br /></strong></span></p>
<div style="background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #ce5c00; font-weight: bold;">11 </span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #ce5c00;">=</span></span></span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #000000;"></span></span></span>WENN((LÄNGE(<span style="color: #000000;"><span style="color: #0000ff;">A1</span></span><span style="color: #ce5c00;"></span>)-LÄNGE(WECHSELN(KLEIN(<span style="color: #000000;"><span style="color: #0000ff;">A1</span></span><span style="color: #ce5c00;"></span>);KLEIN(<span style="color: #000000;"><span style="color: #000000;"><span style="color: #ff0000;">A2</span></span></span><span style="color: #ce5c00;"></span>);"")))/LÄNGE(<span style="color: #000000;"><span style="color: #000000;"><span style="color: #ff0000;">A2</span></span></span><span style="color: #ce5c00;"></span>)-<span style="color: #000000;"><span style="color: #33cccc;">A3</span></span><span style="color: #ce5c00;"></span>&lt;=0;1;(LÄNGE(<span style="color: #000000;"><span style="color: #0000ff;">A1</span></span><span style="color: #ce5c00;"></span>)-LÄNGE(WECHSELN(KLEIN(<span style="color: #000000;"><span style="color: #0000ff;">A1</span></span><span style="color: #ce5c00;"></span>);KLEIN(<span style="color: #000000;"><span style="color: #000000;"><span style="color: #ff0000;">A2</span></span></span><span style="color: #ce5c00;"></span>);"")))/LÄNGE(<span style="color: #000000;"><span style="color: #000000;"><span style="color: #ff0000;">A2</span></span></span><span style="color: #ce5c00;"></span>)-<span style="color: #000000;"><span style="color: #33cccc;">A3</span></span><span style="color: #ce5c00;"></span>)
</pre>
</div>
<hr />
<h4><strong><a id="ff">Finale Funktion</a>: </strong><span style="color: #ffcc00;"><strong><br /></strong></span></h4>
<div style="background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #ce5c00; font-weight: bold;">11 <span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #ce5c00;">=</span></span></span><span style="color: #ce5c00;"><span style="color: #000000;"><span style="color: #000000;"></span></span></span></span>WENNFEHLER(FINDEN("|";WECHSELN(KLEIN(<span style="color: #000000;"><span style="color: #0000ff;">A1</span></span><span style="color: #ce5c00;"></span>);KLEIN(<span style="color: #000000;"><span style="color: #000000;"><span style="color: #ff0000;">A2</span></span></span><span style="color: #ce5c00;"></span>);"|";WENN((LÄNGE(<span style="color: #000000;"><span style="color: #0000ff;">A1</span></span><span style="color: #ce5c00;"></span>)-LÄNGE(WECHSELN(KLEIN(<span style="color: #000000;"><span style="color: #0000ff;">A1</span></span><span style="color: #ce5c00;"></span>);KLEIN(<span style="color: #000000;"><span style="color: #000000;"><span style="color: #ff0000;">A2</span></span></span><span style="color: #ce5c00;"></span>);"")))/LÄNGE(<span style="color: #000000;"><span style="color: #000000;"><span style="color: #ff0000;">A2</span></span></span><span style="color: #ce5c00;"></span>)-<span style="color: #000000;"><span style="color: #33cccc;">A3</span></span><span style="color: #ce5c00;"></span>&lt;=0;1;(LÄNGE(<span style="color: #000000;"><span style="color: #0000ff;">A1</span></span><span style="color: #ce5c00;"></span>)-LÄNGE(WECHSELN(KLEIN(<span style="color: #000000;"><span style="color: #0000ff;">A1</span></span><span style="color: #ce5c00;"></span>);KLEIN(<span style="color: #000000;"><span style="color: #000000;"><span style="color: #ff0000;">A2</span></span></span><span style="color: #ce5c00;"></span>);"")))/LÄNGE(<span style="color: #000000;"><span style="color: #000000;"><span style="color: #ff0000;">A2</span></span></span><span style="color: #ce5c00;"></span>)-<span style="color: #000000;"><span style="color: #33cccc;">A3</span></span><span style="color: #ce5c00;"></span>)));0)
</pre>
</div>
<p>&nbsp;<img src="/images/stories/office/excel/findenr2.png" alt="" /></p>
<hr />
<h3>VBA-Lösung:</h3>
<p>Hierzu muss die Excel-Datei mit Macros gespeichert werden (*.xlsm). Sollte aus Sicherheitsgründen dies nicht möglich sein dann im 2. Teil die Lösung mit verschachtelten Funktionen anwenden.<br />1. In den VBA-Editor wechseln (ALT+F11) und im Menü auf Einfügen Modul klicken. Jetzt sollte ein neues Modul im Projekt-Explorer angezeigt werden.<br />2. Im Modul-Fenster folgenden Code einfügen.</p>
<p><!-- HTML generated using hilite.me --></p>
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #000080; font-weight: bold;">Option</span> Explicit
<span style="color: #008800; font-style: italic;">'Diese Funktion liefert das letzte Vorkommen einer Zeichenkette.</span>
<span style="color: #008800; font-style: italic;">'ggf. auch Treffer davor</span>
<span style="color: #000080; font-weight: bold;">Function</span> FindenR(<span style="color: #000080; font-weight: bold;">ByVal</span> SuchZeichen, <span style="color: #000080; font-weight: bold;">ByVal</span> TextZelle, <span style="color: #000080; font-weight: bold;">Optional</span> Rueckschritte% = <span style="color: #0000ff;">0</span>)
<span style="color: #000080; font-weight: bold;">Dim</span> i%, j%
i = InStrRev(TextZelle, SuchZeichen, , vbTextCompare)
<span style="color: #000080; font-weight: bold;">If</span> Rueckschritte &gt; <span style="color: #0000ff;">0</span> <span style="font-weight: bold;">And</span> i &gt; <span style="color: #0000ff;">0</span> <span style="color: #000080; font-weight: bold;">Then</span>
Rueckschritte = Rueckschritte - <span style="color: #0000ff;">1</span>
TextZelle = Left(TextZelle, i - <span style="color: #0000ff;">1</span>)
j = FindenR(SuchZeichen, TextZelle, Rueckschritte)
<span style="color: #000080; font-weight: bold;">If</span> j &gt; <span style="color: #0000ff;">0</span> <span style="color: #000080; font-weight: bold;">Then</span> i = j
<span style="color: #000080; font-weight: bold;">End</span> <span style="color: #000080; font-weight: bold;">If</span>
FindenR = i
<span style="color: #000080; font-weight: bold;">End</span> <span style="color: #000080; font-weight: bold;">Function</span>
</pre>
</div>
<p><br />3.) Zurück in Excel auf Funktionen gehen.<br />&nbsp;&nbsp;&nbsp; Kategorie Benutzerdefiniert auswählen<br />&nbsp;&nbsp;&nbsp; <strong>Funktion</strong> <span style="color: #0000ff;"><strong>FindenR </strong></span>anklicken und Felder füllen<br /><br /><img src="/images/stories/office/excel/findenr.png" alt="findenr" /></p>

Hauptmenü

  • Home
  • Programm-Schnipsel
    • Joomla
    • MS Office
      • Access
      • Excel
      • Outlook
      • Word
    • PHP
    • dBase
    • Powershell
    • WSH (VBS)
  • Tips und Tricks

Anmeldeformular

  • Passwort vergessen?
  • Benutzername vergessen?
  • Impressum
  • Datenschutzerklärung