2014-01-15 2 views
2

내 코드가 Error 9, Subscript out of range 오류를 발생시키는 경우가 있습니다. 다른 많은 것들 중에서도 내 코드는 많은 양의 셀을 필요로하고 기존의 조건부 서식을 제거한 다음범위에 방금 추가 한 항목 수에 따라 조건을 추가하여 다시 적용합니다. 난 (12) = 에러 그것은으로 일어나면 조금 랜덤 표시 Case Else, .FormatConditions(i).Font.Color = vbWhite.에서 발생하지만, 자주 .Font.Color = vbWhite 발생했을 때에 순간 첨자가 범위를 벗어남 오류 (오류 9) : .FormatConditions

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 

는 오류를 초래한다. 간단히 말해서 REM을 사용하면 가끔씩 사라집니다 (분명히 솔루션이 아닙니다!). 그러나 형식 조건이 추가 된 다른 행 중 하나에 나타납니다.

도움을 주시면 대단히 감사하겠습니다.

+1

+1 귀하의 문제를 설명하는 동안 모든 세부 사항을 다루는 :) 나는 당신이 오류 메시지와 당신이 가리키는 라인과 혼란스러워하고 왜 이것이 일어날 수도 있다고 생각합니다. Excel에서 특정 개체를 찾을 수없는 경우 아래 첨자 범위를 벗어난 오류가 발생합니다. 예를 들어'FormatConditions (12)'는'vbWhite' 때문에 발생하지 않습니다 –

+0

''런타임 오류 '가 더 많을 것입니다 1004 :'해당 응용 프로그램 정의 또는 객체 정의 오류 '오류 ... –

+0

A 어둠에 쐈어. 나 한테 뭔가 해줄 수있어? '.Font.Color = vbWhite'를'.Font.ColorIndex = 2'로 변경 하시겠습니까? –

답변

0

나는 다음과 같이 변경 다음은 내용의 결정 newFC에 브레이크 포인트 추천 수 : FormatCondition으로

희미한 newFC을 세트 newFC = .FormatConditions.Add (유형 : = xlExpression, 연산자 : = xlEqual, Formula1 : = " MATCH = ("& 렛트 (범위 (sLevelRangeName) .Cells (2, k)를 .Column) &"2 MajorLevels, 0) = "& ⅰ)

행운.

+0

고마워. 나는 당신의 제안을 구현하지는 않았지만, 무엇이 잘못되었는지 알아내는 것이 좋습니다. 매우 이상하게도 vbWhite로 설정되는 formatCondition을 제거하면 문제가 해결됩니다. 문제는 그냥 사라집니다. 그것은 내 코드 전체 (약 10,000 줄의 코드를 가짐)에서 자릅니다. 그리고 언제나 FormatCondition이 vbWhite로 설정됩니다. 나는 그 코드 줄을 제거하고 모두 좋다. 이유를 아는 사람 -이 "발견"이 다른 사람들을 도울 수 있기를 바랍니다. 모든 당신의 sugestions 주셔서 감사합니다. – user3197246

관련 문제