2017-04-18 102 views
0

最近我已經自動根據3個關鍵條件爲一系列圖表着色。如果它們是「計劃」,「強制」或「其他」 - 圖表顏色將自動化,而不是手動過程。然而,即使我的代碼沒有變化,它最近也停止了工作。無效的參數 - VBA圖表顏色

我回顧了我的代碼,它突出顯示了我的一個模塊,下面的代碼行是罪魁禍首。

ElseIf InStr(outRng.Offset(tabRowloop, 5 + ((tabcolloop - 1) * 5)), "Planned") > 0 Then      
    serobj.Points(tabRowloop + 1).Format.Fill.ForeColor.RGB = RGB(30, 65, 100) 

如果我註釋掉這一行,代碼將運行但着色不起作用。循環函數很好,但我不明白我的點的着色如何導致「無效參數」錯誤。

任何人都可以提供任何指導原因可能是什麼?

'Code for Recolor Chart Module 
Dim tabRowloop As Integer 
Dim tabcolloop As Integer 
Dim seriesnum As Integer 
Dim serobj As Series 

For tabcolloop = 1 To maxGanttBarsPerSite Step 1 

    seriesnum = stupidRangeIndexThingy(tabcolloop) 
    Set serobj = chobj.Chart.FullSeriesCollection(seriesnum) 
    serobj.Format.Fill.ForeColor.RGB = RGB(50, 50, 50) 

    tabRowloop = 0 
    Do While Len(outRng.Offset(tabRowloop, 1)) 
     If (Len(outRng.Offset(tabRowloop, 1 + ((tabcolloop - 1) * 5))) > 0) Then 
      Debug.Print tabRowloop & "," & tabcolloop 
      If InStr(outRng.Offset(tabRowloop, 5 + ((tabcolloop - 1) * 5)), "Forced") > 0 Then 
       serobj.Points(tabRowloop + 1).Format.Fill.ForeColor.RGB = RGB(192, 0, 0) 
      ElseIf InStr(outRng.Offset(tabRowloop, 5 + ((tabcolloop - 1) * 5)), "Planned") > 0 Then 
       serobj.Points(tabRowloop + 1).Format.Fill.ForeColor.RGB = RGB(30, 65, 100) 
       'problem is with the above line 
      Else 
       serobj.Points(tabRowloop + 1).Format.Fill.ForeColor.RGB = RGB(247, 150, 70) 
      End If 
     End If 
     tabRowloop = tabRowloop + 1 
    Loop 
Next 

'Sort table - Synergy first then by name 
Call SortFinalTable(numUnitsFound) 

'Turn on calc and redraw 
Application.Calculation = xlCalculationAutomatic 
Application.ScreenUpdating = True 

End Sub 
+0

「錯誤」 線之前,通過添加3行嘗試調試tabRowloop'和'Debug.Print tabcolloop'並查看您在即時窗口中獲得的結果 –

+0

循環的前5次運行正常,但是在循環的第六次運行中 - tabcolloop值與「無效參數」錯誤。我只是不明白爲什麼會這樣。 – azurekirby

回答

0

如果您的Points對象的索引超出範圍,那麼您將得到一個Invalid Parameter錯誤。如果它在第一次循環時失敗,當tabRowLoop仍然等於0時,它表明serobj沒有任何點。如果它在後面的循環中失敗,那是因爲你超出了系列中的點數。在遍歷代碼時,在設置serobj變量之後,嘗試通過運行一些debug/immediate命令來確認已選擇了哪個系列。 。Value`,`Debug.Print - `Debug.Print outRng.Offset(1)* 5)tabRowloop,5 +((tabcolloop):

print serobj.Points.Count