2
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VBA条件分岐、繰り返し、処理を抜ける

Last updated at Posted at 2016-07-02

For文

For i = m To n
(中身)
Next

if文

If (条件) Then
  文
End If
If (条件1) Then
   文 
ElseIf (条件2) Then
   文 
Else
   文 
End If

For文とIf文の使い分け

Private Sub Worksheet_Activate()

Dim i As Integer
For i = 1 To 10
  If IsEmpty(Cells(i, 1)) = True Then
    ActiveSheet.Tab.Color = RGB(255, 0, 0)
    Exit For
  Else
    ActiveSheet.Tab.Color = RGB(255, 255, 255)
  End If
Next

End Sub

Do Until文

Do Until (条件)
   文 
Loop

--------------------------------------------

Sub 平均が50点以下のセルを赤にする()

Dim no As Long
no = 4
Do Until Cells(no, 4) = ""
  If Cells(no, 4).Value < 50 Then
    Cells(no, 4).Interior.ColorIndex = 3
  End If
  If no >= 11 Then
    Exit Do
  Else
    no = no + 1
  End If
Loop

End Sub

条件がTrueになるまで実行する。

実行中の処理を途中で抜ける

【参考】

Exit Do

Exit For

Exit Function

Exit Property

Exit Sub
2
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?