0
0

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.

【ExcelVBA】5章:繰り返し処理(Do While ~ Loop文)

Posted at

はじめに

この章ではExcelVBAで扱うことができる繰り返し処理のうち「Do While ~ Loop文」について説明をします。前章で扱った「For ~ Next文」でだいたいの繰り返し処理は記述可能ですが、「Do While ~ Loop文」は繰り返し条件を柔軟に設定できるため汎用性が高いです。

1、前章の処理を「Do While ~ Loop文」で置き換えてみる

Sub set_demonS_inDo()

    Dim pillar() As String
    Dim table_string As String

    Dim i As Long

    ReDim pillar(0 To 7, 0 To 1)

    Range("A1").Value = "冨岡義勇"
    Range("A2").Value = "煉獄杏寿郎"
    Range("A3").Value = "胡蝶しのぶ"
    Range("A4").Value = "宇随天元"
    Range("A5").Value = "甘露寺蜜璃"
    Range("A6").Value = "時透無一郎"
    Range("A7").Value = "悲鳴嶼行冥"
    Range("A8").Value = "不死川実弥"

    Range("B1").Value = "トミオカギユウ"
    Range("B2").Value = "レンゴクキョウジュウロウ"
    Range("B3").Value = "コチョシノブ"
    Range("B4").Value = "ウズイテンゲン"
    Range("B5").Value = "カンロジミツリ"
    Range("B6").Value = "トキトウムイチロウ"
    Range("B7").Value = "ヒメジマギョウメイ"
    Range("B8").Value = "シナズガワサネミ"
    
    i = 0

    Do While i < 8

        pillar(i, 0) = Cells(i + 1, 1).Value
        pillar(i, 1) = Cells(i + 1, 2).Value

        i = i + 1

    Loop
    
    i = 0

    Do While i < 8

        table_string = table_string & pillar(i, 0) & "," & pillar(i, 1) & vbCrLf

        i = i + 1

    Loop

    MsgBox table_string

    '出力結果:冨岡義勇,トミオカギユウ
    '         煉獄杏寿郎,レンゴクキョウジュウロウ
    '         胡蝶しのぶ,コチョシノブ
    '         宇随天元,ウズイテンゲン
    '         甘露寺蜜璃,カンロジミツリ
    '         時透無一郎,トキトウムイチロウ
    '         悲鳴嶼行冥,ヒメジマギョウメイ
    '         不死川実弥,シナズガワサネミ

End Sub

i)Do While i < 8
iに格納されている値が8を超えない間Loopまでの処理を繰り返す。
正確には「i < 8」がTrueである間Loopまでの処理を繰り返す、です。

ii)i = i + 1
「For~Next文」と違い「Do While~Loop文」を使う場合は、自動でiの値は変化しないため、~Loopまでにiを操作する式が必要です。「i = i + 1」はiに「i + 1」の結果を代入する式です。

2、繰り返し条件を変えて処理する

上記例での出力結果は前章と同じですが、プログラムの行数が増えてしまいました。この例だと「For ~ Next文」の方がシンプルで優れた方法のように見えます。しかし「Do While~Loop文」では以下ようのな条件も設定可能です。

Sub set_demonS_inDo2()

    Dim pillar() As String
    Dim table_string As String

    Dim i As Long

    ReDim pillar(0 To 7, 0 To 1)

    Range("A1").Value = "冨岡義勇"
    Range("A2").Value = "煉獄杏寿郎"
    Range("A3").Value = "胡蝶しのぶ"
    Range("A4").Value = "宇随天元"
    Range("A5").Value = "甘露寺蜜璃"
    Range("A6").Value = "時透無一郎"
    Range("A7").Value = "悲鳴嶼行冥"
    Range("A8").Value = "不死川実弥"

    Range("B1").Value = "トミオカギユウ"
    Range("B2").Value = "レンゴクキョウジュウロウ"
    Range("B3").Value = "コチョシノブ"
    Range("B4").Value = "ウズイテンゲン"
    Range("B5").Value = "カンロジミツリ"
    Range("B6").Value = "トキトウムイチロウ"
    Range("B7").Value = "ヒメジマギョウメイ"
    Range("B8").Value = "シナズガワサネミ"
    
    i = 0

    Do While Cells(i + 1, 1).Value <> ""

        pillar(i, 0) = Cells(i + 1, 1).Value
        pillar(i, 1) = Cells(i + 1, 2).Value

        i = i + 1

    Loop
    
    For i = 0 To UBound(pillar, 1)

        table_string = table_string & pillar(i, 0) & "," & pillar(i, 1) & vbCrLf

    Next

    MsgBox table_string

    '出力結果:冨岡義勇,トミオカギユウ
    '         煉獄杏寿郎,レンゴクキョウジュウロウ
    '         胡蝶しのぶ,コチョシノブ
    '         宇随天元,ウズイテンゲン
    '         甘露寺蜜璃,カンロジミツリ
    '         時透無一郎,トキトウムイチロウ
    '         悲鳴嶼行冥,ヒメジマギョウメイ
    '         不死川実弥,シナズガワサネミ

End Sub

「Do While Cells(i + 1, 1).Value <> ""」はCells(i + 1, 1).Valueに格納された値が空白でない間繰り返す。

序章:まえがきと目次
https://qiita.com/daichi05w/items/002f311490dabaaf14d0

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?