VBA循环通过范围并改变#N / A单元格

时间:2017-11-07 05:45:43

标签: excel vba excel-vba na

我对visual basic相当新鲜。我试图遍历一个列并更改所有#N / A&#39。我希望每隔一个#N / A交替,所以第一个改为1,然后接下来改为2,依此类推。我遇到了一些麻烦,想知道是否有人能让我朝着正确的方向前进。这是我到目前为止所提出的事先感谢!

array (size=2)
  'Savings' => 
    array (size=2)
      132 => int 5
      133 => int 6
  'Coverage' => 
    array (size=2)
      172 => int 7
      173 => int 8

1 个答案:

答案 0 :(得分:0)

为了查看单元格是否包含#NA,您需要捕获错误。

<强>代码

Option Explicit

Sub TruckFilterType()

Dim counter As Long
Dim state As Long
Dim CellVal As Variant

counter = 2
state = 1

With Worksheets("Sheet1")
    For counter = 2 To 100
        ' trap #NA error section
        On Error Resume Next
        CellVal = .Cells(counter, 1).Value2
        On Error GoTo 0
        If IsError(CellVal) Then
            If CellVal = CVErr(xlErrNA) Then ' check if current error if xlErrNA (2042)
                If state = 1 Then
                    .Cells(counter, 1).Value = "TYPE 1"
                    state = 2
                Else
                    .Cells(counter, 1).Value = "TYPE 2"
                    state = 1
                End If
            End If
        End If
    Next counter
End With

End Sub