我开始使用VBA设置工作表中数据字段的背景颜色,但由于我的代码太笨重,因此达到了64kb的限制。条件格式化将花费我几天的时间,因为我使用的是另一个工作表中的参数,并且有10个工作表,每个工作表有300个数据点。正如您在下面看到的,很多代码是重复的。我尝试使用更多变量来压缩它,但是每次尝试都会出错。我很乐意就如何将if语句变成某种子例程的建议。任何其他建议将不胜感激。
Sub backcolorOPS()
Dim i As Long, r1 As Range, r2 As Range
'BFN Operations DCR
For i = 4 To 4
Set r1 = Worksheets("Sheet2").Range("F648")
Set r2 = Range("F" & i & ":H" & i)
If r1 = 1 Then r2.Interior.Color = RGB(155, 187, 89)
If r1 = 2 Then r2.Interior.Color = RGB(255, 255, 255)
If r1 = 3 Then r2.Interior.Color = RGB(255, 255, 102)
If r1 = 4 Then r2.Interior.Color = RGB(192, 80, 77)
Set r1 = Worksheets("Sheet2").Range("G648")
Set r2 = Range("I" & i & ":K" & i)
If r1 = 1 Then r2.Interior.Color = RGB(155, 187, 89)
If r1 = 2 Then r2.Interior.Color = RGB(255, 255, 255)
If r1 = 3 Then r2.Interior.Color = RGB(255, 255, 102)
If r1 = 4 Then r2.Interior.Color = RGB(192, 80, 77)
Set r1 = Worksheets("Sheet2").Range("H648")
Set r2 = Range("L" & i & ":N" & i)
If r1 = 1 Then r2.Interior.Color = RGB(155, 187, 89)
If r1 = 2 Then r2.Interior.Color = RGB(255, 255, 255)
If r1 = 3 Then r2.Interior.Color = RGB(255, 255, 102)
If r1 = 4 Then r2.Interior.Color = RGB(192, 80, 77)
Set r1 = Worksheets("Sheet2").Range("I648")
Set r2 = Range("O" & i & ":Q" & i)
If r1 = 1 Then r2.Interior.Color = RGB(155, 187, 89)
If r1 = 2 Then r2.Interior.Color = RGB(255, 255, 255)
If r1 = 3 Then r2.Interior.Color = RGB(255, 255, 102)
If r1 = 4 Then r2.Interior.Color = RGB(192, 80, 77)
Next i
'BFN Operation Transient Response
For i = 5 To 5
Set r1 = Worksheets("Sheet2").Range("F651")
Set r2 = Range("F" & i & ":H" & i)
If r1 = 1 Then r2.Interior.Color = RGB(155, 187, 89)
If r1 = 2 Then r2.Interior.Color = RGB(255, 255, 255)
If r1 = 3 Then r2.Interior.Color = RGB(255, 255, 102)
If r1 = 4 Then r2.Interior.Color = RGB(192, 80, 77)
Set r1 = Worksheets("Sheet2").Range("G651")
Set r2 = Range("I" & i & ":K" & i)
If r1 = 1 Then r2.Interior.Color = RGB(155, 187, 89)
If r1 = 2 Then r2.Interior.Color = RGB(255, 255, 255)
If r1 = 3 Then r2.Interior.Color = RGB(255, 255, 102)
If r1 = 4 Then r2.Interior.Color = RGB(192, 80, 77)
Set r1 = Worksheets("Sheet2").Range("H651")
Set r2 = Range("L" & i & ":N" & i)
If r1 = 1 Then r2.Interior.Color = RGB(155, 187, 89)
If r1 = 2 Then r2.Interior.Color = RGB(255, 255, 255)
If r1 = 3 Then r2.Interior.Color = RGB(255, 255, 102)
If r1 = 4 Then r2.Interior.Color = RGB(192, 80, 77)
Set r1 = Worksheets("Sheet2").Range("I651")
Set r2 = Range("O" & i & ":Q" & i)
If r1 = 1 Then r2.Interior.Color = RGB(155, 187, 89)
If r1 = 2 Then r2.Interior.Color = RGB(255, 255, 255)
If r1 = 3 Then r2.Interior.Color = RGB(255, 255, 102)
If r1 = 4 Then r2.Interior.Color = RGB(192, 80, 77)
Next i
'BFN Mispositioned Components
For i = 8 To 8
Set r1 = Worksheets("Sheet2").Range("F601")
Set r2 = Range("F" & i & ":H" & i)
If r1 = 1 Then r2.Interior.Color = RGB(155, 187, 89)
If r1 = 2 Then r2.Interior.Color = RGB(255, 255, 255)
If r1 = 3 Then r2.Interior.Color = RGB(255, 255, 102)
If r1 = 4 Then r2.Interior.Color = RGB(192, 80, 77)
Set r1 = Worksheets("Sheet2").Range("G601")
Set r2 = Range("I" & i & ":K" & i)
If r1 = 1 Then r2.Interior.Color = RGB(155, 187, 89)
If r1 = 2 Then r2.Interior.Color = RGB(255, 255, 255)
If r1 = 3 Then r2.Interior.Color = RGB(255, 255, 102)
If r1 = 4 Then r2.Interior.Color = RGB(192, 80, 77)
Set r1 = Worksheets("Sheet2").Range("H601")
Set r2 = Range("L" & i & ":N" & i)
If r1 = 1 Then r2.Interior.Color = RGB(155, 187, 89)
If r1 = 2 Then r2.Interior.Color = RGB(255, 255, 255)
If r1 = 3 Then r2.Interior.Color = RGB(255, 255, 102)
If r1 = 4 Then r2.Interior.Color = RGB(192, 80, 77)
Set r1 = Worksheets("Sheet2").Range("I601")
Set r2 = Range("O" & i & ":Q" & i)
If r1 = 1 Then r2.Interior.Color = RGB(155, 187, 89)
If r1 = 2 Then r2.Interior.Color = RGB(255, 255, 255)
If r1 = 3 Then r2.Interior.Color = RGB(255, 255, 102)
If r1 = 4 Then r2.Interior.Color = RGB(192, 80, 77)
Next i