2
2

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 1 year has passed since last update.

Excelで組み合わせ表を作成するマクロ

Posted at

サンプル

数値サンプル

0,1,2の5つの組み合わせパターン243通りが出力されます。

組み合わせパターン.PNG

文字サンプル

文字でも作成可能です。

組み合わせパターン.2PNG.PNG

使用法

CCWが列数。
v = Array(0, 1, 2)が組み合わせる値。

文字バージョン
v = Array("あり", "なし")

Option Explicit

Public Sub Sample1()
    Dim rng As Range
    Dim v As Variant
    Dim i As Long, j As Long, k As Long
    
    Const CCW As Long = 5 ' 列数
    v = Array(0, 1, 2)    ' 組み合わせの値
    
    k = UBound(v) - LBound(v) + 1
    
    Application.ScreenUpdating = False
    Cells.Delete
    With Range("A1").Offset(, CCW - 1).Resize(k)
        .Value = WorksheetFunction.Transpose(v)
        Set rng = .Cells
    End With
    For j = 1 To CCW - 1
        For i = LBound(v) To UBound(v)
            With rng.Offset((i - LBound(v)) * rng.Rows.Count)
                If (i > LBound(v)) Then rng.Copy .Cells
                .Columns(1).Offset(, -1).Value = v(i)
            End With
        Next
        Set rng = rng.CurrentRegion
    Next
    Application.ScreenUpdating = True
End Sub

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?