I have an if statement in a for loop in which I am trying to compare the current Cell value to other strings using the like operator. For some reason this is giving me a type mismatch error. Is this because the rng5.Cells(i, 1).Value
is not a string? Any help would be greatly appreciated! I have pasted the original code below.
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
The OR
operator works only on booleans, so you must present booleans to it. You cant ommit terms like you did
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
use the Instr Function
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
use Lcase or Ucase if required verify on content of the data using like without case sensative
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.