找到一个百分比并从中减去一个百分比
Finding a percentage and minus a percentage from it
我有一行简单的代码可以正常工作,如下所示:
If Availability >= DLookup("[Availability]", "[tbl_RAG]", "[Department] = 'Outbound'") Then RAGAvailability.ForeColor = RGB(0, 176, 80) 'green
我稍微修改了它,从它找到的数字中扣除了 2%,如下所示(请注意下面是不起作用的:
代码:
If Availability < DLookup("[Availability]", "[tbl_RAG]", "[Department] = 'Outbound'") - (Format(2, "percent")) Then RAGAvailability.ForeColor = RGB(192, 0, 0) 'red
我也试过:
If Availability < DLookup("[Availability]", "[tbl_RAG]", "[Department] = 'Outbound'") - 0.02 Then RAGAvailability.ForeColor = RGB(192, 0, 0) 'red
table TBL_RAG 数据已存储为 %。
任何我可能做错的想法
您可以在 DLookUp
.
中使用微积分
尝试以下操作:
If Availability < DLookup("[Availability]-0.02", "[tbl_RAG]", "[Department] = 'Outbound'") Then RAGAvailability.ForeColor = RGB(192, 0, 0) 'red
您的第二次尝试是正确的,但也许 DLookup returns 为空?如果是:
If Availability < Nz(DLookup("[Availability]", "[tbl_RAG]", "[Department] = 'Outbound'"), 0) - 0.02 Then
RAGAvailability.ForeColor = RGB(192, 0, 0) 'red
End If
或者您使用的是 Double 而不是 Currency。那么:
If Availability < CCur(Nz(DLookup("[Availability]", "[tbl_RAG]", "[Department] = 'Outbound'"), 0)) - CCur(0.02) Then
RAGAvailability.ForeColor = RGB(192, 0, 0) 'red
End If
编辑:
来回改变颜色:
Dim Color As Long
If Availability < CCur(Nz(DLookup("[Availability]", "[tbl_RAG]", "[Department] = 'Outbound'"), 0)) - CCur(0.02) Then
Color = vbRed
Else
Color = vbBlack
End If
RAGAvailability.ForeColor = Color
感谢您的帮助。我似乎已经使用以下代码纠正了问题:
Dim db As Database, AvailRs As Recordset
Set db = CurrentDb
Set AvailRs = db.OpenRecordset("Select * from tbl_RAG")
Select Case Availability
Case Is > Format(AvailRs!UpperAvailability, "percent")
RAGAvailability.ForeColor = RGB(0, 176, 80)
Case Is < Format(AvailRs!lowerAvailability, "percent")
RAGAvailability.ForeColor = RGB(192, 0, 0)
Case Else
RAGAvailability.ForeColor = RGB(255, 192, 0)
End Select
发生了什么:
我的理解是代码无法从 Available 中读取字符串。我将其格式化为百分比并使用 Select Case
将其组合在一起
我有一行简单的代码可以正常工作,如下所示:
If Availability >= DLookup("[Availability]", "[tbl_RAG]", "[Department] = 'Outbound'") Then RAGAvailability.ForeColor = RGB(0, 176, 80) 'green
我稍微修改了它,从它找到的数字中扣除了 2%,如下所示(请注意下面是不起作用的:
代码:
If Availability < DLookup("[Availability]", "[tbl_RAG]", "[Department] = 'Outbound'") - (Format(2, "percent")) Then RAGAvailability.ForeColor = RGB(192, 0, 0) 'red
我也试过:
If Availability < DLookup("[Availability]", "[tbl_RAG]", "[Department] = 'Outbound'") - 0.02 Then RAGAvailability.ForeColor = RGB(192, 0, 0) 'red
table TBL_RAG 数据已存储为 %。
任何我可能做错的想法
您可以在 DLookUp
.
尝试以下操作:
If Availability < DLookup("[Availability]-0.02", "[tbl_RAG]", "[Department] = 'Outbound'") Then RAGAvailability.ForeColor = RGB(192, 0, 0) 'red
您的第二次尝试是正确的,但也许 DLookup returns 为空?如果是:
If Availability < Nz(DLookup("[Availability]", "[tbl_RAG]", "[Department] = 'Outbound'"), 0) - 0.02 Then
RAGAvailability.ForeColor = RGB(192, 0, 0) 'red
End If
或者您使用的是 Double 而不是 Currency。那么:
If Availability < CCur(Nz(DLookup("[Availability]", "[tbl_RAG]", "[Department] = 'Outbound'"), 0)) - CCur(0.02) Then
RAGAvailability.ForeColor = RGB(192, 0, 0) 'red
End If
编辑: 来回改变颜色:
Dim Color As Long
If Availability < CCur(Nz(DLookup("[Availability]", "[tbl_RAG]", "[Department] = 'Outbound'"), 0)) - CCur(0.02) Then
Color = vbRed
Else
Color = vbBlack
End If
RAGAvailability.ForeColor = Color
感谢您的帮助。我似乎已经使用以下代码纠正了问题:
Dim db As Database, AvailRs As Recordset
Set db = CurrentDb
Set AvailRs = db.OpenRecordset("Select * from tbl_RAG")
Select Case Availability
Case Is > Format(AvailRs!UpperAvailability, "percent")
RAGAvailability.ForeColor = RGB(0, 176, 80)
Case Is < Format(AvailRs!lowerAvailability, "percent")
RAGAvailability.ForeColor = RGB(192, 0, 0)
Case Else
RAGAvailability.ForeColor = RGB(255, 192, 0)
End Select
发生了什么: 我的理解是代码无法从 Available 中读取字符串。我将其格式化为百分比并使用 Select Case
将其组合在一起