| 演算子 | 説明 | 例 | If文例 | 結果 |
| < | 小さい | 7 < 4 | If 7 < 4 Then MsgBox "True" Else MsgBox "Fales" | Fales |
| <= | 以下 | 4 <= 7 | If 4 <= 7 Then MsgBox "True" Else MsgBox "Fales" | True |
| > | 大きい | 7 > 4 | If 7 > 4 Then MsgBox "True" Else MsgBox "Fales" | Fales |
| >= | 以上 | 4 >= 7 | If 4 >= 7 Then MsgBox "True" Else MsgBox "Fales" | Fales |
| = | 等しい | 4 = 7 | If 4 = 7 Then MsgBox "True" Else MsgBox "Fales" | Fales |
| <> | 等しくない | 4 <> 7 | If 4 <> 7 Then MsgBox "True" Else MsgBox "Fales" | True |
Sub 分岐1_1()
Dim aa As Integer
aa = ThisWorkbook.Sheets("Sheet1").Cells(2, 2)
If aa > 85 Then
ThisWorkbook.Sheets("Sheet1").Cells(2, 3) = "優"
End If
End Sub
--------------------------------------------------
Sub 分岐1_2()
Dim aa As Integer
aa = ThisWorkbook.Sheets("Sheet1").Cells(2, 2)
If aa > 85 Then
ThisWorkbook.Sheets("Sheet1").Cells(2, 3) = "優"
Else
ThisWorkbook.Sheets("Sheet1").Cells(2, 3) = "並"
End If
End Sub
--------------------------------------------------
Sub 分岐1_3()
Dim aa As Integer
aa = ThisWorkbook.Sheets("Sheet1").Cells(2, 2)
If aa > 85 Then
ThisWorkbook.Sheets("Sheet1").Cells(2, 3) = "優"
ElseIf aa > 60 Then
ThisWorkbook.Sheets("Sheet1").Cells(2, 3) = "良"
Else
ThisWorkbook.Sheets("Sheet1").Cells(2, 3) = "可"
End If
End Sub
|
Sub 分岐2_1()
Dim aa As Integer
aa = ThisWorkbook.Sheets("Sheet1").Cells(2, 2)
Select Case aa
Case Is > 85
ThisWorkbook.Sheets("Sheet1").Cells(2, 3) = "優"
Case Is > 60
ThisWorkbook.Sheets("Sheet1").Cells(2, 3) = "良"
Case Else
ThisWorkbook.Sheets("Sheet1").Cells(2, 3) = "可"
End Select
End Sub
-----------------------------------------------------------
Sub 分岐2_2()
Dim aa As Integer
aa = ThisWorkbook.Sheets("Sheet1").Cells(2, 2)
Select Case aa
Case 86 To 100
ThisWorkbook.Sheets("Sheet1").Cells(2, 3) = "優"
Case 61 To 85
ThisWorkbook.Sheets("Sheet1").Cells(2, 3) = "良"
Case Else
ThisWorkbook.Sheets("Sheet1").Cells(2, 3) = "可"
End Select
End Sub
-----------------------------------------------------------
Sub 分岐2_3()
Dim bb As String
bb = ThisWorkbook.Sheets("Sheet1").Cells(4, 2)
Select Case bb
Case "優"
ThisWorkbook.Sheets("Sheet1").Cells(4, 3) = "86〜100点"
Case "良"
ThisWorkbook.Sheets("Sheet1").Cells(4, 3) = "61〜85点"
Case "可"
ThisWorkbook.Sheets("Sheet1").Cells(4, 3) = "60点以下"
Case Else
ThisWorkbook.Sheets("Sheet1").Cells(4, 3) = "指定文字以外"
End Select
End Sub
|
Sub 分岐3_1()
Dim aa As Integer
aa = Cells(2, 2)
If aa > 85 Then
Cells(2, 3) = "優"
Else
Cells(2, 3) = " 並"
End If
End Sub
---------------------------------------------------------
Sub 分岐3_1a()
Dim aa As Integer
aa = Cells(2, 2)
If aa > 85 Then Cells(2, 3) = "優" Else Cells(2, 3) = " 並"
End Sub
|
Sub 分岐3_2()
Dim cc As String
cc = UCase(ThisWorkbook.Sheets("Sheet1").Cells(16, 2))
If cc = "A" Then
ThisWorkbook.Sheets("Sheet1").Cells(16, 3) = "優1"
ElseIf cc = "B" Then
ThisWorkbook.Sheets("Sheet1").Cells(16, 3) = "優2"
ElseIf cc = "C" Then
ThisWorkbook.Sheets("Sheet1").Cells(16, 3) = "良"
ElseIf cc = "D" Then
ThisWorkbook.Sheets("Sheet1").Cells(16, 3) = "可"
Else
ThisWorkbook.Sheets("Sheet1").Cells(16, 3) = "不可"
End If
End Sub
---------------------------------------------------------
Sub 分岐3_2a()
Dim cc As String
cc = UCase(ThisWorkbook.Sheets("Sheet1").Cells(16, 2))
Select Case cc
Case "A", "B"
ThisWorkbook.Sheets("Sheet1").Cells(16, 3) = "優"
Case "C", "D"
ThisWorkbook.Sheets("Sheet1").Cells(16, 3) = "並"
Case Else
ThisWorkbook.Sheets("Sheet1").Cells(16, 3) = "指定文字以外"
End Select
End Sub
|
Sub 分岐3_3()
suzi = InputBox("数字を入力して下さい", "A列検索", "")
Columns("A:A").Select
Set actv = Selection.Find(suzi, , , xlWhole, xlByColumns, xlNext, True)
If actv Is Nothing Then
MsgBox "指定の数字はありません"
Else
actv.Activate
MsgBox suzi & "は「" & ActiveCell.Address & "」に有ります"
End If
Range("A1").Select
End Sub
|
Sub 分岐3_4a()
If "abcd" Like "abcd" = True Then
MsgBox "文字が一致しています"
End If
End Sub
---------------------------------------------------------
Sub 分岐3_4b()
If "abcd" Like "abcd" Then
MsgBox "文字が一致しています"
End If
End Sub
---------------------------------------------------------
Sub 分岐3_4c()
If "abcd" Like "zyxw" = False Then
MsgBox "文字が不致です"
End If
End Sub
---------------------------------------------------------
Sub 分岐3_4d()
If Not "abcd" Like "zyxw" Then
MsgBox "文字が不致です"
End If
End Sub
|
Sub 分岐3_5a()
If Cells(1, 1) = "" Then
MsgBox "空白セルです"
Else
MsgBox "セルに[&H" & Hex(Asc(Cells(1, 1))) & "]・・のデータがあります"
End If
End Sub
---------------------------------------------------------
Sub 分岐3_5b()
If Cells(1, 1) = "" Or Cells(1, 1) = " " Or Cells(1, 1) = " " Then
MsgBox "空白セルです"
Else
MsgBox "セルに[&H" & Hex(Asc(Cells(1, 1))) & "]・・のデータがあります"
End If
End Sub
---------------------------------------------------------
Sub 分岐3_5c()
If IsEmpty(Cells(1, 1)) Then
MsgBox "空白セルです"
Else
MsgBox "セルに[&H" & Hex(Asc(Cells(1, 1))) & "]・・のデータがあります"
End If
End Sub
---------------------------------------------------------
Sub 分岐3_5d()
If Len(Cells(1, 1)) = 0 Then
MsgBox "空白セルです"
Else
MsgBox "セルに[" & Len(Cells(1, 1)) & "]文字のデータがあります"
End If
End Sub
|


Sub Macro8a()
su = 64
chk = IIf(su > 80, "優", "並")
End Sub
|

Sub 繰返し1_1()
Dim i As Integer
For i = 1 To 9
Cells(i, 1).Value = "ループ" & i & "実行"
Next i
End Sub
----------------------------------------------------------
Sub 繰返し1_2()
Dim i As Integer
For i = 1 To 9 Step 2
Cells(i, 2).Value = "ループ" & i & "実行"
Next
End Sub
----------------------------------------------------------
Sub 繰返し1_3()
Dim i As Integer
Dim ia As Integer
ia = 1
For i = 9 To 1 Step -1
Cells(i, 3).Value = "ループ" & ia & "実行"
ia = ia + 1
Next
End Sub
----------------------------------------------------------
Sub 繰返し1_4()
Dim i As Integer
For i = 1 To 9
Cells(i, 4).Value = "ループ" & i & "実行"
If i = 5 Then
Exit For
End If
Next
End Sub
|
'[1] End(xlUp)で読み取り
Sub 最終行1()
Dim endr As Integer
endr = Range("B10000").End(xlUp).Row
MsgBox "このシートのデータ最終行は:" & endr
End Sub
----------------------------------------------------
[2]End(xlDown)で読み取り
Sub 最終行2()
Dim endr As Integer
endr = Range("B3").End(xlDown).Row
MsgBox "このブロックの最終セル:" & endr
End Sub
----------------------------------------------------
[3]UsedRange最終を読み取り
Sub 最終行3()
Dim endr As Integer
ActiveSheet.UsedRange.Select
endr = Selection.Rows.Count
MsgBox "このシートのデータ最終行は:" & endr
End Sub
----------------------------------------------------
[4]SpecialCells(xlLastCell)で最終を読み取り
Sub 最終行4()
Dim endr As Integer
ActiveSheet.Cells.SpecialCells(xlLastCell).Select
endr = ActiveCell.Row
endc = ActiveCell.Column
MsgBox "このシートの最終行[" & endr & "] 最終列[" & endc & "]"
End Sub
----------------------------------------------------
[5]SpecialCells(CurrentRegion)で空白行まで選択
Sub 最終行5()
Dim endr As Integer
Range("A9").Select
Selection.CurrentRegion.Select
endr = Selection.Rows.Count
Range("A1").Select
MsgBox "このシートの最終セル:" & endr + 8
End Sub
|
| ※[5]はシート内の一部のデータベース(表)を指定するとき使用できて便利です。 |
Sub 削除上から()
Dim i As Integer, endr As Integer
Application.ScreenUpdating = False
ThisWorkbook.Sheets("Sheet1").Select
endr = Range("A31000").End(xlUp).Row + 1
For i = 3 To endr
If Cells(i, 1) = "" Then
Exit Sub
End If
If IsNumeric(Cells(i, 1)) = False Then
Rows(i).Delete Shift:=xlUp
i = i - 1
End If
Next
---------------------------------------------------------------------
Sub 削除下から()
Dim i As Integer, endr As Integer
Application.ScreenUpdating = False
ThisWorkbook.Sheets("Sheet1").Select
endr = Range("A31000").End(xlUp).Row + 1
For i = endr To 3 Step -1
If IsNumeric(Cells(i, 1)) = False Then
Rows(i).Delete Shift:=xlUp
End If
Next
End Sub
|
| For ・・・ Nextステートメントの「Next i」のように後に付ける変数「i」はカウンタ変数ですが省略可能です 同様にFor Each ・・・Next ステートメントの「Next myobj」などの変数「myobj」はオブジェクト変数ですが 省略できます。一般的に何方のマクロを見ても省略していますが、特に問題はありません。 ただし、入れ子(For ・・・Nextステートメントの中に更に別のFor ・・・Nextを入れる)の場合は、どの ループに対応しているのか明確にしてマクロを判りやすくする意味もあり「Next 」の後のカウンタ変数を 記入した方が良い。 |
Sub 繰返し2_1()
Dim sel As Range
ThisWorkbook.Sheets("ループ").Select
Cells(4, 1).Select
Selection.CurrentRegion.Select
'
For Each sel In Selection
If sel.Value > 0.05 Then sel.Interior.ColorIndex = 6
Next sel
Cells(1, 1).Select
End Sub
-----------------------------------------------------------
Sub 繰返し2_2()
Dim myrang As Range, sel As Range
ThisWorkbook.Sheets("ループ").Select
endr = Range("A1000").End(xlUp).Row
Set myrang = Range(Cells(3, 1), Cells(endr, 1))
'
For Each sel In myrang
sel.Interior.ColorIndex = xlNone
Next
Cells(1, 1).Select
End Sub
-----------------------------------------------------------
Sub 繰返し2_3()
Dim sel As Worksheet
For Each sheet_name In Worksheets
If Sheets.Count = 1 Then Exit For
sh = sheet_name.Name
Worksheets(sh).Select
aaa = ActiveSheet.UsedRange.Address
If aaa = "$A$1" And Cells(1, 1) = "" Then
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
End If
Next
End Sub
|
| For Each文は、ループに入るタイミングで、あらかじめ作業対象のコレクション(オブジェクトの集合)を、 メモリに読み込みます。そして、その読み込まれた集合に対して処理をします。これはメモリー上での 処理であり高速になります。 一方For文の方は、ループの中にセルを読み取る指示があればそのつどその場所を探して読み取る ので、メモリー上で処理するFor Each文より処理が遅くなります。 なお、覚えると便利なFor Each・・Nextステートメントですが、なじみの薄い方も多いようなので、3-3節で マクロ例を紹介済みですが、以下に文章で詳しく再説明します。 For Each文ではまず、コレクション内の個々のメンバを格納するためにDimステートメントでの変数を 宣言します。変数はバリアントまたはオブジェクトの型を指定します。 次にブック内の全シート名を読取たいのであれば、例えば、オブジェクト変数myobj を使ってWorksheets コレクションに対して繰り返し処理を行いたいのであれば、「For Each myobj In Worksheets」となり ForとNextの間に繰り返し処理を記述します。 オブジェクト変数には、処理が実行されるたびに順番にコレクション内のメンバが格納されるので 「myobj.Name」のように格納されたメンバーの名前を読み取れば、1回目の処理では1枚目の ワークシート名、2回目の処理では2枚目のワークシート名・・・となり全シート名 を読み取ることができます。 なお、コレクション内から取り出される要素の順番を制御することはできません。 |
Sub 繰返し3_1()
Dim i As Integer
i = 1
Do Until Cells(i, 1) = ""
If Cells(i, 1) > 100 Then
Cells(i, 1).Interior.ColorIndex = 6
End If
i = i + 1
Loop
End Sub
-----------------------------------------------------------
Sub 繰返し3_2()
Dim i As Integer
i = 1
Do While Cells(i, 1) <> ""
If Cells(i, 1) > 100 Then
Cells(i, 1).Interior.ColorIndex = 8
End If
i = i + 1
Loop
End Sub
|
Sub 繰返し3_3()
Dim i As Integer
i = 1
Do
If Cells(i, 1) > 100 Then
Cells(i, 1).Interior.ColorIndex = 4
End If
i = i + 1
Loop Until Cells(i, 1) = ""
End Sub
------------------------------------------------------------
Sub 繰返し3_4()
Dim i As Integer
i = 1
Do
If Cells(i, 1) > 100 Then
Cells(i, 1).Interior.ColorIndex = 7
End If
i = i + 1
Loop While Cells(i, 1) <> ""
End Sub
|
Sub 繰返し3_5()
Dim i As Integer
i = 1
Do
If Cells(i, 1) > 100 Then
Cells(i, 1).Interior.ColorIndex = 3
End If
i = i + 1
If Cells(i, 1) = "" Then Exit Do
Loop
End Sub
|