2012-01-08 310 views
1

我對VBA不是很熟悉,但需要更改我的excel以允許超過3個條件格式。VBA錯誤13類型:不匹配

我在網上找到了下面的代碼,想根據內容改變單元格的顏色並選擇六個不同的值。

我的代碼是:要麼從下拉列表否則在BT和BL都寫在檢查這些行會突出顯示選擇

Private Sub Worksheet_Change(ByVal Target As Range) 
    Set MyPlage = Range("G3:AG115") 

    For Each Cell In MyPlage 
    If Cell.Value = "." Then 
     Cell.Interior.ColorIndex=28 
     Cell.Font.Bold = True 
    End If 

    If Cell.Value = "X1" Then 
     Cell.Interior.ColorIndex=32 
     Cell.Font.Bold = True 
    End If 

    If Cell.Value = "1X" Then 
     Cell.Interior.ColorIndex=6 
     Cell.Font.Bold = True 
    End If 

    If Cell.Value = "2X" Then 
     Cell.Interior.ColorIndex=45 
     Cell.Font.Bold = True 
    End If 

    If Cell.Value = "3X" Then 
     Cell.Interior.ColorIndex=4 
     Cell.Font.Bold = True 
    End If 

    If Cell.Value = "XY" Then 
     Cell.Interior.ColorIndex=44 
     Cell.Font.Bold = True 
    End If 

    If Cell.Value = "bt" Then 
     Cell.Font.ColorIndex=27 
     Cell.Interior.ColorIndex=27 
    End If 

    If Cell.Value = "bl" Then 
     Cell.Font.ColorIndex=28 
     Cell.Interior.ColorIndex=28 
    End If 

    If Cell.Value <> "bt" And Cell.Value <> "bl" And Cell Value <> "." And Cell.Value <> "X1" And Cell.Value <> "1X" And Cell.Value <> "2X" And Cell.Value <> "3X" And Cell.Value <> "XY" Then 
     Cell.Interior.ColorIndex=xlNone 
    End If 
    Next 
End Sub 

內容。

當我嘗試更改內容時,我收到Error: 13 Type Mismatch

If Cell.Value = "." Then 

被突出顯示爲錯誤源(我想這個問題可能是與"."但如果我刪除的指令集,則行

If Cell.Value = "X1" Then 

高亮顯示)

我用Google搜索了一下,看到你可以做一個循環,如果錯誤Then Next,我不知道我會如何編寫這個確切的,我犯了d比解決編碼問題要快得多。

如果任何人有任何想法我會去哪裏錯/解決方案將是偉大的。

+1

什麼是細胞那會導致這個錯誤?我嘗試過使用隱式和顯式變量聲明的各種類型的內容,但是我不能重現這個錯誤。 – GSerg 2012-01-08 11:22:56

+0

我認爲你使用的xl03限於3種條件格式(儘管有解決方法)? – brettdj 2012-01-08 12:48:34

+1

這不是你的問題的答案,但爲什麼檢查'For Each Cell In MyPlage',而你可以檢查'For Each Cell In Target'? – 2012-01-08 14:46:40

回答

2

編輯

如果你有在(如#NA#DIV/0等)的任何誤差值,則If Cell...線將失敗

將其更改爲

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim Cell as Range 
    Set MyPlage = Range("G3:AG115") 
    For Each Cell In MyPlage.Cells 
     If Not IsError(Cell) Then 
      If Cell.Value = "." Then 
       Cell.Interior.ColorIndex=28 
       Cell.Font.Bold = True 
      End If 

      etc 

     End If 
    Next 
+0

解決了這個問題非常感謝。 – 2012-01-11 19:30:22

+0

@Jenny當你得到滿意的答案時,tou應該接受它(點擊勾號)參見[Meta](http://meta.stackexchange.com/q/5234/159408) – 2012-01-12 07:53:21