vb如何把excel一列的值赋给数组

For i = 1 To sum
Dim cellxd(1 To 200) As Single

cellxd(i) = objSheet.Cells(i, 8)

这样??给个例子吧,谢谢!!!

Private Sub Form_Load()
Dim S() As String, i As Integer, j As Integer
Dim cellxd() As Single
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Open("c:\1.xls")'把它改成具体文件名称
Set xlSheet = xlBook.Worksheets("Sheet1")

ReDim cellxd(1 To 200)
For i = 1 To 200
cellxd(i) = xlSheet.Cells(i, 1)
Next

xlBook.Close (True)
xlApp.Quit
Set xlBook = Nothing
Set xlBook = Nothing
Set xlApp = Nothing '释放xlApp对象
MsgBox "已经把第1列的值赋给数组!", vbInformation, "提示"
Unload Me
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-09-17
'先定义数组大小,注意要留有足够的余地不能少于SUM,否则会下标越界
Dim cellxd(1 To 200) As Single
'然后再用循环为它赋值
For i = 1 To sum
cellxd(i) = objSheet.Cells(i, 8)
next i
'然后就可以使用数组了
print cellxd(15)
第2个回答  2008-09-17
你的代码调换一下位置就行了~~变成:
Dim cellxd(1 To 200) As Single
For i = 1 To sum '当然,这个sum不能超过200
cellxd(i) = objSheet.Cells(i, 8)
next