Excel VBA如果/ ElseIf似乎不接受函数结果作为输入

时间:2016-12-13 10:44:41

标签: excel vba excel-vba

对于Excel中的VBA,我是一个初学者,当用户输入特定输入时,我正在建立一个交通灯系统。如果输入高于/低于某个边界,则交通信号灯显示绿色/黄色/红色。它适用于用户的简单输入,但对于一个交通信号灯,我在后台使用一个函数来检查更多变量,然后输出0,1或2。如果结果为0,则灯应显示红色,如果1则为黄色,如果为2则为绿色。我的代码看起来如下:

Private Sub Worksheet_Change(ByVal Target As Range)

 ActiveSheet.Shapes("Oval 3").Fill.ForeColor.RGB = vbWhite
 ActiveSheet.Shapes("Oval 4").Fill.ForeColor.RGB = vbWhite
  ActiveSheet.Shapes("Oval 5").Fill.ForeColor.RGB = vbWhite

If Not Intersect(Target, Range("H11")) Is Nothing Then
    If IsNumeric(Target.Value) Then
        If Target.Value < 100 Then
            ActiveSheet.Shapes("Oval 3").Fill.ForeColor.RGB = vbRed
        ElseIf Target.Value >= 100 And Target.Value < 150 Then
            ActiveSheet.Shapes("Oval 4").Fill.ForeColor.RGB = vbYellow
        Else
            ActiveSheet.Shapes("Oval 5").Fill.ForeColor.RGB = vbGreen
        End If
    End If
End If

ActiveSheet.Shapes("Oval 9").Fill.ForeColor.RGB = vbWhite
 ActiveSheet.Shapes("Oval 10").Fill.ForeColor.RGB = vbWhite
  ActiveSheet.Shapes("Oval 11").Fill.ForeColor.RGB = vbWhite

If Not Intersect(Target, Range("A1")) Is Nothing Then
    If IsNumeric(Target.Value) Then
        If Target.Value < 1 Then
            ActiveSheet.Shapes("Oval 9").Fill.ForeColor.RGB = vbRed
        ElseIf Target.Value >= 1 And Target.Value < 2 Then
            ActiveSheet.Shapes("Oval 10").Fill.ForeColor.RGB = vbYellow
        Else
            ActiveSheet.Shapes("Oval 11").Fill.ForeColor.RGB = vbGreen
        End If
    End If
End If

ActiveSheet.Shapes("Oval 13").Fill.ForeColor.RGB = vbWhite
 ActiveSheet.Shapes("Oval 14").Fill.ForeColor.RGB = vbWhite
  ActiveSheet.Shapes("Oval 15").Fill.ForeColor.RGB = vbWhite

If Not Intersect(Target, Range("H11")) Is Nothing Then
    If IsNumeric(Target.Value) Then
        If Target.Value < 40 Then
            ActiveSheet.Shapes("Oval 13").Fill.ForeColor.RGB = vbRed
        ElseIf Target.Value >= 40 And Target.Value < 60 Then
            ActiveSheet.Shapes("Oval 14").Fill.ForeColor.RGB = vbYellow
        Else
            ActiveSheet.Shapes("Oval 15").Fill.ForeColor.RGB = vbGreen
        End If
    End If
End If


 End Sub

A1是在后台获取函数结果的Cell(0,1,2)。如果我将其设置为仅使用普通用户输入它可以工作,但它似乎不接受A1作为输入并且不会按预期更改交通灯的颜色。 (A1 = VALUE(Sheet 2中!E15)

感谢您的帮助!

0 个答案:

没有答案