【发布时间】:2016-11-23 21:24:10
【问题描述】:
我正在用 VBA 编写一个代码,它遍历每一行的几列,并找到每两行之间的差异并将该值存储在一个数组中。我在 for 循环中添加了一个 if 语句,以避免在操作中包含空白单元格,它适用于前五列,但不适用于第六列。
Dim Datarray As Variant
Dim LR As Long, LR1 As Long
Dim p As Integer, i As Integer
LR1=LR-1
ReDim R(LR1) As Variant
For i = 1 To DateTime_Column
LR = Cells(Rows.Count, i).End(xlUp).Row
Datarray = Range(Cells(1, i), Cells(LR, i))
For p = 2 To LR1
If Datarray(p, 1) And Datarray(p + 1, 1) <> Empty Then
R(p) = Abs(Datarray(p + 1, 1) - Datarray(p, 1))
End If
Next p
Rave = WorksheetFunction.Average(R) ***Error occurs here because R is empty***
Next i
有没有人发现我的 if 语句的编写方式有什么问题?代码似乎认为第六列中的单元格不是空的。我检查了 Dataarray,每个点都有值。
【问题讨论】:
-
不应该计算
LR和LR1之前ReDim语句?现在看来R的尺寸为零元素。此外,在代码顶部添加Option Explicit语句可能允许编译器标记其他问题。
标签: arrays excel for-loop conditional vba