根据两个不同的单元格更改选项卡颜色(if语句)
Change tab color according to two different cells (if statement)
我正在尝试按照 if 逻辑更改单元格 C32 和 C46 的选项卡颜色。尽管逻辑对大多数逻辑都有效,但有些逻辑并没有给我带来正确的颜色。我对 VBA 不是很熟悉。
什么不起作用:
C32= Fail and C46= Not Complette (brings me green instead of red)
C32= Not applicable and C46= Not Applicable (brings me red instead of gray)
C32= Not applicable and C46= Not Complete (brings me green instead of gray)
C32= Not Complete and C46= Not Complete (brings me green instead of gray)
C32= Not Complete and C46= Not Applicable (brings me red instead of gray)
请帮忙。修复起来似乎很简单,但可以找到解决方案
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("$C").Value = "Pass" And Range("$C").Value = "Pass" Or Range("$C").Value = "Not Complete" Then
Me.Tab.ColorIndex = 10
ElseIf Range("$C").Value = "Pass" And Range("$C").Value = "Not Applicable" Then
Me.Tab.ColorIndex = 48
ElseIf Range("$C").Value = "Pass" And Range("$C").Value = "Fail" Then
Me.Tab.Color = vbRed
ElseIf Range("$C").Value = "Fail" And Range("$C").Value = "Fail" Or Range("$C").Value = "Not Complete" Or Range("$C").Value = "Not Applicable" Then
Me.Tab.Color = vbRed
ElseIf Range("$C").Value = "Fail" And Range("$C").Value = "Pass" Then
Me.Tab.ColorIndex = 10
ElseIf Range("$C").Value = "Not Applicable" And Range("$C").Value = "Not Applicable" Or Range("$C").Value = "Not Complete" Then
Me.Tab.ColorIndex = 48
ElseIf Range("$C").Value = "Not Applicable" And Range("$C").Value = "Fail" Then
Me.Tab.Color = vbRed
ElseIf Range("$C").Value = "Not Applicable" And Range("$C").Value = "Pass" Then
Me.Tab.ColorIndex = 10
ElseIf Range("$C").Value = "Not Complete" And Range("$C").Value = "Not Applicable" Or Range("$C").Value = "Not Complete" Then
Me.Tab.ColorIndex = 48
ElseIf Range("$C").Value = "Not Complete" And Range("$C").Value = "Pass" Then
Me.Tab.ColorIndex = 10
ElseIf Range("$C").Value = "Not Complete" And Range("$C").Value = "Fail" Then
Me.Tab.Color = vbRed
End If
End Sub
一些备注:
- 您正在使用
SelectionChange
事件(是故意的吗?)如果是,请更改我的代码以使用它
- 我在 Select 案例陈述中组织了代码,它 (IMO) 更容易阅读
- 我添加了括号以更好地理解与 C46 相关的选项
代码:
Option Explicit
Private Sub Worksheet_change(ByVal Target As Range)
' Suggest that you define a range to watch when changed for this (un comment and adjust next line if you consider it)
' If not intersect(Target, me.Range("c32:c46")) Then Exit sub
changeColorTab Me, LCase$(Me.Range("c32").Value), LCase$(Me.Range("c46").Value)
End Sub
Private Sub changeColorTab(ByVal targetSheet As Worksheet, ByVal c32Value As String, ByVal c46Value As String)
Dim targetcolor As Long
Select Case True
Case c32Value = "pass" And (c46Value = "pass" Or c46Value = "not complete")
targetcolor = 10 ' Green
Case c32Value = "pass" And c46Value = "not applicable"
targetcolor = 48 ' Dark grey
Case c32Value = "pass" And (c46Value = "fail")
targetcolor = 3 ' Red
Case c32Value = "fail" And (c46Value = "fail" Or c46Value = "not complete" Or c46Value = "not applicable")
targetcolor = 3 ' Red
Case c32Value = "fail" And c46Value = "pass"
targetcolor = 10 ' Green
Case c32Value = "not applicable" And (c46Value = "not applicable" Or c46Value = "not complete")
targetcolor = 48 ' Dark grey
Case c32Value = "not applicable" And c46Value = "fail"
targetcolor = 3 ' Red
Case c32Value = "not applicable" And c46Value = "pass"
targetcolor = 10 ' Green
Case c32Value = "not complete" And (c46Value = "not applicable" Or c46Value = "not complete")
targetcolor = 48
Case c32Value = "not complete" And c46Value = "pass"
targetcolor = 10
Case c32Value = "not complete" And c46Value = "fail"
targetcolor = 3 ' Red
Case Else
targetcolor = -4142 ' Light grey
End Select
targetSheet.Tab.ColorIndex = targetcolor
End Sub
如果有效请告诉我
我正在尝试按照 if 逻辑更改单元格 C32 和 C46 的选项卡颜色。尽管逻辑对大多数逻辑都有效,但有些逻辑并没有给我带来正确的颜色。我对 VBA 不是很熟悉。
什么不起作用:
C32= Fail and C46= Not Complette (brings me green instead of red)
C32= Not applicable and C46= Not Applicable (brings me red instead of gray)
C32= Not applicable and C46= Not Complete (brings me green instead of gray)
C32= Not Complete and C46= Not Complete (brings me green instead of gray)
C32= Not Complete and C46= Not Applicable (brings me red instead of gray)
请帮忙。修复起来似乎很简单,但可以找到解决方案
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("$C").Value = "Pass" And Range("$C").Value = "Pass" Or Range("$C").Value = "Not Complete" Then
Me.Tab.ColorIndex = 10
ElseIf Range("$C").Value = "Pass" And Range("$C").Value = "Not Applicable" Then
Me.Tab.ColorIndex = 48
ElseIf Range("$C").Value = "Pass" And Range("$C").Value = "Fail" Then
Me.Tab.Color = vbRed
ElseIf Range("$C").Value = "Fail" And Range("$C").Value = "Fail" Or Range("$C").Value = "Not Complete" Or Range("$C").Value = "Not Applicable" Then
Me.Tab.Color = vbRed
ElseIf Range("$C").Value = "Fail" And Range("$C").Value = "Pass" Then
Me.Tab.ColorIndex = 10
ElseIf Range("$C").Value = "Not Applicable" And Range("$C").Value = "Not Applicable" Or Range("$C").Value = "Not Complete" Then
Me.Tab.ColorIndex = 48
ElseIf Range("$C").Value = "Not Applicable" And Range("$C").Value = "Fail" Then
Me.Tab.Color = vbRed
ElseIf Range("$C").Value = "Not Applicable" And Range("$C").Value = "Pass" Then
Me.Tab.ColorIndex = 10
ElseIf Range("$C").Value = "Not Complete" And Range("$C").Value = "Not Applicable" Or Range("$C").Value = "Not Complete" Then
Me.Tab.ColorIndex = 48
ElseIf Range("$C").Value = "Not Complete" And Range("$C").Value = "Pass" Then
Me.Tab.ColorIndex = 10
ElseIf Range("$C").Value = "Not Complete" And Range("$C").Value = "Fail" Then
Me.Tab.Color = vbRed
End If
End Sub
一些备注:
- 您正在使用
SelectionChange
事件(是故意的吗?)如果是,请更改我的代码以使用它 - 我在 Select 案例陈述中组织了代码,它 (IMO) 更容易阅读
- 我添加了括号以更好地理解与 C46 相关的选项
代码:
Option Explicit
Private Sub Worksheet_change(ByVal Target As Range)
' Suggest that you define a range to watch when changed for this (un comment and adjust next line if you consider it)
' If not intersect(Target, me.Range("c32:c46")) Then Exit sub
changeColorTab Me, LCase$(Me.Range("c32").Value), LCase$(Me.Range("c46").Value)
End Sub
Private Sub changeColorTab(ByVal targetSheet As Worksheet, ByVal c32Value As String, ByVal c46Value As String)
Dim targetcolor As Long
Select Case True
Case c32Value = "pass" And (c46Value = "pass" Or c46Value = "not complete")
targetcolor = 10 ' Green
Case c32Value = "pass" And c46Value = "not applicable"
targetcolor = 48 ' Dark grey
Case c32Value = "pass" And (c46Value = "fail")
targetcolor = 3 ' Red
Case c32Value = "fail" And (c46Value = "fail" Or c46Value = "not complete" Or c46Value = "not applicable")
targetcolor = 3 ' Red
Case c32Value = "fail" And c46Value = "pass"
targetcolor = 10 ' Green
Case c32Value = "not applicable" And (c46Value = "not applicable" Or c46Value = "not complete")
targetcolor = 48 ' Dark grey
Case c32Value = "not applicable" And c46Value = "fail"
targetcolor = 3 ' Red
Case c32Value = "not applicable" And c46Value = "pass"
targetcolor = 10 ' Green
Case c32Value = "not complete" And (c46Value = "not applicable" Or c46Value = "not complete")
targetcolor = 48
Case c32Value = "not complete" And c46Value = "pass"
targetcolor = 10
Case c32Value = "not complete" And c46Value = "fail"
targetcolor = 3 ' Red
Case Else
targetcolor = -4142 ' Light grey
End Select
targetSheet.Tab.ColorIndex = targetcolor
End Sub
如果有效请告诉我