2016-12-30 112 views
1

可有人指出,因为我得到一个什么是错在我的代码...运行时错误91 - 对象变量或带块变量未设置

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

在此行中的代码下面

循环while 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: 
+0

您没有初始'Do'来表示循环的开始。 –

+0

对不起,我在代码中做了Do,但忘记在转到此页面时输入它。错误仍然存​​在 – BradleyS

+1

您需要处理这样一个事实,即在您清除它们时,最终不会再找到它。如果C2没有任何内容或者<>为e,并且设置一个布尔值作为循环测试的项目,则需要进行测试。 –

回答

4

由于您首先将“#N/A”放置在单元格中,然后查找它们,在第一阶段直接采取行动不会更简单吗?

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 
相关问题