excel - 从遍历行的 if 语句返回文本值
问题描述
我想创建一个 if 函数,该函数根据 D 列中的行颜色和 EEg 列中的行百分比返回文本值,具体取决于 D6 中的颜色和 E6 中的值,在 F6 中返回一个值,然后循环遍历重复的行这。我编写的代码运行,但我的 excel 表上的任何地方都没有出现任何值,这可以解决吗?
Sub Message_Click()
Set shr = ActiveWorkbook.Sheets("Report")
shr.Range("F6:F37").ClearContents
Dim rng2 As Range
Dim rng3 As Range
Set rng2 = ActiveSheet.Range("D6:D37")
Set rng3 = ActiveSheet.Range("E6:E37")
For RR = 1 To 33
Set rng2 = Cells(RR + 5, 3)
Set rng3 = Cells(RR + 5, 4)
If rng2.Interior.ColorIndex = 50 Then
Range("F6:F37").Value = "Passed"
ElseIf rng2.Interior.ColorIndex = 38 And rng3 > 60 Then
Range("F6:F37").Value = "Warning"
ElseIf rng2.Interior.ColorIndex = 38 And rng3 < 60 Then
Range("F6:F37").Value = "Still has chances"
ElseIf rng2.Interior.ColorIndex = 38 And rng3 = 100 Then
Range("F6:F37").Value = "Failed"
End If
Next
End Sub
解决方案
当您将范围的值与数字进行比较时,它应该如下所示:
rng3.value < 60
另外,请记住,您没有对颜色索引执行单元格检查。因此,如果 rng2 中的一个单元格的颜色索引不是 50,但其他所有单元格都有,则:
rng2.Interior.ColorIndex = 50
将返回FALSE
整个范围。所以基本上你是在做范围检查。
正如评论中的某人所说,逻辑有点倒退。但是,这是一种方法:
Sub Message_Click()
Dim sht As Worksheet
Dim RR As Long
Set sht = ThisWorkbook.Worksheets("Report")
sht.Range("F6:F37").ClearContents
For RR = 6 To 37 Step 1
If sht.Cells(RR, "D").Interior.ColorIndex = 50 Then
sht.Cells(RR, "F").Value = "Passed"
ElseIf sht.Cells(RR, "D").Interior.ColorIndex= 38 And sht.Cells(RR, "E").Value > 60 And sht.Cells(RR, "E").Value < 100 Then
sht.Cells(RR, "F").Value = "Warning"
ElseIf sht.Cells(RR, "D").Interior.ColorIndex= 38 And sht.Cells(RR, "E").Value < 60 Then
sht.Cells(RR, "F").Value = "Still has chances"
ElseIf sht.Cells(RR, "D").Interior.ColorIndex= 38 And sht.Cells(RR, "E").Value = 100 Then
sht.Cells(RR, "F").Value = "Failed"
Else
sht.Cells(RR, "F").Value = "N/A"
End If
Next RR
End Sub
这是示例结果:
推荐阅读
- reactjs - ReactSelect 两个字段,我需要将它们的值保存在数组中
- angular - 订阅 AngularFireAuth 电子邮件验证事件
- rest - 如何使用 REST API 更新 VSTS 测试运行的失败测试和通过率字段?
- python - 使用 Python 从数据框中删除仅具有单个单词的列值
- php - 使用 .htaccess 删除 .php 并重定向到其非 .php 版本
- python-3.x - 将值作为对象而不是字符串值的方法
- ruby-on-rails - Rails 构建belongs_to 关联“必须存在”错误
- angular - 如何跟踪数据是否以完全反应的方式加载?
- c++ - C++ 根据调用的构造函数强制编译时错误
- android - 使用 Android 导航架构组件重新创建 backstack