VBA 動態二維陣列資料無法傳入

小弟今天想把活頁簿中的資料傳入陣列中以方便後面的運算,

但是發現執行到下方部份時,

資料完全無法傳入陣列中,

不知是發生什麼問題?

並且在於動態陣列重新宣告的部分,在第一次執行時都會成功,

但是第二次開始就會說超出陣列的訊息,也請各位大大幫助

Dim piecearray() As String

Dim numbertray As Integer

Windows("book12.xls").Activate

Sheets("tape piece").Select

manyX = 1

Do

ReDim Preserve piecearray(numbertray+1,3)

piecearray(numbertray, 1) = Cells(manyX, 1)

piecearray(numbertray, 2) = Cells(manyX, 3)

numbertray = numbertray + 1

Cells(numbertray, 10) = piecearray(numbertray, 1)

manyX = manyX + 1

Loop While Cells(manyX + 1, 1) <> ""

1 Answer

Rating
  • Anonymous
    10 years ago
    Favorite Answer

    因為 ReDim Preserve 只能改變最後一維大小所以, 初始(第一次)跑過時沒問題,但第二次再經過時, 第一維度不能被改, 因此出錯 ...換言之. 版大需把動態的維度放後面 ~---

    範例 :

    Sub fdsa()

    Dim I As Integer

    Dim X()

    For I = 0 To 3

    ReDim Preserve X(1, I)

    X(0, I) = I

    X(1, I) = I * 2

    Next

    [A1].Resize(2, 3) = X

    End Sub---

    先試試再說 ~

Still have questions? Get your answers by asking now.