用一些 vba 代码编写,进入死胡同!如果没有结束如果得到错误结束块结束
writing with some vba code, hit a dead end! getting error end block end if without end if
我正在创建仪表板。我有两个形状 oval 1 和 oval 2。它们会根据特定单元格的值改变颜色
我遇到一个错误:
block if without end if
我哪里做错了!
Sub Worksheet_Change(ByVal Target As Range)
'
If Intersect(Target, Range("E10")) Is Nothing Then Exit Sub
If Target.Value >= -0.1 And Target.Value <= 0.1 Then
ActiveSheet.Shapes.Range(Array("Oval 1")).Select
With Selection.ShapeRange.Fill
.ForeColor.RGB = RGB(0, 176, 80)
End With
ElseIf Target.Value >= -0.29 And Target.Value < 0.29 Then
ActiveSheet.Shapes.Range(Array("Oval 1")).Select
With Selection.ShapeRange.Fill
.ForeColor.RGB = RGB(255, 255, 0)
End With
Else
ActiveSheet.Shapes.Range(Array("Oval 1")).Select
With Selection.ShapeRange.Fill
.ForeColor.RGB = RGB(255, 0, 0)
End With
If Intersect(Target, Range("N10")) Is Nothing Then Exit Sub
If Target.Value >= -0.1 And Target.Value <= 0.1 Then
ActiveSheet.Shapes.Range(Array("Oval 2")).Select
With Selection.ShapeRange.Fill
.ForeColor.RGB = RGB(0, 176, 80)
End With
ElseIf Target.Value >= -0.29 And Target.Value < 0.29 Then
ActiveSheet.Shapes.Range(Array("Oval 2")).Select
With Selection.ShapeRange.Fill
.ForeColor.RGB = RGB(255, 255, 0)
End With
Else
ActiveSheet.Shapes.Range(Array("Oval 2")).Select
With Selection.ShapeRange.Fill
.ForeColor.RGB = RGB(255, 0, 0)
End With
End If
Range("A1").Select
End Sub
重构:
Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
If Not Intersect(Target, Me.Range("E10")) Is Nothing Then
Me.Shapes.Range("Oval 1").ShapeRange.Fill.ForeColor.RGB = ValueColor(Target.Value)
End If
If Not Intersect(Target, Me.Range("N10")) Is Nothing Then
Me.Shapes.Range("Oval 2").ShapeRange.Fill.ForeColor.RGB = ValueColor(Target.Value)
End If
End Sub
Function ValueColor(v) As Long
Dim rv As Long
If v > -0.1 And v <= 0.1 Then
rv = RGB(0, 176, 80)
ElseIf v.Value >= -0.29 And v.Value < 0.29 Then
rv = RGB(255, 255, 0)
Else
rv = RGB(255, 0, 0)
End If
ValueColor = rv
End Function
我正在创建仪表板。我有两个形状 oval 1 和 oval 2。它们会根据特定单元格的值改变颜色
我遇到一个错误:
block if without end if
我哪里做错了!
Sub Worksheet_Change(ByVal Target As Range)
'
If Intersect(Target, Range("E10")) Is Nothing Then Exit Sub
If Target.Value >= -0.1 And Target.Value <= 0.1 Then
ActiveSheet.Shapes.Range(Array("Oval 1")).Select
With Selection.ShapeRange.Fill
.ForeColor.RGB = RGB(0, 176, 80)
End With
ElseIf Target.Value >= -0.29 And Target.Value < 0.29 Then
ActiveSheet.Shapes.Range(Array("Oval 1")).Select
With Selection.ShapeRange.Fill
.ForeColor.RGB = RGB(255, 255, 0)
End With
Else
ActiveSheet.Shapes.Range(Array("Oval 1")).Select
With Selection.ShapeRange.Fill
.ForeColor.RGB = RGB(255, 0, 0)
End With
If Intersect(Target, Range("N10")) Is Nothing Then Exit Sub
If Target.Value >= -0.1 And Target.Value <= 0.1 Then
ActiveSheet.Shapes.Range(Array("Oval 2")).Select
With Selection.ShapeRange.Fill
.ForeColor.RGB = RGB(0, 176, 80)
End With
ElseIf Target.Value >= -0.29 And Target.Value < 0.29 Then
ActiveSheet.Shapes.Range(Array("Oval 2")).Select
With Selection.ShapeRange.Fill
.ForeColor.RGB = RGB(255, 255, 0)
End With
Else
ActiveSheet.Shapes.Range(Array("Oval 2")).Select
With Selection.ShapeRange.Fill
.ForeColor.RGB = RGB(255, 0, 0)
End With
End If
Range("A1").Select
End Sub
重构:
Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
If Not Intersect(Target, Me.Range("E10")) Is Nothing Then
Me.Shapes.Range("Oval 1").ShapeRange.Fill.ForeColor.RGB = ValueColor(Target.Value)
End If
If Not Intersect(Target, Me.Range("N10")) Is Nothing Then
Me.Shapes.Range("Oval 2").ShapeRange.Fill.ForeColor.RGB = ValueColor(Target.Value)
End If
End Sub
Function ValueColor(v) As Long
Dim rv As Long
If v > -0.1 And v <= 0.1 Then
rv = RGB(0, 176, 80)
ElseIf v.Value >= -0.29 And v.Value < 0.29 Then
rv = RGB(255, 255, 0)
Else
rv = RGB(255, 0, 0)
End If
ValueColor = rv
End Function