我需要一个不同大小的n的数组,它需要对应给定的列表

时间:2017-02-17 19:06:28

标签: arrays excel vba algorithm

这是我到目前为止所做的,但我不知道该怎么做     `Sub SetUpList()             Dim UnsortedList(1到100000,1到1)为Double             昏昏欲睡             对于i = 1到100000                 未排序的列表(i,1)= Rnd(-i)             下一个我             范围(“A1:A100000”)。value =未排序列表         结束子

    Sub InitializeA()

        Dim i As Long

        n = Cells(2, 2).value
        ReDim A(1 To n)
                For i = 1 To n
            A(i) = Cells(i, 1).value
        Next i
    End Sub

1 个答案:

答案 0 :(得分:0)

以下是将列表中的部分打印到电子表格中的方法。

Option Explicit
'this generates the list and I need to create an array from this list for different sizes of n
Sub SetUpList()
    Dim UnsortedList(1 To 100000, 1 To 1) As Double
    Dim i As Long, N As Long
    Dim A As Variant, R As Range

    For i = 1 To 100000
        UnsortedList(i, 1) = Rnd(-i)
    Next i
    Range("A1:A100000").Value = UnsortedList

    N = Cells(2, 2).Value
    'this allows us to determine the size of the array which will vary because there can be different sizes of n

    Initialize

End Sub

Sub Initialize()
    Dim rDest As Range
    Dim i As Long, N As Long
    Dim A As Variant
    Dim UnsortedList As Variant

UnsortedList = Range("A1", Cells(Rows.Count, "A").End(xlUp))
N = Cells(2, 2)
Set rDest = Range("C1")

ReDim A(1 To N, 1 To 1)
    For i = 1 To N
        A(i, 1) = UnsortedList(i, 1)
    Next i

    Set rDest = rDest.Resize(rowsize:=N)
    rDest.EntireColumn.Clear
    rDest = A
End Sub