将数字发送到excel中的其他单元格

时间:2017-09-29 20:52:05

标签: excel excel-vba vba

好吧,我写了一个代码,用于获取4位数字的所有可能组合 并且运作良好,但我希望将每个数字发送到对面的单元格

replace_by=['a1','b1','c1','d1','e1']
main_list=[('a',[]),('b',[]),('c',[]),('d',[]),('e',[])]

new_list = [(replace_by[i],b)for (i,(a,b)) in enumerate(main_list)]
print(new_list)

并且效果很好,但我希望将每个数字发送到相对的单元格 像这样 enter image description here

编写此代码来执行该操作

Dim N As Long, ary(1 To 4) As Long
N = Application.InputBox(prompt:="enter value", Type:=1)
xxx = 1

For i = 0 To N
    For J = 0 To N
        For k = 0 To N
            For l = 0 To N
                If i + J + k + l = N Then
                    ary(1) = i
                    ary(2) = J
                    ary(3) = k
                    ary(4) = l
                    Call LittleSort(ary())
                    Cells(xxx, 1).Value = "'" & ary(1) & ary(2) & ary(3) & ary(4)
                    xxx = xxx + 1
                End If
            Next l
        Next k
    Next J
Next i

Range("A:A").RemoveDuplicates Columns:=1, Header:=xlNo
End Sub

Public Sub LittleSort(ByRef InOut)

Dim i As Long, J As Long, Low As Long
Dim Hi As Long, Temp As Variant

Low = LBound(InOut)
Hi = UBound(InOut)

J = (Hi - Low + 1) \ 2
Do While J > 0
    For i = Low To Hi - J
      If InOut(i) > InOut(i + J) Then
        Temp = InOut(i)
        InOut(i) = InOut(i + J)
        InOut(i + J) = Temp
      End If
    Next i
    For i = Hi - J To Low Step -1
      If InOut(i) > InOut(i + J) Then
        Temp = InOut(i)
        InOut(i) = InOut(i + J)
        InOut(i + J) = Temp
      End If
    Next i
    J = J \ 2
Loop
End Sub

我的问题是我的4个数字被分为5个数字

enter image description here

我该怎么做?

0 个答案:

没有答案