2014-11-24 84 views
0

這是根據從A1到A4單元格定義的標籤中的單元格顏色爲列圖着色的代碼。它上週五運行,但現在線.Points(iCategory).Format.Fill.ForeColor.RGB = rCategory.Interior.Color給出錯誤。上週五完美運行的代碼中的「錯誤91:對象變量或帶有塊變量未設置」

Sub ColorByCategoryLabel() 
    Dim rPatterns As Range 
    Dim iCategory As Long 
    Dim vCategories As Variant 
    Dim rCategory As Range 

    Set rPatterns = ActiveSheet.Range("A1:A4") 
    With ActiveChart.SeriesCollection(1) 
    vCategories = .XValues 
    For iCategory = 1 To UBound(vCategories) 
     Set rCategory = rPatterns.Find(What:=vCategories(iCategory)) 
     .Points(iCategory).Format.Fill.ForeColor.RGB = rCategory.Interior.Color 
    Next 
    End With 
End Sub 

回答

3

這是因爲rCategoryNothing.Find不返回任何內容。要測試它,請將您的代碼更改爲此。

For iCategory = 1 To UBound(vCategories) 
    Set rCategory = rPatterns.Find(What:=vCategories(iCategory)) 

    If rCategory Is Nothing Then 
     MsgBox vCategories(iCategory) & " Not Found" 
    Else 
     .Points(iCategory).Format.Fill.ForeColor.RGB = rCategory.Interior.Color 
    End If 
Next 

檢查,如果你指的是右側紙和搜索實際存在的東西。

+0

好的,已解決!在A1到A4中沒有提及,我有公式參考文本真正的另一個單元格。有趣的是這個星期五工作,而不是星期一。我想Excel很累了一個星期:D – Mikelowski 2014-11-24 09:20:16

+0

很高興它得到了解決:) – 2014-11-24 11:04:13

相關問題