1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| Option Explicit
Dim oRng As Excel.Range
Dim lRang As Long
Dim v0 As Variant, vx As Variant
Sub test()
Call subMatrices(4)
End Sub
Sub subMatrices(ByVal n As Integer)
Dim col1 As VBA.Collection, col2 As VBA.Collection
Dim i As Integer, j As Integer
Set col1 = New VBA.Collection
Set col2 = New VBA.Collection
For i = 1 To n
col1.Add i, CStr(i)
Next i
With ThisWorkbook.Worksheets(1)
.UsedRange.ClearContents
Set oRng = .Range(.Cells(1, 1), .Cells(n, n))
End With
lRang = 0
v0 = oRng.Value
For i = 1 To UBound(v0, 1)
For j = 1 To UBound(v0, 2)
v0(i, j) = 0
Next j
Next i
Call subRecursive(col1, col2, n)
oRng.Columns.AutoFit
Set oRng = Nothing
Set col1 = Nothing
Set col2 = Nothing
End Sub
Sub subRecursive(ByVal col1 As VBA.Collection, ByVal col2 As VBA.Collection, ByVal n As Integer)
Dim i As Integer, j As Integer, col3 As VBA.Collection, col4 As VBA.Collection, s As String, nb1 As Integer, v As Variant
Set col3 = New VBA.Collection
Set col4 = New VBA.Collection
For i = 1 To col1.Count
col3.Add col1.Item(i)
Next i
For i = 1 To col2.Count
col4.Add col2.Item(i)
Next i
If col1.Count = 1 Then
col2.Add col1(1), CStr(col2.Count + 1)
vx = v0
For i = 1 To col2.Count
vx(i, col2(i)) = 1
Next i
oRng.Offset(lRang * (n + 1), 0).Value = vx
lRang = lRang + 1
Else
nb1 = col1.Count
For i = 1 To nb1
For j = col1.Count To 1 Step -1
col1.Remove j
Next j
For j = 1 To col3.Count
col1.Add col3.Item(j)
Next j
For j = col2.Count To 1 Step -1
col2.Remove j
Next j
For j = 1 To col4.Count
col2.Add col4.Item(j)
Next j
col2.Add col1.Item(i), CStr(col2.Count + 1)
col1.Remove i
Call subRecursive(col1, col2, n)
Next i
End If
Set col3 = Nothing
Set col4 = Nothing
End Sub |
Partager