Like Operator * don't work using a string

Mory19

New Member
Joined
Nov 28, 2022
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Hi I am new learning vba trying to automate my reports at my job. In this case I'm trying to first make a simple code and learn with static range and after adding other functions in this case use a if conditional with like and * operator use a value and a range with a loop comparing the text inside the cells, my code is below and when i run the code the message box tell me the cells are not like my "a" value and only yes when the cell content is exact (a2 cell=f2 cell). Can't understand if I forget about something or my code is bad writing. Thank you very much in advance
imagen_2022-12-04_170140823.png


VBA Code:
Sub test4()

Dim codeos As Range, a As String, cell As Range
a = [a2].Value
Set codeos = Range("f2:f5")

For Each cell In codeos

  If a Like "*" & cell & "*" Then 
    MsgBox "Yes"
  Else
    MsgBox "No"
  End If
  
Next cell

End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi
Try changing your code so this one
VBA Code:
Option Explicit

Sub test4()
Dim codeos As Range, a As String, cell As Range
a = [a2]
Set codeos = Range("f2:f5")
For Each cell In codeos
  If cell Like "*" & a & "*" Then
    MsgBox "Yes"
  Else
    MsgBox "No"
  End If
Next cell
End Sub
Hi,
Mario
 
Upvote 0
Solution
Hi
Try changing your code so this one
VBA Code:
Option Explicit

Sub test4()
Dim codeos As Range, a As String, cell As Range
a = [a2]
Set codeos = Range("f2:f5")
For Each cell In codeos
  If cell Like "*" & a & "*" Then
    MsgBox "Yes"
  Else
    MsgBox "No"
  End If
Next cell
End Sub
Hi,
Mario
Thank you very much Mario for help i didn't notice the order in the conditional, works perfect :)
Regards, Cristobal
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top