我需要獲得所有可能的組合列表,而不是排列組合。所有可能的組合
爲了確保我有正確的名字,123和321對我來說是一回事,應該只列出一次。
下面的代碼做我需要但我不能將其轉換爲MS Access vba。
對不起,我知道這是基本的,它已被問及一百萬次,但我找不到任何適用於我的MS Access。
Sub test_print_nCr()
print_nCr 7, 3, Range("A1")
End Sub
2.
Public Function print_nCr(n As Integer, r As Integer, p As Range)
c = 1
internal_print_nCr n, r, p, 1, 1
End Function
3.
Public Function internal_print_nCr(n As Integer, r As Integer, ByVal p As Range, Optional i As Integer, Optional l As Integer) As Integer
' n is the number of items we are choosing from
' r is the number of items to choose
' p is the upper corner of the output range
' i is the minimum item we are allowed to pick
' l is how many levels we are in to the choosing
' c is the complete set we are working on
If n < 1 Or r > n Or r < 0 Then Err.Raise 1
If i < 1 Then i = 1
If l < 1 Then l = 1
If c < 1 Then c = 1
If r = 0 Then
p = 1
Exit Function
End If
Dim x As Integer
Dim y As Integer
For x = i To n - r + 1
If r = 1 Then
If c > 1 Then
For y = 0 To l - 2
If p.Offset(c - 1, y) = "" Then p.Offset(c - 1, y) = p.Offset(c - 2, y)
Next
End If
p.Offset(c - 1, l - 1) = x
c = c + 1
Else
p.Offset(c - 1, l - 1) = x
internal_print_nCr n, r - 1, p, x + 1, l + 1
End If
Next
End Function
再次感謝您
你怎麼能不把它轉換?這看起來像VBA代碼 - 你會得到錯誤嗎?如果是這樣,錯誤文本是什麼,以及哪一行是拋出錯誤?如果沒有錯誤,當您在調試器中完成時發生了什麼?換句話說,什麼是意外行爲? – 2013-03-22 01:07:19
來自Excel工作簿的代碼並使用單元格來存儲數據。不適合從Access功能開始。 – grantnz 2013-03-22 01:17:47
是的,我應該提到,對不起,這是excel代碼,它複製工作簿中的值。我只需要能夠將值傳遞給一個變量。 – lalachka 2013-03-22 01:31:17