跨多个行和列的组合脚本

时间:2016-07-01 19:46:50

标签: excel vba

Could not post the image so here is the link for sample data. 我在7列(a-g)和69行中有一组数据,在这些行的每个单元格中都有数字。我是编码的新手,并在这里使用了一个脚本来组合单元格中的值。我已将其扩展到5但我需要的是将69行(a1-g1,a2-g2等)中的每一行中的一行视为一组数据并运行其中一个的每个可能组合在任何给定单元格中放置4个不同选项的行。选项是日期。所以结果在单元格a1-g1中,我需要它们来匹配例如1/1/2000的日期。我认为它的846000和改变。我使用下面的脚本将一个单元格与一个单元格进行比较。现在我需要一行到一个单元格。我希望我尽可能清楚地解释它。

更新:我已经将所有行连接成一个单元格。现在我有3列而不是9列。其中一列是我的数据,一列是日期,另一张是id。我相信编辑下面的脚本3而不是4应该给我一个结果。

更新2:我不能只在我的第3列中放入一个值,并仅为该选项进行复制。

子组合()

Dim c1() As Variant
Dim c2() As Variant
Dim c3() As Variant
Dim c4() As Variant

Dim out() As Variant
Dim j As Long, k As Long, l As Long, m As Long, n As Long


Dim col1 As Range
Dim col2 As Range
Dim col3 As Range
Dim col4 As Range

Dim out1 As Range


Set col1 = Range("A1", Range("A1").End(xlDown))
Set col2 = Range("B1", Range("B1").End(xlDown))
Set col3 = Range("C1", Range("C1").End(xlDown))
Set col4 = Range("D1", Range("D1").End(xlDown))

c1 = col1
c2 = col2
c3 = col3
c4 = col4

Set out1 = Range("G2", Range("K2").Offset(UBound(c1) * UBound(c2) * UBound(c3) * UBound(c4)))
out = out1

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(c4)
                out(n, 1) = c1(j, 1)
                out(n, 2) = c2(k, 1)
                out(n, 3) = c3(l, 1)
                out(n, 4) = c4(m, 1)
                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


out1.Value = out

End Sub

0 个答案:

没有答案