将工作表数据复制到数组时出错

时间:2013-02-14 04:17:49

标签: excel-vba vba excel

我需要将Excel范围内的数据复制到数组中。我使用以下代码,但它显示错误“预期数组。”

  Dim filename As String
  Dim arraysize As Integer

  arraysize = 50
  I = 1
  Do Until ThisWorkbook.Sheets("sheet1").Cells(I, 1).Value = ""   
     filename(arraysize) = ThisWorkbook.Sheets("sheet1").Cells(I, 1).Value    
     I = I + 1
  Loop

2 个答案:

答案 0 :(得分:1)

试试这个

Sub Demo()
    Dim filename As Variant
    Dim arraysize As Long
    Dim rng As Range
    Dim i As Long

    i = 1
    With ThisWorkbook.Sheets("sheet1")
        Set rng = .Range(.Cells(i, 1), .Cells(i, 1).End(xlDown))
    End With

    ' load as two dimensional array
    filename = rng.Value

    ' transform into 1 dimensional array
    filename = Application.Transpose(filename)

    arraysize = UBound(filename)
End Sub

答案 1 :(得分:-1)

您尚未声明数组。我无法想到我头顶的代码,但这应该指向正确的方向。

Dim Filename(50)

Do
    Filename(I)= excel.row
Loop