我的部分访问代码是从现有的Excel文件中读取数据。我已经能够弄清楚如何去做,但是需要很长时间才能完成。 excel文件有大约400行,我只是从两列中获取数据并将其放入多维数组中。
Dim excelapp As Excel.Application
Dim strBxNum As String: strBxNum = "bx" & strInd
Dim i As Integer
Dim r As Integer
Set excelapp = CreateObject("Excel.Application")
Dim WkBk As Excel.Workbook
Set WkBk = excelapp.Workbooks.Open(fileName:=strFileName)
i = 6
r = 0
Do While WkBk.Sheets(3).Range("C" & i) <> ""
strWriteUp(r, 0) = WkBk.Sheets(3).Range("C" & i)
strWriteUp(r, 1) = WkBk.Sheets(3).Range("P" & i)
i = i + 1
r = r + 1
Loop
有更有效的方法吗?
答案 0 :(得分:1)
逐个单元格操作往往比使用数组一次读取范围要慢得多(特别是跨应用程序边界)。
E.g。
Dim excelapp As Excel.Application
Dim strBxNum As String: strBxNum = "bx" & strInd
Dim i As Long, lr As Long
Dim WkBk As Excel.Workbook, arrC, arrP
Set excelapp = CreateObject("Excel.Application")
Set WkBk = excelapp.Workbooks.Open(fileName:=strFileName)
i = 6
With WkBk.Sheets(3)
lr = .Cells(.Rows.Count, "C").End(xlUp).Row
arrC = .Range(.cells(i, "C"), .Cells(lr, "C") ).Value
arrP = .Range(.cells(i, "P"), .Cells(lr, "P") ).Value
End With
这将为您提供两个基于1的2-D阵列(1到#rows,1对1),您可以按原样使用,或者循环并组合成单个2-D阵列。
编辑将arrP放入arrC:
Dim n As Long, nR as Long
nR = ubound(arrC, 1)
ReDim Preserve arrC(1 to nR, 1 to 2)
For n = 1 to nR
arrC(n, 2) = arrP(n, 1)
next n