进行组合并检查每个可行的组合

时间:2018-10-30 09:05:51

标签: excel vba excel-vba

我的(A)列中有值

| 3, 4, 5|
|2, 4, 5|
|4, 5|
|2, 3|
|5|
|4|
|3|

以及集合{2,3,4,5}中所有其他可能的3,2和1点组合。我希望这些元素之间所有可能的组合,以便集合中没有数字重复,即|3, 4, 5|仅可以与|2|组合,或者|3, 4|仅可以与|2, 5|组合或|2||5|

Dim lRowCount As Long
Dim temp As String, s As String
Dim arrLength As Long
Dim hasElement As Boolean
Dim plans() As String, currentPlan() As String
Dim locationCount As Long
Dim currentRoutes As String
Dim line As Long

Worksheets("Sheet1").Activate
Application.ActiveSheet.UsedRange
lRowCount = ActiveSheet.UsedRange.Rows.Count
locationCount = -1
line = 2

Debug.Print ("*********")

For K = 2 To lRowCount - 1
    currentRoutes = ""
    For i = K To lRowCount
        s = ActiveSheet.Cells(i, 1)
        Do
            temp = s
            s = Replace(s, " ", "")
        Loop Until temp = s
        currentPlan = Split(Trim(s), ",")
        arrLength = UBound(currentPlan) - LBound(currentPlan) + 1
        hasElement = False

        If Len(Join(plans)) > 0 Then
            For j = 0 To arrLength - 1
                pos = Application.Match(currentPlan(j), plans, False)

                If Not IsError(pos) Then
                    hasElement = True
                    Exit For
                End If
            Next j
        End If
        If Not hasElement Then
            currentRoutes = currentRoutes & (Join(currentPlan, ",")) & " "
            If Len(Join(plans)) > 0 Then
                plans = Split(Join(plans, ",") & "," & Join(currentPlan, ","), ",")
            Else
                plans = currentPlan
            End If
        End If
    Next i
If locationCount < 0 Then
    locationCount = UBound(plans) - LBound(plans) + 1
End If

If (UBound(plans) - LBound(plans) + 1) < locationCount Then
    Debug.Print ("Invalid selection")
Else
    Debug.Print (Trim(currentRoutes))
    Worksheets("Sheet1").Cells(line, 11) = currentRoutes
    line = line + 1
End If

Erase plans
Debug.Print ("*********")
Next K

当前问题是,如果选择了组合|3,4|,则仅检查下一个可能的组合,即,它需要|3,4||2,5|但不检查{{1 }}和|2|。请帮助我解决这个问题。

2 个答案:

答案 0 :(得分:0)

这是一个简短的代码,可以从一组中生成所有组合

Sub test()
    Dim a As String
    a = "2345"
    combine a, 3 'list all possible combinations of 3 characters from string a
End Sub
Sub combine(a As String, numberofvalues As Integer, Optional level As Long = 1, Optional solution As String = "", Optional firsti As Long = 1, Optional combinationcount As Long = 0)
    Dim i As Long
    For i = firsti To Len(a)
        solution = solution & Mid(a, i, 1)
        combinationcount = combinationcount + 1
        Sheets("sheet1").Cells(combinationcount, 1) = solution
        If level < numberofvalues Then
            combine a, numberofvalues, level + 1, solution, i + 1, combinationcount
        End If
        solution = Left(solution, Len(solution) - 1)
    Next i
End Sub

答案 1 :(得分:0)

所以我不明白您的要求。

我希望我的理解是正确的。

<div class="table">
  <?php
  $args = array( 'post_type' => 'Team', 'posts_per_page' => 20 );
  $loop = new WP_Query( $args );
  `while ( $loop->have_posts() ) : $loop->the_post();
  ?>
  endwhile
  ?>
  <table class="tb1" style="width:50%">
    <tr>
      <th > Name</th>
      <th >Email</th>
      <th >Phon no</th>
      <th >Designation</th>
    </tr>
    <tr>
      <td ><?php  echo get_field('name');?></td>
      <td ><?php  echo get_field('email');?></td>
      <td><?php  echo get_field('phon_no');?></td>
      <td><?php  echo get_field('designation');?></td>
    </tr>
  </table>