2016-12-30 97 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 
相關問題