excel排列组合问题有四列,假设分别为A,B,C,D.A列有3个数据,简单假设为1,2,3.B列有27个数据,简单假设

1个回答

  • 用多个 FOR 和 NEXT来实现,比如下面这个

    Sub zuhe()

    Dim a, b, c, d, e

    For a = 1 To 9

    For b = 1 To 7

    For c = 1 To 6

    For d = 1 To 9

    e = a & b

    e = e & c

    e = e & d

    Range("a" & 65536).End(xlUp).Offset(1, 0).Value = e

    Next

    Next

    Next

    Next

    End Sub

    将所有1-9,1-7,1-6,1-9的组合,共计3402个不同组合全部罗列出来

    以下是你需要的宏,要设置a,b,c,d列里面为你要组合出来的数字,最大组合能力262140个 :

    Sub zuhe()

    Dim A, B, C, D

    Dim H, I, J, K, L, M

    A = Application.WorksheetFunction.Max(Range("a:a"))

    B = Application.WorksheetFunction.Max(Range("b:b"))

    C = Application.WorksheetFunction.Max(Range("c:c"))

    D = Application.WorksheetFunction.Max(Range("d:d"))

    For H = 1 To A

    For I = 1 To B

    For J = 1 To C

    For K = 1 To D

    M = M + 1

    L = Range("a" & H).Value & Range("b" & I).Value & Range("c" & J).Value & Range("d" & K).Value

    Range("e1").Value = "组合如下:"

    If M