【发布时间】:2018-05-26 12:15:36
【问题描述】:
我有一个 VBA 程序,它应该创建并写出多达 7 个不同变量的完整组合,每个变量都有不同的级别。
代码循环然后所有组合并在每个变量之间用空格写入它们。它首先按最后一行 (LineP) 组织,然后从第一行到最后一行 (Line1 到 Line 6)。
到目前为止,代码仍然有效,除了如果用户将一行留空,它会假定没有组合,因为数组是空的。 我可以通过将 数组定义为 " " 来解决这个问题,但是这样会在组合中的变量之间留下额外的两个空格。 代码现在的工作方式不仅涉及不在变量的位置写入任何内容,而且还涉及删除空间。
每个变量的不同级别存储在一个数组中(变量 1 的级别在 Array1 中,变量 P 的级别在 ArrayP 中,等等)。下面是我目前用来写出每个组合的代码:
`'Create Label Combinations
If Rowi > 1 Then
Dim Labeli As String
Dim Rowi2 As Integer
Rowi2 = Rowi
If P = 1 Then
For iP = 0 To UBound(ArrayP)
For i1 = 0 To UBound(Array1)
For i2 = 0 To UBound(Array2)
For i3 = 0 To UBound(Array3)
For i4 = 0 To UBound(Array4)
For i5 = 0 To UBound(Array5)
For i6 = 0 To UBound(Array6)
Labeli = Array1(i1) & " " & Array2(i2) & _
" " & Array3(i3) & " " & _
Array4(i4) & " " & Array5(i5) & _
" " & Array6(i6) & " " & ArrayP(iP)
Cells(Rowi2, 1).Value = Labeli
Rowi2 = Rowi2 + 1
Next i6
Next i5
Next i4
Next i3
Next i2
Next i1
Next iP
End If
End If`
这里是当前输出的一个例子:
由于每次使用的变量数量和每个变量的级别都会发生变化,我不确定是否可以使用多维数组来解决这个问题。我认为可以在“Labeli”字符串中嵌入一个 if 语句,但我没有发现任何暗示这是可能的。任何帮助将非常感激。谢谢!
【问题讨论】:
标签: excel combinations vba