Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 74
Default Search for string containing

Hello,

I have the following in VBA:

If .Cells(X, "K") = "RG" Then
.Cells(X, "Q").Value = "TRC"

How can I modify this formula to look in column K for a string that contains
RG and perform the same function? (i.e., if it contains RG-TRVL or TRVL-RG,
then TRC should automatically be placed in column Q)?

Thank you.
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,651
Default Search for string containing

On Thu, 14 Jan 2010 12:25:01 -0800, richzip
wrote:

Hello,

I have the following in VBA:

If .Cells(X, "K") = "RG" Then
.Cells(X, "Q").Value = "TRC"

How can I modify this formula to look in column K for a string that contains
RG and perform the same function? (i.e., if it contains RG-TRVL or TRVL-RG,
then TRC should automatically be placed in column Q)?

Thank you.



Use the InStr function. If it returns a non-zero, your substring is contained
in the string.
--ron
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Search for string containing

Take a look at Like or InStr in VBA's help

if ucase(.cells(x,"K").value) like "*RG*" then
or
if instr(1,.cells(x,"K").value,"RG",vbTextCompare) 0 then

(InStr has a parm that you can specify to ignore case (if you want).)


richzip wrote:

Hello,

I have the following in VBA:

If .Cells(X, "K") = "RG" Then
.Cells(X, "Q").Value = "TRC"

How can I modify this formula to look in column K for a string that contains
RG and perform the same function? (i.e., if it contains RG-TRVL or TRVL-RG,
then TRC should automatically be placed in column Q)?

Thank you.


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 269
Default Search for string containing

The line

If InStr(.Cells(X, "K"), "RG") 0 Then .Cells(X, "Q").Value = "TRC"

should do the trick
--
If this helps, please remember to click yes.


"richzip" wrote:

Hello,

I have the following in VBA:

If .Cells(X, "K") = "RG" Then
.Cells(X, "Q").Value = "TRC"

How can I modify this formula to look in column K for a string that contains
RG and perform the same function? (i.e., if it contains RG-TRVL or TRVL-RG,
then TRC should automatically be placed in column Q)?

Thank you.

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,203
Default Search for string containing

Here's part of code that I'd probably set up to do it:

Dim testRange as Range
Dim anyTestCell as Range

Set testRange = ActiveSheet.Range("K1:" & _
ActiveSheet.Range("K" & Rows.Count).End(xlup).Address)
For Each anyTestCell in testRange
If Instr(anyTestCell,"RG")0 Then
Range("Q" & anyTestCell.Row) = "TRC"
End IF
Next ' end anyTestCell loop
.... more code
'good housekeeping:
Set testRange = Nothing
....
End Sub



"richzip" wrote:

Hello,

I have the following in VBA:

If .Cells(X, "K") = "RG" Then
.Cells(X, "Q").Value = "TRC"

How can I modify this formula to look in column K for a string that contains
RG and perform the same function? (i.e., if it contains RG-TRVL or TRVL-RG,
then TRC should automatically be placed in column Q)?

Thank you.



  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,203
Default Search for string containing

For you exact statement, I think you could rewrite it this way:

If Instr(.Cells(X, "K"),"RG") 0 Then
.Cells(X,"Q").Value = "TRC"
End If

"richzip" wrote:

Hello,

I have the following in VBA:

If .Cells(X, "K") = "RG" Then
.Cells(X, "Q").Value = "TRC"

How can I modify this formula to look in column K for a string that contains
RG and perform the same function? (i.e., if it contains RG-TRVL or TRVL-RG,
then TRC should automatically be placed in column Q)?

Thank you.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
search string for number value samuel Excel Discussion (Misc queries) 3 May 13th 08 10:05 PM
How to search a string from the right ? EuroMaverick Setting up and Configuration of Excel 2 March 28th 07 09:20 PM
to search for a string and affect data if it finds the string? Shwaman Excel Worksheet Functions 1 January 11th 06 12:56 AM
Q: search in string JIM.H. Excel Discussion (Misc queries) 5 January 5th 05 10:24 PM
Segregating a Search String RTP Excel Discussion (Misc queries) 1 December 22nd 04 07:04 PM


All times are GMT +1. The time now is 04:21 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"