将一个值单元与一定数量进行比较,并根据比较来定义内部颜色

我有一个Cell1的值,想知道这个值是否在一定数量之间。 当我知道在哪个数字是Cell1之间时,我改变了另一个Cell = Cell2的内部颜色。 我写的只是第一个比较,虽然它是错误的,并改变了错误的Cell的内部颜色。 这就是我写的:

Sub CellFormatColor() Dim r As Double Dim i As Integer For i = 5 To 15 r = Cells(23, i) If r > 5000 & r < 24999 Then Cells(11, i).Interior.Color = RGB(150, 100, 100) ElseIf r > 25000 & r < 49999 Then Cells(12, i).Interior.Color = RGB(150, 100, 100) ElseIf r > 50000 & r < 99999 Then Cells(13, i).Interior.Color = RGB(150, 100, 100) ElseIf r > 100000 & r < 249999 Then Cells(14, i).Interior.Color = RGB(150, 100, 100) ElseIf r > 250000 & r < 499999 Then Cells(15, i).Interior.Color = RGB(150, 100, 100) ElseIf r > 500000 & r < 999999 Then Cells(16, i).Interior.Color = RGB(150, 100, 100) ElseIf r > 1000000 & r < 1999999 Then Cells(17, i).Interior.Color = RGB(150, 100, 100) ElseIf r > 2000000 & r < 4999999 Then Cells(18, i).Interior.Color = RGB(150, 100, 100) ElseIf r > 5000000 Then Cells(12, i).Interior.Color = RGB(150, 100, 100) End If Next i End Sub 

例如,r =单元格(23,5)= 200000.当进行比较时,单元格(11,5)会改变颜色,尽pipe200000不在5000和24999之间。真正的单元格必须是单元格(14,5 )。

任何帮助恳求?

你需要在你的逻辑中使用AND而不是在这个基础上做一些事情

 cells(10,i).offset(cint(r/24998),0).interior.color.... 

或者,您可以用Select Casereplace您的ElseIf

 Select Case r Case 5001 To 24998 Cells(11, i).Interior.Color = RGB(150, 100, 100) Case 25001 To 49998 Cells(12, i).Interior.Color = RGB(150, 100, 100) Case 500001 To 99998 Cells(13, i).Interior.Color = RGB(150, 100, 100) Case 100001 To 249998 Cells(14, i).Interior.Color = RGB(150, 100, 100) Case 250001 To 499998 Cells(15, i).Interior.Color = RGB(150, 100, 100) Case 500001 To 999999 Cells(16, i).Interior.Color = RGB(150, 100, 100) Case 1000001 To 1999998 Cells(17, i).Interior.Color = RGB(150, 100, 100) Case 2000001 To 4999998 Cells(18, i).Interior.Color = RGB(150, 100, 100) Case Is > 5000000 Cells(12, i).Interior.Color = RGB(150, 100, 100) End Select