在阵列VBA中访问列

时间:2017-07-31 18:38:26

标签: arrays excel vba excel-vba

我的代码首先分配工作表并抓取行数

Set ws2 = ThisWorkbook.Worksheets("Sheet2")
lastRow22 As Long
lastRow22 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row

我有以下内容将整个列放入第1列到第80列的数组

Dim arrEntireWs2() as Variant  
With ws2
    arrEntireWs2 = .Range(.Cells(2,1),.Cells(lastRow22,80)).Value
End With

然后我循环了

Dim lngArrEntireWs2Index as Long
For lngArrEntireWs2Index = LBound(arrEntireWs2,1) to Ubound(arrEntireWs2,1)
    'Things I want to do
Next lngArrEntireWs2Index

我的问题是如何获取它循环的行上某列的值?就像我在经历循环时如何抓住第10列上的内容一样?

1 个答案:

答案 0 :(得分:1)

这就是你要找的......

arrayIndexOutOfBound

对我来说这看起来更好

Dim lngArrEntireWs2Index as Long
For lngArrEntireWs2Index = LBound(arrEntireWs2,1) to Ubound(arrEntireWs2,1)
     If arrEntireWs2(lngArrEntireWs2Index, 10) Then
          debug.print; arrEntireWs2(lngArrEntireWs2Index, 10)
     End if
Next lngArrEntireWs2Index