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

VBA_組み合わせ出力プログラム(二次元配列)

Last updated at Posted at 2019-01-07

#1.概要
VBAで二次元配列の組み合わせをDebugPrintするサンプルプログラム。
以下のような配列でa,b,cから1つずつ選ぶ全組み合わせを出力する。
 (配列は予めセルに入力しSelectionしておく)
a1 a2 a3 a4
b1 b2 b3 b4
c1 c2 c3 c4
2019-01-07.png

#2.ソースコード


Option Explicit


Sub 組み合わせ()

'---------1.元データ格納処理----------

'以下のような配列でa,b,cから1つずつ選ぶ全組み合わせを出力
'(配列は予めセルに入力しSelectionしておく)
'a1  a2  a3  a4
'b1  b2  b3  b4
'c1  c2  c3  c4

    'Selectionを格納する配列
    Dim rg_selection As Range
    Set rg_selection = Selection
         
    'SelectionのValueを格納、要素数はSelectionを基にRedim
    Dim str_array() As String
    ReDim str_array(rg_selection.Rows.Count - 1, rg_selection.Columns.Count - 1)

    Dim rg As Range    'ForEach用
    Dim i, j As Long
    
    For Each rg In rg_selection
        i = rg.Row - rg_selection.Row
        j = rg.Column - rg_selection.Column
        
        str_array(i, j) = rg.Value
'        Debug.Print str_array(i, j)
    Next
    
'---------2.組み合わせ生成---------

    Dim m As Long '行方向のカウンタ
    Dim n_a, n_b, n_c '列方向のカウンタ
'   Dim n_d As Long '要素dを追加する場合
    
    
    
    
    Dim Buf_() As String '生成した組み合わせのバッファ
    ReDim Buf_(UBound(str_array, 2))
    
'組み合わせ生成ループ
    For n_a = 0 To UBound(str_array, 2)
        m = 0 '0行目を対象
        Buf_(m) = str_array(m, n_a)
                
        For n_b = 0 To UBound(str_array, 2)
            m = 1 '1行目を対象
            Buf_(m) = str_array(m, n_b)
            
            For n_c = 0 To UBound(str_array, 2)
                m = 2 '2行目を対象
                Buf_(m) = str_array(m, n_c)
                Call DebugPrintArray(Buf_)

'要素dを追加する場合
'                For n_d = 0 To UBound(str_array, 2)
'                    m = 3 '3行目を対象
'                        Buf_(m) = str_array(m, n_d)
'                    Call DebugPrintArray(Buf_)
'
'                Next
            Next
        Next
    Next
 
End Sub

'配列のDebugPrint
Function DebugPrintArray(var As Variant)
    
    Dim hoge As Variant
    
    '改行無し、各要素間はスペース
    For Each hoge In var
        Debug.Print hoge + " ";
    Next
    
    '改行する
    Debug.Print
    
End Function



#3.実行結果
a1 b1 c1
a1 b1 c2
a1 b1 c3
a1 b1 c4
a1 b2 c1
a1 b2 c2
a1 b2 c3
a1 b2 c4
a1 b3 c1
a1 b3 c2
a1 b3 c3
a1 b3 c4
a1 b4 c1
a1 b4 c2
a1 b4 c3
a1 b4 c4
a2 b1 c1
a2 b1 c2
a2 b1 c3
a2 b1 c4
a2 b2 c1
a2 b2 c2
a2 b2 c3
a2 b2 c4
a2 b3 c1
a2 b3 c2
a2 b3 c3
a2 b3 c4
a2 b4 c1
a2 b4 c2
a2 b4 c3
a2 b4 c4
a3 b1 c1
a3 b1 c2
a3 b1 c3
a3 b1 c4
a3 b2 c1
a3 b2 c2
a3 b2 c3
a3 b2 c4
a3 b3 c1
a3 b3 c2
a3 b3 c3
a3 b3 c4
a3 b4 c1
a3 b4 c2
a3 b4 c3
a3 b4 c4
a4 b1 c1
a4 b1 c2
a4 b1 c3
a4 b1 c4
a4 b2 c1
a4 b2 c2
a4 b2 c3
a4 b2 c4
a4 b3 c1
a4 b3 c2
a4 b3 c3
a4 b3 c4
a4 b4 c1
a4 b4 c2
a4 b4 c3
a4 b4 c4

1
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
1
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?