递归函数调用而不是循环呢?
这是我如何构建数据来回答您的问题(我还没有足够的代表来发布图片,所以这是我使用的表格的图表:
+----------------------------------------------------+
| |一个 |乙| C |
|----------------------------------------|
| 1 |工装裤 | | |
|----------------------------------------|
| 2 |颜色 |腿尺寸 |腰围 |
|----------------------------------------|
| 3 |黑色 | 31 | 32 |
|----------------------------------------|
| 4 |海军 | 33 | 34 |
|----------------------------------------|
| 5 |卡其色 | 35 | 36 |
|----------------------------------------|
| 6 | | | 38 |
|----------------------------------------|
| 7 | | | 40 |
|----------------------------------------|
| 8 | | | 42 |
+----------------------------------------------------+
这里有一段代码,您可以使用它来获取您想要的列表。请注意,它正在寻找空单元格,所以不要跳过。 :)
Option Explicit
Dim HomeSheet As String
Dim NewSheet As String
Function GetAllPermutations()
HomeSheet = ActiveSheet.Name 'Where we start from
NewSheet = Sheets.Add.Name 'Place to put new stuff
Sheets(HomeSheet).Select
RecursivePermutations "", 1, 0
End Function
Function RecursivePermutations(ByVal CurrentValue As String, ByVal SourceColumn As Long, ByRef OutputRow As Long)
Dim x As Long
Dim myText As String
For x = 3 To Cells.Rows.Count
'Is this row's value empty? Then we need to exit
If Cells(x, SourceColumn) = "" Then
If x = 3 Then
'We hit a brand new blank column, time to write it out
OutputRow = OutputRow + 1
Sheets(NewSheet).Cells(OutputRow, 1) = CurrentValue
End If
'All done here
Exit Function
End If
If SourceColumn = 1 Then
'First time through loop for this base value
myText = Trim(Cells(x, SourceColumn))
Else
'Add another one
myText = "|" & Trim(Cells(x, SourceColumn))
End If
'Make recursive call to self
RecursivePermutations CurrentValue & myText, SourceColumn + 1, OutputRow
Next x
End Function