运行时错误91 - 对象变量或未设置块变量

时间:2016-12-30 15:49:44

标签: excel vba excel-vba

有人可以指出我的代码中有什么问题,因为我得到了......

  

运行时错误91 - 对象变量或未设置块变量

在下面的代码中的这一行

  

循环时c2.Address<> ë

Dim Cell As Range
Dim SrchRng2 As Range
Dim c2 As Range, e As String

'Check each row in column - if BLUE text (set by CF) change to #N/A
For Each Cell In Intersect(Columns("E"), ActiveSheet.UsedRange)
    If Cell.DisplayFormat.Font.ColorIndex = 5 Then Cell.Value = "#N/A"
Next
On Error GoTo NoBlueText
'Search column E for cells with #N/A and clear cells across columns E:G in row
Set SrchRng2 = ActiveSheet.Range("E2", ActiveSheet.Range("E" & Rows.Count).End(xlUp))
Set c2 = SrchRng2.Find("#N/A", LookIn:=xlValues)

If Not c2 Is Nothing Then
    e = c2.Address
Do
            ActiveSheet.Range("E" & c2.Row & ":G" & c2.Row).Cells.ClearContents
        Set c2 = SrchRng2.FindNext(c2)
    Loop While c2.Address <> e
End If

NoBlueText:

1 个答案:

答案 0 :(得分:4)

因为您首先要放置&#34;#N / A&#34;在细胞中然后寻找它们,在第一阶段直接行动会不会更简单?

With ActiveSheet
    With .Range("E2", .Cells(.Rows.count, "E").End(xlUp))
        For Each cell In .Cells
            If cell.DisplayFormat.Font.ColorIndex = 5 Then cell.Resize(, 3).ClearContents
        Next
    End With
End With