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?

てすと、(ぶなしめじ)

Last updated at Posted at 2025-03-07

①新しいExcelを開く

②Alt+F11を押す

③「挿入」→「標準モジュール」

④ここに下記を貼り付ける

Sub FindCombinations()
    Dim nums As Variant
    Dim n As Integer, i As Integer, j As Integer
    Dim lastRow As Integer
    Dim rng As Range
    Dim ws As Worksheet
    
    ' アクティブなシートを設定
    Set ws = ActiveSheet
    
    ' A1:A5のデータを配列に格納
    ' ----------------------------------------↓ここを変える!!!
    nums = ws.Range("A1:A10").Value
    n = UBound(nums, 1) ' データの個数
    
    ' 結果の出力先(G列を使用)

    lastRow = 6 ' 6行目列の出力開始行

    ' すべての組み合わせを試す(ビットマスクを使用)
    For i = 1 To (2 ^ n) - 1
        Dim sum As Integer: sum = 0
        Dim comb As String: comb = ""
        
        ' 各数値をチェック
        For j = 0 To n - 1
            If (i And (2 ^ j)) <> 0 Then
                sum = sum + nums(j + 1, 1) ' 選択した数値の合計
                comb = comb & nums(j + 1, 1) & ", " ' 組み合わせを文字列化
            End If
        Next j
        
        ' 合計が10ならB6列に出力
        If sum = 10 Then
            ws.Cells(lastRow, 2).Value = Left(comb, Len(comb) - 2) ' 最後のカンマを削除
            lastRow = lastRow + 1
        End If
    Next i
    
    MsgBox "完了しました!", vbInformation
End Sub

⑤この部分の("A1:A10")に数字が入ってる範囲を書く

qiita.rb
----------------------------------------ここを変える!!!

⑥F5を押して実行

⑦B6より下にけっかがでr

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?