怎麼樣遞歸函數調用,而不是循環?
這裏是我的結構我的數據來回答你的問題(我還沒有足夠的代表處發佈的圖片,所以這裏是我使用的表的示意圖:
+---------------------------------------+
| | A | B | C |
|---------------------------------------|
| 1 | Cargo Trousers | | |
|---------------------------------------|
| 2 | Colours | Leg Size | Waist |
|---------------------------------------|
| 3 | Black | 31 | 32 |
|---------------------------------------|
| 4 | Navy | 33 | 34 |
|---------------------------------------|
| 5 | Khaki | 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