Это моя реализация смешанной базы. Выходные данные выглядят как ваши, кроме заголовков - их нужно добавить.
Итак, представьте счетчик, который выглядит следующим образом - каждая цифра сбрасывается в ноль, когда достигает числа уровней в каждой из ваших переменных:
000000000
000000001
000000002
000000010
000000011
и т. Д.
Это соответствует первым строкам вашей таблицы
8 8 8 8 8 8 8 8 8
8 8 8 8 8 8 8 8 10
8 8 8 8 8 8 8 8 12
8 8 8 8 8 8 8 10 8
8 8 8 8 8 8 8 10 10
Option Explicit
Sub Combinations()
'Set up arrays to define number of levels in each variable and to hold counter.
Dim nValues, startValues, countArray As Variant
nValues = Array(3, 3, 3, 3, 3, 7, 7, 7, 7)
countArray = Array(0, 0, 0, 0, 0, 0, 0, 0, 0)
'Define constants
Const startValue = 8, increment = 2, nCols = 9
'Define variables
Dim row As Long
Dim column, carry As Integer
'Work out number of rows and define an array to hold results
Dim nRows As Variant
nRows = WorksheetFunction.Product(nValues)
Dim holdingArray() As Integer
ReDim holdingArray(nRows, nCols)
'Loop over rows
For row = 0 To nRows - 1
carry = 0
' Loop over columns
For column = 0 To nCols - 1
countArray(column) = countArray(column) + carry
'Check if a 'carry' is needed
If countArray(column) = nValues(column) Then
carry = 1
countArray(column) = 0
Else
carry = 0
End If
'Store results (reverse order of columns)
holdingArray(row, nCols - 1 - column) = startValue + countArray(column) * increment
Next column
'Increment counter
countArray(0) = countArray(0) + 1
Next row
'Transfer array to sheet
Range(Cells(1, 1), Cells(nRows, nCols)) = holdingArray
End Sub
РЕДАКТИРОВАТЬ
Хотя первоначальный ответ дает правильный результат, он лучше работает, если массивы начинаются с 1 (поэтому база вариантов 1). Также я сделал ошибку новичка, думая, что вы можете определить несколько переменных одинакового типа в одном выражении Dim без повторения типа.
Версия 2:
Option Explicit
Option Base 1
Sub Combinations()
'Define constants
Const startValue = 8, increment = 2
'Define variables
Dim row As Long, nRows As Long
Dim column As Integer, carry As Integer, nCols As Integer
'Set up arrays to define number of levels in each variable and to hold counter.
Dim nValues As Variant, countArray As Variant
nValues = Array(3, 3, 3, 3, 3, 7, 7, 7, 7)
nCols = UBound(nValues)
ReDim countArray(nCols)
Debug.Print ("ubound=" & UBound(nValues))
'Work out number of rows and define an array to hold results
nRows = WorksheetFunction.Product(nValues)
Debug.Print ("nrows=" & nRows)
Dim holdingArray() As Integer
ReDim holdingArray(nRows, nCols)
'Loop over rows
For row = 1 To nRows
carry = 0
' Loop over columns
For column = 1 To nCols
countArray(column) = countArray(column) + carry
'Check if a 'carry' is needed
If countArray(column) = nValues(column) Then
carry = 1
countArray(column) = 0
Else
carry = 0
End If
'Store results (reverse order of columns)
holdingArray(row, nCols + 1 - column) = startValue + countArray(column) * increment
Next column
'Increment counter
countArray(1) = countArray(1) + 1
Next row
'Transfer array to sheet
Range(Cells(1, 1), Cells(nRows, nCols)) = holdingArray
End Sub