Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Notice: Undefined offset: 1 in /home/www/s013w173/html/mambots/system/blocklist.php on line 38 Warning: Cannot modify header information - headers already sent by (output started at /home/www/s013w173/html/mambots/system/blocklist.php:38) in /home/www/s013w173/html/includes/joomla.php on line 697 Warning: Cannot modify header information - headers already sent by (output started at /home/www/s013w173/html/mambots/system/blocklist.php:38) in /home/www/s013w173/html/index.php on line 251 Warning: Cannot modify header information - headers already sent by (output started at /home/www/s013w173/html/mambots/system/blocklist.php:38) in /home/www/s013w173/html/index.php on line 252 Warning: Cannot modify header information - headers already sent by (output started at /home/www/s013w173/html/mambots/system/blocklist.php:38) in /home/www/s013w173/html/index.php on line 253 Warning: Cannot modify header information - headers already sent by (output started at /home/www/s013w173/html/mambots/system/blocklist.php:38) in /home/www/s013w173/html/index.php on line 254 Warning: Cannot modify header information - headers already sent by (output started at /home/www/s013w173/html/mambots/system/blocklist.php:38) in /home/www/s013w173/html/index.php on line 255 Excel InLife - Grundlagen - Schleifen

Willkommen auf Excel-Inlife

Diese Seite soll Ihnen im Umgang mit Excel im Alltag helfen. Hierbei finden Sie im Downloadbereich Beispieldateien und Add-Ins, mit denen Sie ihre alltäglichen Excel-Probleme lösen können. Zudem finden Sie im "Tipps&Tricks"-Bereich Lösungen zu Fragen, die in verschiedenen Foren öfters auftreten. 

 
  Home arrow Tutorials arrow Grundlagen arrow Grundlagen - Schleifen
Grundlagen - Schleifen
(16 Stimmen)
Das Wissen über den den Umgang mit Schleifen stellt vor allem in Excel-Programmierung eines der wichtigsten Voraussetzen dar. Mit Hilfe von Schleifen lassen sich mehrmalige Anweisungsfolgen ausführen. Auch dieses Tutorial soll anhand eines kleinen Beispiels die Grundlagen  bezüglich der Anwendung der verschiedenen Schleifenarten darstellen.

  A B
1 Vorname Name
2 Sebastian Schulz
3 Stefan Muster
4 Kathrin Schneider
 

For ... Next


Die erste Schleifenart stellt die For ... Next-Schleife dar, welche in der alltäglichen Programmierung einer der häufigst anzutreffenden Varianten ist. Sie sollte möglichst dann Anwendung finden, wenn die Anzahl der möglichen Operationen bereits im Vorfeld bekannt ist. Hierzu werden die Schleifendurchläufe in einer Zählvariable zuvor festgelegt. Der Ablauf wird solange durchlaufen, bis der zuvor definierte Endwert erreicht ist. Die allgemeine Schleifensyntax soll im Folgenden dargestellt werden. Die Befehle in Klammern stellen optionale Parameter dar, die nicht zwingend mit angegeben bzw. Verwendung finden müssen.

  For Variable = Startwert To Endwert (Step Schrittwert)
           ANWEISUNG
        (Exit For)
           ANWEISUNG
  Next Variable


Die Variable selber muss ein nummerische Wert (a,b,c,...) sein, der vom Programmierer frei gewählt werden darf. Dieser Wert sollte jedoch, um spätere Fehler zu vermeiden, nach der geplanten Variablengröße dimensioniert werden (z.B. Dim a as Integer,...).

Die Schrittweite, ein optionaler Parameter, beschreibt die Größe, die die Schleife bei der Bearbeitung voransschreiten soll. Wird dieser Parameter nicht extra mit angegeben, geht Excel von einer Schrittweite von 1 aus.

Der Startwert ist ein nummerischer Wert, der den Anfang der Schleife darstellt.

Genauso wie der Startwert, muss auch der Endwert ein nummerischer Wert sein, der den Ende der Schleife bestimmt. Sollte jedoch der Entwert größer als der Startwert sein, resultiert hieraus eine negative Schrittweite.

Nun soll aus unserem Beispiel der Vorname der obigen Tabelle jeweils in einem Hinweisfenster angezeigt werden. In unserem Beispiel ist bereits der Startwert (Zeile 2) und der Endwert (Zeile 4) bekannt und kann so in unsere Schleife problemlos übernommen werden. Zusätzlich könnte auf den Step-Befehl verzichtet werden, da hier jeweils nur um einen Schritt vorangeschritten werden kann. Der Befehl wurde zur Einhaltung der Syntax mit angegeben.

    Dim i As Integer
    For i = 2 To 4 Step 1
        MsgBox Cells(i, 1).Value
    Next i


Diese Schleife kann jedoch auch vorzeitig mit einer Exit-For-Anweisung verlassen werden, um mit der Anweisung nach der For ... Next-Schleife fortzufahren. Dies wird jedoch häufig mit einer Bedingung (if-else-Anweisung) verbunden. Somit wollen wir unser oberes Beispiel dahingehend erweitern und die Schleife verlassen, sobald der Name "Stefan" angezeigt werden würde und dann ein Hinweis ausgeben. Somit müsste der VBA-Code hierzu lauten:

Dim i As Integer
    For i = 2 To 4
        If Cells(i, 1).Value = "Stefan" Then
            Exit For
        Else
            MsgBox Cells(i, 1).Value
        End If
    Next i
    MsgBox "Der Name Stefan wurde erreicht"


Do ... Loop


Die Do ... Loop Schleife wird solange angewendet, bis eine Bedingung erfüllt ist. Im Gegensatz zur For ... Next-Schleife muss hier im Vorfeld nicht bekannt sein, wieviele Werte durchlaufen werden. Sie wird solange ausgeführt, bis eine Bedingung erfüllt ist. Hier kann zwischen zwei verschidenen Typen unterschieden werden.
Die allgemeine Syntax von Do While ist.

Do While Bedingung
    ANWEISUNG
    (Exit Do)
    ANWEISUNG
Loop


Übertragen auf unser Beispiel bedeutet das, dass die Schleife solange durchlaufen werden muss, bis eine leere Zelle zum ersten mal auftritt. (Dies hätte aber auch mit Hilfe einer If-Anweisung in der For ... Next-Schleife realisiert werden können)

  Dim z As Integer
    z = 2
    Do While Cells(z, 1) <> ""
        MsgBox Cells(z, 1).Value
        z = z + 1
    Loop



Auch hier soll zunächst auf die allgemeine Schreibweise eingegangen werden.

Do Until Bedingung
   ANWEISUNG
   (Exit Do)
   ANWEISUNG
Loop (Bedingung)


Auch dies soll auf unser Beispiel angewendet werden und muss folgendermaßen lauten.

Dim z As Integer
    z = 2
    Do Until Cells(z, 1) <> ""
        MsgBox Cells(z, 1).Value
        z = z + 1
    Loop


Die Zuweisung der Bedingung kann sowohl am Anfang sowie am Schluss der Schleife erfolgen. Soll die Schleife jedoch mindestens einmal durchlaufen werden, muss die Bedingung am Ende der Schleife gesetzt werden. (Loop Bedingung).
Die Schleife kann genau wie die For...Next.Schleife mit einer Exit Anweisung, in diesem Fall Exit Do, vorzeitig beendet werden.

For each ... Next


Im Gegensatz zur For...Next-Schleife, bei der die Anzahl der Durchläufe zurvor festgelegt wurde (mit dem Endwert), legt die For each...Next-Schleife das Ende implizit fest. Diese Schleife wiederholt die Anweisung für alle festgelegten Elemente.

For Each Element In Objekt
ANWEISUNG
(Exit Do)
ANWEISUNG
Next (Element)


Die Elemente können verschiedener Natur sein und sich zum Beispiel auf Zellen, Tabellenblätter,.. beziehen.

Objekte dagegen können ebenfalls verschiedenartig sein und beispielsweise eine Arbeitsmappe oder ein Tabellenblatt sein.

For Each cell In Range("A2:A4")
        MsgBox cell.Value
Next


 
< zurück