Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 150
Default Matching strings with a pattern

Hi,

I have cells containing strings of the type "Mumbai - 400078" and
"Belgaum - 590011" with the following pattern:
Alphabetic string followed by space followed by hyphen followed by
space and then a numeric string. I want to fill such cells with a
color (interior.colorindex)? How do I identify such cells to the code?

Thanks in Advance for the help.

Regards,
Raj
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default Matching strings with a pattern

didn't give much information but maybe some kind of loop with

With Range("A1")
If InStr(1, .Value, " - ") 1 Then
..Interior.ColorIndex = 19
End If
End With



--


Gary Keramidas
Excel 2003


"Raj" wrote in message
...
Hi,

I have cells containing strings of the type "Mumbai - 400078" and
"Belgaum - 590011" with the following pattern:
Alphabetic string followed by space followed by hyphen followed by
space and then a numeric string. I want to fill such cells with a
color (interior.colorindex)? How do I identify such cells to the code?

Thanks in Advance for the help.

Regards,
Raj


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Matching strings with a pattern

On Sat, 27 Mar 2010 18:14:25 -0700 (PDT), Raj wrote:

Hi,

I have cells containing strings of the type "Mumbai - 400078" and
"Belgaum - 590011" with the following pattern:
Alphabetic string followed by space followed by hyphen followed by
space and then a numeric string. I want to fill such cells with a
color (interior.colorindex)? How do I identify such cells to the code?

Thanks in Advance for the help.

Regards,
Raj


Here's an example of one way of doing this using a regular expression to match
your pattern:

==========================================
Option Explicit
Sub cSpec()
Dim rg As Range, c As Range
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Pattern = "^[A-Z]+\s-\s\d+$"
re.Global = False
re.ignorecase = True
Set rg = Range("A1:A10")
For Each c In rg
If re.test(c) = True Then
c.Interior.ColorIndex = 3
Else
c.Interior.ColorIndex = xlColorIndexNone
End If
Next c
End Sub
=========================================
--ron
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Matching strings with a pattern

If your number string is **always** six digits long, you can do this...

Sub ColorCells()
Dim Cell As Range, YourRange As Range
Set YourRange = Worksheets("Sheet2").Range("A1:C100")
YourRange.Interior.ColorIndex = xlColorIndexNone
For Each Cell In YourRange
If Cell Like "* - ######" Then Cell.Interior.ColorIndex = 3
Next
End Sub

--
Rick (MVP - Excel)



"Raj" wrote in message
...
Hi,

I have cells containing strings of the type "Mumbai - 400078" and
"Belgaum - 590011" with the following pattern:
Alphabetic string followed by space followed by hyphen followed by
space and then a numeric string. I want to fill such cells with a
color (interior.colorindex)? How do I identify such cells to the code?

Thanks in Advance for the help.

Regards,
Raj


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 150
Default Matching strings with a pattern

Thanks a ton, everyone. I tried Rick's solution and it worked. I will
try out and study Ron's Regex solution next.

Regards,
Raj



On Mar 28, 7:55*am, "Rick Rothstein"
wrote:
If your number string is **always** six digits long, you can do this...

Sub ColorCells()
* Dim Cell As Range, YourRange As Range
* Set YourRange = Worksheets("Sheet2").Range("A1:C100")
* YourRange.Interior.ColorIndex = xlColorIndexNone
* For Each Cell In YourRange
* * If Cell Like "* - ######" Then Cell.Interior.ColorIndex = 3
* Next
End Sub

--
Rick (MVP - Excel)

"Raj" wrote in message

...

Hi,


I have cells containing strings of the type "Mumbai - 400078" and
"Belgaum - 590011" with the following pattern:
Alphabetic string followed by space followed by hyphen followed by
space and then a numeric string. I want to fill such cells with a
color (interior.colorindex)? How do I identify such cells to the code?


Thanks in Advance for the help.


Regards,
Raj




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Matching strings with a pattern

On Sat, 27 Mar 2010 22:55:14 -0400, "Rick Rothstein"
wrote:

If your number string is **always** six digits long, you can do this...

Sub ColorCells()
Dim Cell As Range, YourRange As Range
Set YourRange = Worksheets("Sheet2").Range("A1:C100")
YourRange.Interior.ColorIndex = xlColorIndexNone
For Each Cell In YourRange
If Cell Like "* - ######" Then Cell.Interior.ColorIndex = 3
Next
End Sub

--
Rick (MVP - Excel)


I interpreted the OP to mean by "alphabetic" that that string only contained
letters. Yours will match any character, or even no character, in the first
part of the string.

As a matter of fact, the "*" is superfluous in your pattern.
--ron
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Matching strings with a pattern

On Sun, 28 Mar 2010 06:49:12 -0400, Ron Rosenfeld
wrote:

I interpreted the OP to mean by "alphabetic" that that string only contained
letters.


I should have written "letters in the first part"

--ron
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Matching strings with a pattern

Ron has raised a good point about your original specification and what my
code does to meet it. You said that the part before the space/dash/space
should be alphabetic... my code allows that part of the text being tested to
be any characters, not just letters of the alphabet. The function below will
test the text to see if the first part is, in fact, alphabetic while the
second part is six digits (with both parts delimited by the space/dash/space
characters). So, use this instead of the code I posted earlier...

Sub ColorCells()
Dim Cell As Range, YourRange As Range
Set YourRange = Worksheets("Sheet2").Range("A1:C100")
YourRange.Interior.ColorIndex = xlColorIndexNone
For Each Cell In YourRange
If Not Cell Like "*[!A-Za-z]* - ######" And Cell Like _
"* - ######" Then Cell.Interior.ColorIndex = 3
Next
End Sub

--
Rick (MVP - Excel)



"Raj" wrote in message
...
Thanks a ton, everyone. I tried Rick's solution and it worked. I will
try out and study Ron's Regex solution next.

Regards,
Raj



On Mar 28, 7:55 am, "Rick Rothstein"
wrote:
If your number string is **always** six digits long, you can do this...

Sub ColorCells()
Dim Cell As Range, YourRange As Range
Set YourRange = Worksheets("Sheet2").Range("A1:C100")
YourRange.Interior.ColorIndex = xlColorIndexNone
For Each Cell In YourRange
If Cell Like "* - ######" Then Cell.Interior.ColorIndex = 3
Next
End Sub

--
Rick (MVP - Excel)

"Raj" wrote in message

...

Hi,


I have cells containing strings of the type "Mumbai - 400078" and
"Belgaum - 590011" with the following pattern:
Alphabetic string followed by space followed by hyphen followed by
space and then a numeric string. I want to fill such cells with a
color (interior.colorindex)? How do I identify such cells to the code?


Thanks in Advance for the help.


Regards,
Raj


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Matching strings with a pattern

If your number string is **always** six digits long, you can do this...

Sub ColorCells()
Dim Cell As Range, YourRange As Range
Set YourRange = Worksheets("Sheet2").Range("A1:C100")
YourRange.Interior.ColorIndex = xlColorIndexNone
For Each Cell In YourRange
If Cell Like "* - ######" Then Cell.Interior.ColorIndex = 3
Next
End Sub


I interpreted the OP to mean by "alphabetic" that that string only
contained
letters. Yours will match any character, or even no character, in the
first
part of the string.

As a matter of fact, the "*" is superfluous in your pattern.


Good point Ron! I completely overlooked that. I just posted some corrected
code for the OP (still using the Like operator test method).

Going back to my original code though... what did you mean the "*" is
superfluous in your pattern? If we leave it out, then this test will fail...

If "Mumbai - 400078" like " - ######" Then

Or did I miss the point of your comment?

--
Rick (MVP - Excel)

  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Matching strings with a pattern

On Sun, 28 Mar 2010 10:36:21 -0400, "Rick Rothstein"
wrote:

Good point Ron! I completely overlooked that. I just posted some corrected
code for the OP (still using the Like operator test method).

Going back to my original code though... what did you mean the "*" is
superfluous in your pattern? If we leave it out, then this test will fail...

If "Mumbai - 400078" like " - ######" Then

Or did I miss the point of your comment?

--
Rick (MVP - Excel)


I was not clear (in either my writing or my own mind :-)

I was trying to express that your original code, as well as your current code,
will match a string with zero characters prior to the <space<hyphen<space

In other words, a string: ' - 123456' will match (in both of your code
examples).

Also, and I don't know how this may apply to the OP's problem, your code allows
a first string of any length, including zero, and the second string must be
exactly six digits (which you did specify in your initial post).

In mine, both strings must be at least 1 character, with no upper limit. But
it would be trivial to add more restrictive length parameters. For example, if
six digits were required at the end, then:

re.Pattern = "^[A-Z]+\s-\s\d+$"

gets changed to

re.Pattern = "^[A-Z]+\s-\s\d{6}$"

--ron


  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Matching strings with a pattern

One more change in response to a comment by Ron. It seems that my code will
allow no characters to appear before the space/dash/space and that is should
require at least one character (or more) to be in that position. Ron raises
a good point, so here is the adjusted code to cater to this requirement as
well...

Sub ColorCells()
Dim Cell As Range, YourRange As Range
Set YourRange = Worksheets("Sheet2").Range("A1:C100")
YourRange.Interior.ColorIndex = xlColorIndexNone
For Each Cell In YourRange
If Not Cell Like "*[!A-Za-z]* - ######" And Cell Like "* - ######" _
And Cell Like "?* - *" Then Cell.Interior.ColorIndex = 3
Next
End Sub

--
Rick (MVP - Excel)



"Rick Rothstein" wrote in message
...
Ron has raised a good point about your original specification and what my
code does to meet it. You said that the part before the space/dash/space
should be alphabetic... my code allows that part of the text being tested
to be any characters, not just letters of the alphabet. The function below
will test the text to see if the first part is, in fact, alphabetic while
the second part is six digits (with both parts delimited by the
space/dash/space characters). So, use this instead of the code I posted
earlier...

Sub ColorCells()
Dim Cell As Range, YourRange As Range
Set YourRange = Worksheets("Sheet2").Range("A1:C100")
YourRange.Interior.ColorIndex = xlColorIndexNone
For Each Cell In YourRange
If Not Cell Like "*[!A-Za-z]* - ######" And Cell Like _
"* - ######" Then Cell.Interior.ColorIndex = 3
Next
End Sub

--
Rick (MVP - Excel)



"Raj" wrote in message
...
Thanks a ton, everyone. I tried Rick's solution and it worked. I will
try out and study Ron's Regex solution next.

Regards,
Raj



On Mar 28, 7:55 am, "Rick Rothstein"
wrote:
If your number string is **always** six digits long, you can do this...

Sub ColorCells()
Dim Cell As Range, YourRange As Range
Set YourRange = Worksheets("Sheet2").Range("A1:C100")
YourRange.Interior.ColorIndex = xlColorIndexNone
For Each Cell In YourRange
If Cell Like "* - ######" Then Cell.Interior.ColorIndex = 3
Next
End Sub

--
Rick (MVP - Excel)

"Raj" wrote in message

...

Hi,

I have cells containing strings of the type "Mumbai - 400078" and
"Belgaum - 590011" with the following pattern:
Alphabetic string followed by space followed by hyphen followed by
space and then a numeric string. I want to fill such cells with a
color (interior.colorindex)? How do I identify such cells to the code?

Thanks in Advance for the help.

Regards,
Raj


  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Matching strings with a pattern

Good point Ron! I completely overlooked that. I just posted some corrected
code for the OP (still using the Like operator test method).

Going back to my original code though... what did you mean the "*" is
superfluous in your pattern? If we leave it out, then this test will
fail...

If "Mumbai - 400078" like " - ######" Then

Or did I miss the point of your comment?


I was not clear (in either my writing or my own mind :-)

I was trying to express that your original code, as well as your current
code,
will match a string with zero characters prior to the
<space<hyphen<space

In other words, a string: ' - 123456' will match (in both of your code
examples).

Also, and I don't know how this may apply to the OP's problem, your code
allows
a first string of any length, including zero, and the second string must
be
exactly six digits (which you did specify in your initial post).


That's a reasonable observation... thanks for noting it. I just posted
revised code to make sure at least one character exists before the
space/dash/space.

--
Rick (MVP - Excel)

  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Matching strings with a pattern

On Sun, 28 Mar 2010 12:47:20 -0400, "Rick Rothstein"
wrote:

That's a reasonable observation... thanks for noting it. I just posted
revised code to make sure at least one character exists before the
space/dash/space.


I noted your last revision. Of course, these kinds of modifications seem
simpler to me with regular expressions, than with sequential Like statements.
But that's just a matter of taste.
--ron
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
Pattern Matching [email protected] Excel Discussion (Misc queries) 6 September 24th 07 09:19 PM
Pattern Matching using LIKE ? Jakobshavn Isbrae Excel Programming 2 December 31st 06 01:18 PM
VBE replace with pattern matching? RB Smissaert Excel Programming 2 August 13th 06 02:18 PM
pattern matching possible? vito Excel Worksheet Functions 1 May 16th 06 05:03 PM
Pattern Matching with cells function John[_80_] Excel Programming 2 June 29th 04 04:51 AM


All times are GMT +1. The time now is 08:18 PM.

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

About Us

"It's about Microsoft Excel"