Wildcard in VBA?

  • Follow


I apologize if this is here twice--I don't see the post I thought went 
through earlier today.  Anyway ..

I have the following code:  

If .Cells(X, "H") = "RG" Then
           .Cells(X, "G").Value = "TRC"
End If

Can I modify this with a "wildcard" character?  In other words, if RG is 
found anywhere (RG123, RG456, 123RG, etc) within cell H, I want TRC to be 
placed into column G

Thanks!
0
Reply Utf 1/15/2010 1:06:01 AM

maybe something like this, i just hard coded the variable for testing

Sub test()
Dim x As Long
x = 3

If Cells(x, "H") Like "?G" Then
           Cells(x, "G").Value = "TRC"
End If

End Sub

-- 


Gary Keramidas
Excel 2003


"richzip" <richzip@discussions.microsoft.com> wrote in message 
news:A86712BF-A835-41B6-8BCD-038648805CEE@microsoft.com...
>I apologize if this is here twice--I don't see the post I thought went
> through earlier today.  Anyway ..
>
> I have the following code:
>
> If .Cells(X, "H") = "RG" Then
>           .Cells(X, "G").Value = "TRC"
> End If
>
> Can I modify this with a "wildcard" character?  In other words, if RG is
> found anywhere (RG123, RG456, 123RG, etc) within cell H, I want TRC to be
> placed into column G
>
> Thanks! 

0
Reply Gary 1/15/2010 1:33:01 AM


Try something like

If InStr(Cells(X, "H").Text, "RG") > 0 Then
    Cells(X, "G").Value = "TRC"
End If

Steve

"richzip" <richzip@discussions.microsoft.com> wrote in message 
news:A86712BF-A835-41B6-8BCD-038648805CEE@microsoft.com...
> I apologize if this is here twice--I don't see the post I thought went
> through earlier today.  Anyway ..
>
> I have the following code:
>
> If .Cells(X, "H") = "RG" Then
>           .Cells(X, "G").Value = "TRC"
> End If
>
> Can I modify this with a "wildcard" character?  In other words, if RG is
> found anywhere (RG123, RG456, 123RG, etc) within cell H, I want TRC to be
> placed into column G
>
> Thanks! 

0
Reply Steve 1/15/2010 1:53:01 AM

Given what the OP posted, I believe your Like operator test should be =
this...

If Cells(x, "H") Like "*RG*" Then

That would be a case sensitive test; if the OP need a non-case sensitive =
test, that would look like this...

If Cells(x, "H") Like "*[Rr][Gg]*" Then

--=20
Rick (MVP - Excel)


"Gary Keramidas" <GKeramidasAtMSN.com> wrote in message =
news:uLVUwKYlKHA.4872@TK2MSFTNGP05.phx.gbl...
> maybe something like this, i just hard coded the variable for testing
>=20
> Sub test()
> Dim x As Long
> x =3D 3
>=20
> If Cells(x, "H") Like "?G" Then
>           Cells(x, "G").Value =3D "TRC"
> End If
>=20
> End Sub
>=20
> --=20
>=20
>=20
> Gary Keramidas
> Excel 2003
>=20
>=20
> "richzip" <richzip@discussions.microsoft.com> wrote in message=20
> news:A86712BF-A835-41B6-8BCD-038648805CEE@microsoft.com...
>>I apologize if this is here twice--I don't see the post I thought went
>> through earlier today.  Anyway ..
>>
>> I have the following code:
>>
>> If .Cells(X, "H") =3D "RG" Then
>>           .Cells(X, "G").Value =3D "TRC"
>> End If
>>
>> Can I modify this with a "wildcard" character?  In other words, if RG =
is
>> found anywhere (RG123, RG456, 123RG, etc) within cell H, I want TRC =
to be
>> placed into column G
>>
>> Thanks!=20
>
0
Reply Rick 1/15/2010 4:00:27 AM

yea, i missed this part (RG123, RG456, 123RG).
i guess i should read the entire post<g>

-- 


Gary Keramidas
Excel 2003


"Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message 
news:OZvB9cZlKHA.2164@TK2MSFTNGP02.phx.gbl...
Given what the OP posted, I believe your Like operator test should be this...

If Cells(x, "H") Like "*RG*" Then

That would be a case sensitive test; if the OP need a non-case sensitive test, 
that would look like this...

If Cells(x, "H") Like "*[Rr][Gg]*" Then

-- 
Rick (MVP - Excel)


"Gary Keramidas" <GKeramidasAtMSN.com> wrote in message 
news:uLVUwKYlKHA.4872@TK2MSFTNGP05.phx.gbl...
> maybe something like this, i just hard coded the variable for testing
>
> Sub test()
> Dim x As Long
> x = 3
>
> If Cells(x, "H") Like "?G" Then
>           Cells(x, "G").Value = "TRC"
> End If
>
> End Sub
>
> -- 
>
>
> Gary Keramidas
> Excel 2003
>
>
> "richzip" <richzip@discussions.microsoft.com> wrote in message
> news:A86712BF-A835-41B6-8BCD-038648805CEE@microsoft.com...
>>I apologize if this is here twice--I don't see the post I thought went
>> through earlier today.  Anyway ..
>>
>> I have the following code:
>>
>> If .Cells(X, "H") = "RG" Then
>>           .Cells(X, "G").Value = "TRC"
>> End If
>>
>> Can I modify this with a "wildcard" character?  In other words, if RG is
>> found anywhere (RG123, RG456, 123RG, etc) within cell H, I want TRC to be
>> placed into column G
>>
>> Thanks!
> 

0
Reply Gary 1/15/2010 4:22:30 AM

4 Replies
1928 Views

(page loaded in 1.604 seconds)


Reply: