当前位置: 代码迷 >> VBA >> 这段改变颜色的VBA代码有什么有关问题?为什么无论改哪个位置都会变色
  详细解决方案

这段改变颜色的VBA代码有什么有关问题?为什么无论改哪个位置都会变色

热度:1052   发布时间:2013-02-26 00:00:00.0
这段改变颜色的VBA代码有什么问题?为什么无论改哪个位置都会变色?
VB code
Private Sub Worksheet_Change(ByVal Target As Range)    Application.EnableEvents = false    On Error Goto ENEV    REM add your code below    If ((Target.Column >= 5) and (Target.Column <= 8)) or (Target.Column >= 14) Then        if IsNumeric(Target) then            If Target > 0 Then                Target.Font.Color = RGB(255,0,0)            Elseif Target < 0 Then                Target.Font.Color = RGB(0,128,0)            Else                Target.Font.Color = RGB(128,128,138)            End If        End if    End If    ENEV:    Application.EnableEvents = trueEnd Sub


------解决方案--------------------------------------------------------
我这测试好像是正常的
------解决方案--------------------------------------------------------
用这个试试:
VBScript code
Private Sub Worksheet_Change(ByVal Target As Range)    Application.EnableEvents = False    On Error GoTo ENEV    Rem add your code below    With Target        If ((.Column >= 5) And (.Column <= 8)) Or (.Column >= 14) Then            If IsNumeric(Target) Then                If .Value > 0 Then                    .Font.ColorIndex = 3                ElseIf .Value < 0 Then                    'Target.Font.Color = RGB(0, 128, 0)                    .Font.ColorIndex = 10                Else                    .Font.ColorIndex = 48                End If            End If        Else            .Font.ColorIndex = 0        End If    End WithENEV:    Application.EnableEvents = TrueEnd Sub
  相关解决方案