該腳本應該將兩組四列的每一個單元組合起來,並在相鄰單元格上打印每個組合。我有過這樣的工作了2套三個格,但現在我得到一個VBA Excel單元格組合腳本1004錯誤
1004應用程序定義或對象定義erorr
它強調:
Set out_1 = Range("I1", Range("L1").Offset(UBound(c1) * UBound(c2) * UBound(c3) * UBound(c7)))
我不是真的很確定如何解決這個問題,誰能幫忙?
Sub Combinations()
Dim c1() As Variant
Dim c2() As Variant
Dim c3() As Variant
Dim c4() As Variant
Dim c5() As Variant
Dim c6() As Variant
Dim c7() As Variant
Dim c8() As Variant
Dim out1() As Variant
Dim out2() As Variant
Dim j, k, l, m, n 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 out_1 As Range
Dim out_2 As Range
Set col1 = Range("A1", Range("A1").End(xlDown))
Set col2 = Range("C1", Range("C1").End(xlDown))
Set col3 = Range("E1", Range("E1").End(xlDown))
Set col4 = Range("B1", Range("B1").End(xlDown))
Set col5 = Range("D1", Range("D1").End(xlDown))
Set col6 = Range("F1", Range("F1").End(xlDown))
Set col7 = Range("G1", Range("G1").End(xlDown))
Set col8 = Range("H1", Range("H1").End(xlDown))
c1 = col1
c2 = col2
c3 = col3
c4 = col4
c5 = col5
c6 = col6
c7 = col7
c8 = col8
Set out_1 = Range("I1", Range("L1").Offset(UBound(c1) * UBound(c2) * UBound(c3) * UBound(c7)))
Set out_2 = Range("M1", Range("P1").Offset(UBound(c4) * UBound(c5) * UBound(c6) * UBound(c8)))
out1 = out_1
out2 = out_2
j = 1
k = 1
l = 1
m = 1
n = 1
Do While j <= UBound(c1)
Do While k <= UBound(c2)
Do While l <= UBound(c3)
Do While m <= UBound(c7)
out1(n, 1) = c1(j, 1)
out1(n, 2) = c2(k, 1)
out1(n, 3) = c3(l, 1)
out1(n, 4) = c7(m, 1)
n = n + 1
m = m + 1
Loop
m = m
l = l + 1
Loop
l = 1
k = k + 1
Loop
k = 1
j = j + 1
Loop
j = 1
k = 1
l = 1
m = 1
n = 1
Do While j <= UBound(c4)
Do While k <= UBound(c5)
Do While l <= UBound(c6)
Do While m <= UBound(c8)
out1(n, 1) = c1(j, 1)
out1(n, 2) = c2(k, 1)
out1(n, 3) = c3(l, 1)
out1(n, 4) = c8(m, 1)
n = n + 1
m = m + 1
Loop
m = m
l = l + 1
Loop
l = 1
k = k + 1
Loop
k = 1
j = j + 1
Loop
out_1.Value = out1
out_2.Value = out2
End Sub
您可能縮短一個位通過創建一個二維陣列,而不是10一個維陣列。這是一個很好的網站,可用於需要審查的代碼:[Code Review](http://codereview.stackexchange.com/)。 – Brian
你確定沒有列是空的,因此使Ubound超過100萬?這會導致偏移量大於Excel表格允許的行數。或者就此而言,所有Ubound的乘數不超過允許的行數? –
我會推薦'debug.print'ing你所有的UBound(cN)來查看這些值是什麼。 – Tim