Subscript out of range error (Error 9): .FormatConditions

ⅰ亾dé卋堺 提交于 2019-12-13 15:21:58

问题


My code sometimes throws up an Error 9, Subscript out of range error. Amongst many other things, my code takes a load of cells and removes existing conditional formatting to them and then re-applies it adding in i number of conditions dependent on the number of items that have just been added to a range.

Function FormatLevelX()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim r As Integer
Dim sLevelRangeName As String
For j = 1 To Sheets("LEVEL").Range("MajorLevels").Columns.Count 'repeat this for each of the major levels
    sLevelRangeName = "Level" & Sheets("LEVEL").Range("MajorLevels").Cells(1, j)
    For k = 1 To Sheets("LEVEL").Range(sLevelRangeName).Columns.Count 'repeat this for each column per major level
        For r = 2 To 5 'repeat this for each of the 4 cells (each on a different row) in the column that need conditional formatting
            With Sheets("LEVEL").Range(sLevelRangeName).Cells(r, k)
                    .FormatConditions.Delete
                    .Validation.Delete
                For i = 1 To Sheets("Level").Range("MajorLevels").Columns.Count 'make one rule per major level
                    .FormatConditions.Add Type:=xlExpression, Operator:=xlEqual, Formula1:="=MATCH(" & ColLett(Range(sLevelRangeName).Cells(2, k).Column) & "2,MajorLevels,0)=" & i
                        Select Case (i)
                        Case 1, 2, 3, 4, 5
                            .FormatConditions(i).Interior.ColorIndex = 45 + i
                            .FormatConditions(i).Font.Color = vbWhite
                            .FormatConditions(i).NumberFormat = "@"
                        Case 6
                            .FormatConditions(i).Interior.ColorIndex = 23
                            .FormatConditions(i).Font.Color = vbWhite
                            .FormatConditions(i).NumberFormat = "@"
                        Case 7, 8, 9
                            .FormatConditions(i).Interior.ColorIndex = 45 + i + 1
                            .FormatConditions(i).Font.Color = vbWhite
                            .FormatConditions(i).NumberFormat = "@"
                        Case Else
                            .FormatConditions(i).Interior.ColorIndex = 9 + i - 10
                            .FormatConditions(i).Font.Color = vbWhite
                            .FormatConditions(i).NumberFormat = "@"
                        End Select
                Next i
            End With
        Next r
    Next k
Next j

End Function

At the moment it is causing the error when i=12 and the error occurs under Case Else, .FormatConditions(i).Font.Color = vbWhite. It appears a little random as to when it happens, but frequently occurs on the .Font.Color = vbWhite. If I simply REM this out then it sometimes goes away (clearly not the solution!). Though will then appear on one of the other lines with format conditions being added.

Any help hugely appreciated.


回答1:


May I suggest the following change then breakpoint on newFC to determine it's contents:

Dim newFC As FormatCondition
set newFC = .FormatConditions.Add(Type:=xlExpression, Operator:=xlEqual, _
     Formula1:="=MATCH(" & ColLett(Range(sLevelRangeName).Cells(2, k).Column) & _
    "2,MajorLevels,0)=" & i)

Good Luck.



来源:https://stackoverflow.com/questions/21132561/subscript-out-of-range-error-error-9-formatconditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!