2016-11-04 177 views
1

我得到了一個非常小的百分比值列表(0.000%格式),這表示路由器的錯誤百分比。我想根據單元格上的數量格式化單元格顏色。如果量超過0.050%,它應該是紅色的,如果超過0.005%,爲琥珀色,其他一切都是綠色在Excel中格式不正確的百分比值

這裏是我寫的代碼:

With .Cells(i, 8) 
     If .NumberFormat <> "0.000%" Then 
      .NumberFormat = "0.000%" 
      If .Value2 <> vbNullString And IsNumeric(.Value2) Then .Value = .Value/100 
       If .Value2 = vbNullString Then 
        .Value = "---" 
        .HorizontalAlignment = xlRight 
       End If 
     Else 
      .Value = 0 
     End If 

     If .Value > 0.05 Then 
      .Interior.Color = RGB(237, 67, 55) '<-- Red color 
      .Font.Color = vbWhite 

      ElseIf .Value > 0.005 Then 
       .Interior.Color = RGB(255, 190, 0) '<-- Amber Colour 
       .Font.Color = vbWhite 

      Else 
       .Interior.Color = RGB(50, 205, 50) '<-- Green color 
       .Font.Color = vbWhite 
     End If 
    End With 

但顏色格式是不準確的,這裏是一些結果列表:

0.034% <---green 
0.845% <---amber 
0.007% <---green 
0.005% <---green 
0.094% <---green 

它不應該是這樣,由於含有0.845%和琥珀色應該是鮮紅的細胞!

回答

3

存儲的值不是百分比。這是十進制等值,這意味着您必須將小數點左移兩位。所以要比較0.05%您必須使用0.0005

+0

@PaulOgilive我試着並沒有工作,但我今天再試一次,它做到了!歡呼的兄弟! –

0

這應該清理的代碼爲你讓它更快一點太:

Sub Test() 
Dim Cel As Range, Rng As Range 

Set Rng = Range("H1:H" & Range("H1048576").End(xlUp).Row).SpecialCells(xlCellTypeConstants) 

For Each Cel In Rng 
    If Trim(Cel.Value) = "" Then Cel.Value = "---": Cel.HorizontalAlignment = xlRight 
    If IsNumeric(Cel.Value) Then 
     Cel.Value = Cel.Value/100 
     If Cel.Value > 0.0005 Then 
      Cel.Interior.Color = RGB(237, 67, 55): Cel.Font.Color = vbWhite 
       ElseIf Cel.Value > 0.00005 Then 
        Cel.Interior.Color = RGB(255, 190, 0): Cel.Font.Color = vbWhite 
        Else: Cel.Interior.Color = RGB(50, 205, 50): Cel.Font.Color = vbWhite 
     End If 
    End If 
Next 

With Range("H1:H" & Range("H1048576").End(xlUp).Row).SpecialCells(xlCellTypeBlanks) 
    .Value = "---" 
    .HorizontalAlignment = xlRight 
End With 

End Sub 

我只是保羅意識到之後,昨天糾正你的問題....