Excel组合发生器
我有一个代码,根据8列的input生成一个排列,并连接在一起的列。 它迄今为止工作很好,但我想出了一个问题。 它在超过2行被填充时工作。 所以如果只有一行AH中的任何一列的第10行崩溃。 如果第8列只有A,则所有行都填满了A,B,C,那么它就会崩溃
我也试过了
Set col1 = Range(Range("A10"), Range("A" & Rows.Count).End(xlUp))
代替
Set col1 = Range("A10", Range("A10").End(xlDown))
但是那里有一个types不匹配的错误。
任何帮助将是伟大的。 这是整个代码:
Sub combinations() Dim out() As Variant Dim f, g, h, i, j, k, l, m As Long Dim col1 As Range Dim col2 As Range Dim col3 As Range Dim col4 As Range Dim col5 As Range Dim col6 As Range Dim col7 As Range Dim col8 As Range Dim out1 As Range 'Set col1 = Range("A10", Range("A10").End(xlDown)) Set col1 = Range(Range("A10"), Range("A" & Rows.Count).End(xlUp)) Set col2 = Range("B10", Range("B10").End(xlDown)) Set col3 = Range("C10", Range("C10").End(xlDown)) Set col4 = Range("D10", Range("D10").End(xlDown)) Set col5 = Range("E10", Range("E10").End(xlDown)) Set col6 = Range("F10", Range("F10").End(xlDown)) Set col7 = Range("G10", Range("G10").End(xlDown)) Set col8 = Range("H10", Range("H10").End(xlDown)) c1 = col1 c2 = col2 c3 = col3 c4 = col4 c5 = col5 c6 = col6 c7 = col7 c8 = col8 'initializes each column from column1-column8 as Range, sets the size of the range from row10 to last row Set out1 = Range("M1", Range("T1").Offset(UBound(c1) * UBound(c2) * UBound(c3) * UBound(c4) * UBound(c5) * UBound(c6) * UBound(c7) * UBound(c8))) out = out1 'creates a range for the output f = 1 g = 1 h = 1 i = 1 j = 1 k = 1 l = 1 m = 1 n = 1 Do While f <= UBound(c1) Do While g <= UBound(c2) Do While h <= UBound(c3) Do While i <= UBound(c4) Do While j <= UBound(c5) Do While k <= UBound(c6) Do While l <= UBound(c7) Do While m <= UBound(c8) out(n, 1) = c1(f, 1) out(n, 2) = c2(g, 1) out(n, 3) = c3(h, 1) out(n, 4) = c4(i, 1) out(n, 5) = c1(j, 1) out(n, 6) = c2(k, 1) out(n, 7) = c3(l, 1) out(n, 8) = c4(m, 1) 'goes down one column and grabs each cells value n = n + 1 m = m + 1 Loop m = 1 l = l + 1 Loop l = 1 k = k + 1 Loop k = 1 j = j + 1 Loop j = 1 i = i + 1 Loop i = 1 h = h + 1 Loop h = 1 g = g + 1 Loop g = 1 f = f + 1 Loop 'repeats process for all 8 columns out1.Value = out 'places values in the output range "out1" Dim LastRow As Long LastRow = Cells(Rows.Count, "M").End(xlUp).Row 'Range("Z1:Z" & LastRow).Formula = "=M1 & "" | "" & N1 & "" | "" & O1 & "" | "" & P1 & "" | "" & Q1 & "" | "" & R1 & "" | "" & S1 & "" | "" & T1 " Range("Z1:Z" & LastRow).Formula = "=M1 & $F$3 & N1 & $F$3 & O1 & $F$3 & P1 & $F$3 & Q1 & $F$3 & R1 & $F$3 & S1 & $F$3 & T1 " 'concatentates the cells from column MT, seperated by the delimiter in cell F3 Range("Z1").Select Range(Selection, Selection.End(xlDown)).Select Selection.Copy Range("A1").Select Sheets("Sheet2").Select Columns("F").ColumnWidth = 120 Range("F2").Select Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Range("A1").Select 'Copies the concatenated output, pastes in sheet2 as values End Sub
你有多个问题:
Set col1 = Range("A10", Range("A10").End(xlDown)) c1 = col1
如果col1只有第10行填充,则此序列的结果是c1是具有维(1到1048567,1到1)的variables数组,
更好的是:
Set col1 = Range("A10", Cells(Rows.Count, "A").End(xlUp))
但是,与此同时,只有一个单元格填充在列中,C1将不再是一个数组。
因此,保持大部分algorithm的一个解决scheme是使用这个序列来设置你的列和variables数组:
Dim c1, c2, c3, c4, c5, c6, c7, c8 Dim col1 As Range Dim col2 As Range Dim col3 As Range Dim col4 As Range Dim col5 As Range Dim col6 As Range Dim col7 As Range Dim col8 As Range Dim out1 As Range 'Set col1 = Range("A10", Range("A10").End(xlDown)) Set col1 = Range("A10", Cells(Rows.Count, "A").End(xlUp)) Set col2 = Range("B10", Cells(Rows.Count, "b").End(xlUp)) Set col3 = Range("C10", Cells(Rows.Count, "c").End(xlUp)) Set col4 = Range("D10", Cells(Rows.Count, "d").End(xlUp)) Set col5 = Range("E10", Cells(Rows.Count, "e").End(xlUp)) Set col6 = Range("F10", Cells(Rows.Count, "f").End(xlUp)) Set col7 = Range("G10", Cells(Rows.Count, "g").End(xlUp)) Set col8 = Range("H10", Cells(Rows.Count, "h").End(xlUp)) c1 = col1 If Not IsArray(c1) Then ReDim c1(1, 1) c1(1, 1) = col1.Value End If c2 = col2 If Not IsArray(c2) Then ReDim c2(1, 1) c2(1, 1) = col1.Value End If c3 = col3 If Not IsArray(c3) Then ReDim c3(1, 1) c3(1, 1) = col1.Value End If c4 = col4 If Not IsArray(c4) Then ReDim c4(1, 1) c4(1, 1) = col1.Value End If c5 = col5 If Not IsArray(c5) Then ReDim c5(1, 1) c5(1, 1) = col1.Value End If c6 = col6 If Not IsArray(c6) Then ReDim c6(1, 1) c6(1, 1) = col1.Value End If c7 = col7 If Not IsArray(c7) Then ReDim c7(1, 1) c7(1, 1) = col1.Value End If c8 = col8 If Not IsArray(c8) Then ReDim c8(1, 1) c8(1, 1) = col1.Value End If
最后,你应该在VB编辑器中设置要求variables声明的选项。 这将在任何新模块的开始处放置Option Explicit,并确保您不仅声明所有variables(不在此代码中),而且还将帮助避免input错误。