« 2018年2月 | トップページ | 2018年4月 »

2018年3月

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

| | コメント (0) | トラックバック (0)

2018年3月25日 (日)

For文の使い方とテンプレート

VBAをしばらく使っていないと
For文などの基本的な事も結構忘れてしまいます。

そんな時のために
コピぺして使えるテンプレートを作っておきます。

Integer型の変数の宣言と
For文はセットで使われるので
まとめてテンプレートにしておきます。
For文の中はよく使うかなと思うものにしました。
状況にあわせて変更してください。

For文の使い方1:
まずは一番シンプルな使い方のテンプレートを作ります。

i = 1から始めます。
For文の中を実行して
Next i で i が +1 されてi = 2になります。
i が5以下なので
再度For文の中を実行します。
Next i で i が +1 されてi = 3になります。
このようなループを繰り返して
Next i で i が +1 されてi = 6になると
iは5よりも大きくなるのでFor文を終了します。

コードはこちら

For文の使い方1:

Sub macro180325a()
'For文の使い方1
'(i, 1)のセルにiの値を入力する

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

実行結果:
Vba20180325a

For文の使い方2:
次は"For文の使い方1"を改造して
iがNext iで+2されるようにします。

「For i = 1 to 5」の後に
「Step 2」を加えて
「For i = 1 to 5 Step 2」にします。
実は"For文の使い方1"では「Step 1」が省略されています。
Stepを省略するとiの増分は+1になります。
Stepの後には任意の整数を指定出来ます。

コードはこちら

For文の使い方2:

Sub macro180325b()
'For文の使い方2
'(i, 1)のセルにiの値を入力する

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

実行結果:
Vba20180325b

For文の使い方3:
再度、"For文の使い方1"を改造して
条件によりFor文の途中で抜ける方法を
テンプレートにしておきます。

For文の中に「Exit For」を記述することで
途中でもFor文を抜けることができます。
「Exit For」はFor文の中で何回でも使えます。

条件判断はIf文を使います。

コードはこちら

For文の使い方3:

Sub macro180325c()
'For文の使い方3
'flag >< 0のとき、
'(i, 1)のセルにiの値を入力する
'flag = 0のとき、
'For文の中を実行しないでFor文を抜ける

    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

flag = 0のときの実行結果:
Vba20180325c

上記のコードのflagを0以外にすると
"For文の使い方1"と同じ実行結果になります。

For文の使い方4:
次は"For文の使い方3"を改造して
条件によりFor文の中を実行しないで、次のループにいきたい場合
また、For文の途中で次のループにいきたい場合の方法です。
For文の最終行に飛ぶのに「GoTo」を使います。
「GoTo」は指定した行ラベルに飛び実行を続けます。
行ラベルは「任意の名前:」で指定できます。

コードはこちら

For文の使い方4:

Sub macro180325d()
'For文の使い方4
'iが奇数のとき、
'(i, 1)のセルにiの値を入力する
'iが 偶数のとき、
'For文の中を実行しないでSTEP1に行く

    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

実行結果:
Vba20180325d

For文の使い方5:
次はFor文を入れ子にして使う方法をテンプレートにしておきます。
変数をi, jの2つ宣言してFor文に使います。

コードはこちら

For文の使い方5:

Sub macro180325e()
'For文の使い方5
'2重入れ子
'セル(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

実行結果:
Vba20180325e

| | コメント (0) | トラックバック (0)

« 2018年2月 | トップページ | 2018年4月 »