我试图在价值条件下改变单元格颜色,情况是我的工作中有不同的帖子,他们选择pcs放入托盘
在每个工作站,我们计算小时和每小时的pcs = pcs:小时=解决方案
例如:如果每小时120pcs或以上的单元格是绿色,则:119pcs和80pcs一小时单元格颜色为橙色小于80pcs一小时单元格颜色为红色
现在很多帖子我的代码都在工作但是对于一些帖子它给了我错误的颜色为什么我没找到
我不知道它有什么问题
我发布Vman的代码所有工作站都有这个代码,但只是改变条件值Vman是120pcs一小时像我告诉其他站可以是2250,100,350,300
等大多数工作站都能提供正确的色彩效果
是Vman的代码
If ComboBox1.Text = "Vman" Then
Range("ah10").Font.Color = vbBlack
Range("ah10").Font.Bold = True
Range("ah10").Font.Color = vbBlack
'grijs-Gray
If Range("ah10").Value = "" Then
Range("ah10").Interior.Color = RGB(128, 128, 128)
Range("ah10").Font.Color = vbBlack
'rood-Red
ElseIf Range("ah10").Value < "80" Then
If Range("ah10").Value > "" Then
Range("ah10").Interior.Color = vbRed
Range("ah10").Font.Color = vbWhite
End If
'oranje-Orange
ElseIf Range("ah10").Value >= "80" Then
If Range("ah10").Value < "120" Then
Range("ah10").Interior.Color = RGB(255, 153, 0)
End If
'groen-Green
ElseIf Range("ah10").Value >= "120" Then
Range("ah10").Interior.Color = RGB(146, 208, 80)
End If
End If
If ComboBox1.Text = "Vman 3" Then
Range("ah10").Font.Color = vbBlack
Range("ah10").Font.Bold = True
Range("ah10").Font.Color = vbBlack
'grijs-Gray
If Range("ah10").Value = "" Then
Range("ah10").Interior.Color = RGB(128, 128, 128)
Range("ah10").Font.Color = vbBlack
'rood-Red
ElseIf Range("ah10").Value < "80" Then
If Range("ah10").Value > "" Then
Range("ah10").Interior.Color = vbRed
Range("ah10").Font.Color = vbWhite
End If
'oranje-Orange
ElseIf Range("ah10").Value >= "80" Then
If Range("ah10").Value < "120" Then
Range("ah10").Interior.Color = RGB(255, 153, 0)
End If
'groen-Green
ElseIf Range("ah10").Value >= "120" Then
Range("ah10").Interior.Color = RGB(146, 208, 80)
End If
End If
我没有找到大多数工作站工作的代码的解决方案,并给我良好的颜色,但有些没有。
是否有理由让代码两次?
此外,它不起作用的原因似乎是因为你的价值在“引号”内。
至于为什么我使用.value2
而不是.value
参考这篇文章:
What is the difference between .text, .value, and .value2?
我还建议用变量来指代范围。这样,您只需更改一次,而不是代码中的10个不同位置。
试试这个
Dim pieces As Range
Set pieces = Range("AH10")
If ComboBox1.Text = "Vman" Or ComboBox1.Text = "Vman 3" Or ComboBox1.Text = "Vman 4" Then
pieces.Font.Color = vbBlack
pieces.Font.Bold = True
If pieces.Value2 = "" Then 'grijs-Gray
pieces.Interior.Color = RGB(128, 128, 128)
ElseIf pieces.Value2 < 80 Then 'rood-Red
pieces.Interior.Color = vbRed
pieces.Font.Color = vbWhite
ElseIf pieces.Value2 > 79 And pieces.Value2 < 120 Then 'oranje-Orange
pieces.Interior.Color = RGB(255, 153, 0)
ElseIf pieces.Value2 > 119 Then 'groen-Green
pieces.Interior.Color = RGB(146, 208, 80)
End If
End If
当像这样重复时,你可以使用With ... End With
代替或与变量一起使用。
If ComboBox1.Text = "Vman" Or ComboBox1.Text = "Vman 3" Or ComboBox1.Text = "Vman 4" Then
With Range("AH10")
.Font.Color = vbBlack
.Font.Bold = True
If .Value2 = "" Then 'grijs-Gray
.Interior.Color = RGB(128, 128, 128)
ElseIf .Value2 < 80 Then 'rood-Red
.Interior.Color = vbRed
.Font.Color = vbWhite
ElseIf .Value2 > 79 And pieces.Value2 < 120 Then 'oranje-Orange
.Interior.Color = RGB(255, 153, 0)
ElseIf .Value2 > 119 Then 'groen-Green
.Interior.Color = RGB(146, 208, 80)
End If
End With
End If