Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在excel中查找所有可能的组合_Excel_Vba_Excel Formula_Excel 2010_Excel 2007 - Fatal编程技术网

在excel中查找所有可能的组合

在excel中查找所有可能的组合,excel,vba,excel-formula,excel-2010,excel-2007,Excel,Vba,Excel Formula,Excel 2010,Excel 2007,我有这样的excel数据 X Y a 1 b 2 c 3 我想在X列中进行所有可能的组合,并从Y列生成和 a+b 3 a+c 4 a+b+c 6 b+c 5 此示例在即时窗口中生成输出。根据您的输出需求进行调整 Option Explicit Public Sub Combine() Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("Sheet1") Dim

我有这样的excel数据

X    Y

a    1
b    2 
c    3
我想在X列中进行所有可能的组合,并从Y列生成和

a+b    3
a+c    4
a+b+c  6
b+c    5

此示例在即时窗口中生成输出。根据您的输出需求进行调整

Option Explicit

Public Sub Combine()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1")

    Dim LastRow As Long
    LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    Dim iRow As Long, jRow As Long, kRow As Long
    For iRow = 2 To LastRow
        For jRow = iRow + 1 To LastRow

            'output 2-combos
            Debug.Print ws.Cells(iRow, "A").Value & "+" & ws.Cells(jRow, "A").Value, ws.Cells(iRow, "B").Value + ws.Cells(jRow, "B").Value

            For kRow = jRow + 1 To LastRow
                'output 3-combos
                Debug.Print ws.Cells(iRow, "A").Value & "+" & ws.Cells(jRow, "A").Value & "+" & ws.Cells(kRow, "A").Value, ws.Cells(iRow, "B").Value + ws.Cells(jRow, "B").Value + ws.Cells(kRow, "B").Value
            Next kRow
        Next jRow
    Next iRow
End Sub
所以这…

将输出

a+b            3 
a+b+c          6 
a+b+d          7 
a+c            4 
a+c+d          8 
a+d            5 
b+c            5 
b+c+d          9 
b+d            6 
c+d            7 

你看过这里吗?我确信这类问题已经被提出并得到了回答。。。到目前为止,你尝试了什么?你计划做多少次?因为这将由VBA计算,直到大约8个X,然后它将超过时间和内存,并且由于需要计算的置换太多而变得不可能。其实我对excel不太了解@太阳能麦克andreas@AnkitSagar请你回答我的问题好吗?因为如果你计划8个或更多的X,这是不可能的。如果您计划少于8,请从“我的评论”中的链接开始。@Pᴇʜ不止8个,附近有什么吗?