如何用VB将excel中的数据导入并定义为一维数组

2024-11-30 23:40:57
推荐回答(2个)
回答(1):

Option Explicit
Dim A(0 To 2414) 'form级变量
Private Sub Command1_Click() '测试结果
Dim i As Long
For i = 0 To 5 '打印几个就行
Debug.Print A(i)
Next
End SubPrivate Sub Form_Load()
Dim i As Long
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(App.Path & "\temp.xls") 'xls文件放在当前目录,你也可以指定目录
Set xlSheet = xlBook.Worksheets

For i = 1 To 2415
A(i - 1) = xlSheet(1).cells(i, 1)
Next

xlApp.DisplayAlerts = False
xlBook.Close
xlApp.Quit
End Sub

回答(2):

'添加一个list ,把数组里面的输出到listSet xlBook = xlApp.Workbooks.Open("d:/11.xls")
xlApp.Visible = False
'获得该工作簿的“sheet1”表
Set xlSheet = xlBook.Sheets("sheet1")

xlSheet.Select
For i = 0 To 2414 a = "a" & i
aa(i) = xlSheet.Range(a)
List1.AddItem aa(i) Next

'释放资源,顺序到过来的,表》》簿》》应用程序
Set xlSheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing