0
1

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 3 years have passed since last update.

VBAでスイッチ文を記述する

Posted at

VBAでスイッチ文

VBAにはswicthはない。
その代替にSelect Case ~ Case ~ Case Else ~ End Selectを使用する。
VBAのCaseはラベルではなく次のCase文が来るまでのブロックを実行する。
よってCase文が連続した場合には空文が指定されたとみなされ何も実行されない。
なおBasicの特徴としてCase文の後にコロンをつけて実行文を記述することもできる。

    Case "foo"  ⇒なにも実行されない
    Case "bar"  ⇒なにも実行されない
    Case "baz"  ⇒ b = 30 が実行される
        b = 30
Sub select_case()
    a = "foo"
    b = ""
    Select Case a
        Case "foo"
        Case "bar"
        Case "baz"
            b = 30
        Case Else
            b = 40
    End Select
    Debug.Print b
End Sub
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?