我在for循环中有一个if语句,我试图使用like运算符将当前Cell值与其他字符串进行比较。出于某种原因,这给了我一个类型不匹配的错误。这是因为rng5.Cells(i, 1).Value
不是字符串吗?任何帮助将不胜感激!我已粘贴下面的原始代码。
For i = 1 To rng5.Rows.Count
If rng5.Cells(i, 1).Value Like "*test*" Or "*Test*" Or "*Demo*" Or "*demo*" Then
rng5.Cells(i, 1).EntireRow.Hidden = True
End If
Next i
答案 0 :(得分:2)
OR
运算符仅适用于布尔值,因此您必须将布尔值表示为布尔值。你不能省略像你这样的术语
For i = 1 To rng5.Rows.Count
If rng5.Cells(i, 1).Value Like "*test*" Or rng5.Cells(i, 1).Value Like "*Test*" Or rng5.Cells(i, 1).Value Like "*Demo*" Or rng5.Cells(i, 1).Value Like "*demo*" Then
rng5.Cells(i, 1).EntireRow.Hidden = True
End If
Next i
答案 1 :(得分:1)
使用Instr函数
For i = 1 To rng5.Rows.Count
If instr(1,rng5.Cells(i, 1).Value, "test")>0 Or instr(1,rng5.Cells(i, 1).Value, "Test")>0 Or instr(1,rng5.Cells(i, 1).Value, "demo")>0 Or instr(1,rng5.Cells(i, 1).Value, "Demo")>0 Then
rng5.Cells(i, 1).EntireRow.Hidden = True
End If
Next i
答案 2 :(得分:0)
如果需要,使用Lcase或Ucase验证数据的内容,使用类似无情感的
Sub test()
For i = 1 To rng5.Rows.Count
If LCase(rng5.Cells(i, 1).Value) Like "*test*" Or _
LCase(rng5.Cells(i, 1).Value) Like "*demo*" Then
rng5.Cells(i, 1).EntireRow.Hidden = True
End If
Next i
End Sub