0

*強いテキスト* 以下のコードは正常に実行されますが、正と負の両方のセル値を同じものとして扱い、それに応じて色を付けるコードが必要です

元:

1) 0 ~ 4 および 0 ~ -4 = 緑色 2) 5 ~ 9 および -5 ~ -9 = オレンジ色

以下のコードは、正の値に対してのみ機能します

サブ changeTextColor()

GreenColor = RGB(0, 128, 0)
RedColor = RGB(255, 0, 0)
OrangeColor = RGB(255, 204, 0)
WhiteColor = RGB(255, 255, 255)

'Get number of rows in the specified column
RowsCount = Range("K2", Range("K2").End(xlDown)).Rows.Count

'Select cell
Range("K2").Select

'Loop the cells
For x = 1 To RowsCount
    If ((ActiveCell.Value) <= 4) Then
        ActiveCell.Interior.Color = GreenColor
    ElseIf ((ActiveCell.Value) >= 5) And ((ActiveCell.Value) <= 9) Then
        ActiveCell.Interior.Color = OrangeColor
    ElseIf ((ActiveCell.Value) > 10) And ((ActiveCell.Value) <= 10000) Then
        ActiveCell.Interior.Color = RedColor

    End If

    ActiveCell.Offset(1, 0).Select


Next

サブ終了

4

1 に答える 1

1

絶対値を使用してください。のように
条件を置き換えます。(ActiveCell.Value) <= 4)(Abs(ActiveCell.Value) <= 4)

このようなコードは、「選択」を使用せず、セルを直接参照するだけではるかに高速に実行されます。

Dim v As Long
Dim r As Range
Dim i As Long

' [...]

'Select cell -> NO, don't
' Range("K2").Select

'Loop the cells
For x = 1 To RowsCount
    Set r = ActiveSheet.Cells(1+x, "K") ' starts at K2
    v = Abs(r.Value)

    If v <= 4 Then
        r.Interior.Color = GreenColor
    elseif ... ' and so on
    ' ...

Next

ところで、Conditional Formattingを見てください。

于 2013-09-06T09:23:09.853 に答える