Excel VBA-将单元格值添加到数组

时间:2019-06-04 10:25:19

标签: excel vba

我试图在数组中添加一个单元格的值,但是我的代码中出现类型不匹配错误。为什么会这样?

Dim rng As Range
Dim cell As Range
Dim arr As Variant

Set rng = Range("panel_is_on")

For Each cell In rng
    If cell.Value2 = "On" Then
        If cell.Offset(0, -1).Value2 = "ISJ" Or cell.Offset(0, -1).Value2 = "BSJ" Then
            arr(i) = cell.Offset(0, -3).Value2
            i = i + 1
        End If
    End If
Next cell

1 个答案:

答案 0 :(得分:1)

这对我有用:

For Each cell In rng

If cell.Value2 = "On" Then
    If cell.Offset(0, -1).Value2 = "ISJ" Or cell.Offset(0, -1).Value2 = "BSJ" Then
    i = i + 1
    ReDim Preserve arr(1 To i)
    arr(i) = cell.Offset(0, -3).Value2
 End if 
Next cell