« For文の使い方とテンプレート | トップページ | ワークシート関数をVBAで使う »

2018年3月31日 (土)

Using For Statements and Templates about it

If I do not use VBA for a while
I forget a basic thing such as a For statement well.

For such a time
I will make a template that I can copy and use.

The declaration of integer variables and For statements
are used in sets.
Let's make it a template at once.
I write codes in the For statement what I often use.
Please change according to the situation.

How to use For statement 1:
First of all, I will make the template of the simplest usage.

Start with "i = 1".
Run VBA in the For statement
"Next i" will increment i by 1 and "i = 2".
Since i is less than or equal to 5
Execute again VBA in the For statement.
"Next i" will increment i by 1 and "i = 3".
Repeat such a loop
When i is incremented by 1 and becomes "i = 6"
Since i is larger than 5, Excel ends the For statement.

Code here

How to use For statement 1:

Sub macro180325a()
'How to use For statement 1
'Enter the value of i into the cell of (i, 1)

    Dim i As Integer
   
    For i = 1 To 5
       
        Cells(i, 1) = i
          
    Next i
   

Result:
Vba20180325a

How to use For statement 2:
Next, to rewrite "How to use For statement 1",
Iwill make i be +2 on Next i.

After "For i = 1 to 5"
Add "Step 2",
Make it "For i = 1 to 5 Step 2".
Actually "Step 1" is omitted in "How to use For statement 1".
When Step is omitted, the increment of i becomes +1.
Any integer can be specified after Step.

Code here

How to use For statement 2:

Sub macro180325b()
'How to use For statement 2
'Enter the value of i into the cell of (i, 1)

    Dim i As Integer
    
    For i = 1 To 5 Step 2
       
        Cells(i, 1) = i
           
    Next i
   
End Sub

Result:
Vba20180325b

How to use For statement 3:
Again, to rewrite "How to use For statement 1",
I will write a code that can go out in the middle of a For statement depending on the condition .

By describing "Exit For" in the For statement
You can leave the For statement on the way.
"Exit For" can be used many times in the For statement.

For condition judgment, use the If statement.

Code here

How to use For statement 3:

Sub macro180325c()
'How to use For statement 3
'When 'flag> <0,
'Enter the value of i in the cell '(i, 1)
'When flag = 0,
'Do not execute in the For statement

    Dim i As Integer
    Dim flag As Integer
    flag = 0
   
    For i = 1 To 5
    
        If flag = 0 Then
            Cells(i, 1) = "Exit"
            Exit For
        End If
        
        Cells(i, 1) = i
          
    Next i
   
End Sub

Result when flag = 0:
Vba20180325c

If flag of above code is set to other than 0,
It is the same execution result as "How to use For statement 1".

How to use For statement 4:
Next, to rewrite "How to use For 3",
I will make it another code.
If you want to go to the next loop
without executing rested code in the For statement depending on the condition,
you can use this code.

Use "GoTo" to jump to the last line of the For statement.
"GoTo" jumps to the specified line label and continues execution.
You can specify the line label with "Any Name:".

Code here

How to use For statement 4:

Sub macro180325d()
'How to use For statement 4
'When i is an odd number,
'Enter the value of i in the cell (i, 1)
'When i is an even number,
'Go to "STEP 1" without executing in the For statement

    Dim i As Integer
   
    For i = 1 To 5
    
        If i Mod 2 = 0 Then
             Cells(i, 1) = "GoTo"
            GoTo STEP1
        End If
       
         Cells(i, 1) = i
STEP1:
    Next i
   
End Sub

Result:
Vba20180325d

How to use For statement 5:
The next is how to use nested For statements.
Declare two variables i and j. i and j are used in the For statements.

Code here

How to use For statement 5:

Sub macro180325e()
'How to use For statement 5
'Double nested child
'Output to cell (i, j)

    Dim i As Integer, j As Integer
      
    For i = 1 To 5
        For j = 1 To 3
                       
             Cells(i, j) = i & ", " & j
            
        Next j
    Next i
   
End Sub

Result:
Vba20180325e

|

« For文の使い方とテンプレート | トップページ | ワークシート関数をVBAで使う »

コメント

この記事へのコメントは終了しました。

トラックバック


この記事へのトラックバック一覧です: Using For Statements and Templates about it:

« For文の使い方とテンプレート | トップページ | ワークシート関数をVBAで使う »