2017-02-17 25 views
0

這是我迄今爲止所得到的,但我不知道該怎麼辦 `Sub SetUpList() Dim UnsortedList (1至100000,1至1)As Double Dim i As Long For i = 1 To 100000 UnsortedList(i,1)= Rnd(-i) Next i Range(「A1:A100000」)。value = UnsortedList 結束子我需要一個從n的不同大小vaires的數組,它需要對應給定的列表

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 
+0

爲什麼不按照需要調整'範圍'的大小,然後將所有值一次拉入數組? – Comintern

+2

歡迎來到StackOverflow!請訪問該頁面:[如何提出一個很好的問題](http://stackoverflow.com/help/how-to-ask),因爲你的文章沒有描述這個問題,而且這裏也沒有提出明確的問題。 –

+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 
+0

@RonRosenfield謝謝你,我的代碼完成了我現在想要的工作。我真的很感謝你的幫助 – Mark

+0

@JonathanAkinrele很高興幫助。如果您可以將我的回覆標記爲答案,我將不勝感激。 –

相關問題