PC Review


Reply
Thread Tools Rate Thread

Cell search, match part of string

 
 
=?Utf-8?B?QTFwcm8=?=
Guest
Posts: n/a
 
      28th Mar 2007
I've got two spreadsheets that have a column with the same data, more or less.
i.e. spreadsheet 1 has "Gloucester" and the spreadsheet 2 "Gloucester "

so i've got this bit of macro
where sitename comes from spreadsheet 1 and is a string and
osheet.Range("A" & counter) comes from spreadsheet 2.

For counter = 1 To 50 Step 1

If sitename = osheet.Range("A" & counter) Then

do stuff

Next counter

The problem is that with the example above, i.e. spreadsheet 1 has
"Gloucester" and the spreadsheet 2 "Gloucester ", the if will never be true

Is there any way of getting around this.

Maybe removing the empty spaces at the end of a cell?

TIA
 
Reply With Quote
 
 
 
 
=?Utf-8?B?QTFwcm8=?=
Guest
Posts: n/a
 
      28th Mar 2007
trim(sitename) does the trick

google is my friend I guess

"A1pro" wrote:

> I've got two spreadsheets that have a column with the same data, more or less.
> i.e. spreadsheet 1 has "Gloucester" and the spreadsheet 2 "Gloucester "
>
> so i've got this bit of macro
> where sitename comes from spreadsheet 1 and is a string and
> osheet.Range("A" & counter) comes from spreadsheet 2.
>
> For counter = 1 To 50 Step 1
>
> If sitename = osheet.Range("A" & counter) Then
>
> do stuff
>
> Next counter
>
> The problem is that with the example above, i.e. spreadsheet 1 has
> "Gloucester" and the spreadsheet 2 "Gloucester ", the if will never be true
>
> Is there any way of getting around this.
>
> Maybe removing the empty spaces at the end of a cell?
>
> TIA

 
Reply With Quote
 
=?Utf-8?B?VmVyZ2VsIEFkcmlhbm8=?=
Guest
Posts: n/a
 
      28th Mar 2007
Hi,

Maybe the following would give you some ideas

1. To ignore leading and trailing spaces:

If trim(sitename) = trim(osheet.Range("A" & counter))

2. To apply #1 AND ignore the case

If ucase(trim(sitename)) = ucase(trim(osheet.Range("A" & counter)))

3. If you want to ignore only the spaces at the end of the string, use Rtrim
and if you want to ignore only the spaces at the beginning, use Ltrim.




--
Hope that helps.

Vergel Adriano


"A1pro" wrote:

> I've got two spreadsheets that have a column with the same data, more or less.
> i.e. spreadsheet 1 has "Gloucester" and the spreadsheet 2 "Gloucester "
>
> so i've got this bit of macro
> where sitename comes from spreadsheet 1 and is a string and
> osheet.Range("A" & counter) comes from spreadsheet 2.
>
> For counter = 1 To 50 Step 1
>
> If sitename = osheet.Range("A" & counter) Then
>
> do stuff
>
> Next counter
>
> The problem is that with the example above, i.e. spreadsheet 1 has
> "Gloucester" and the spreadsheet 2 "Gloucester ", the if will never be true
>
> Is there any way of getting around this.
>
> Maybe removing the empty spaces at the end of a cell?
>
> TIA

 
Reply With Quote
 
=?Utf-8?B?Sm9lbA==?=
Guest
Posts: n/a
 
      28th Mar 2007
Try this
If sitename = osheet.clean(Range("A" & counter)) Then

"A1pro" wrote:

> I've got two spreadsheets that have a column with the same data, more or less.
> i.e. spreadsheet 1 has "Gloucester" and the spreadsheet 2 "Gloucester "
>
> so i've got this bit of macro
> where sitename comes from spreadsheet 1 and is a string and
> osheet.Range("A" & counter) comes from spreadsheet 2.
>
> For counter = 1 To 50 Step 1
>
> If sitename = osheet.Range("A" & counter) Then
>
> do stuff
>
> Next counter
>
> The problem is that with the example above, i.e. spreadsheet 1 has
> "Gloucester" and the spreadsheet 2 "Gloucester ", the if will never be true
>
> Is there any way of getting around this.
>
> Maybe removing the empty spaces at the end of a cell?
>
> TIA

 
Reply With Quote
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      28th Mar 2007
Joel,

Just a heads up -

Clean only does non-printing characters (and i suspect only those below
128).

a = " a "
? len(a)
3
? len(Application.Clean(a))
3
? len(trim(a))
1

(it doesn't clean chr(160), "nbsp" often copied in from the internet. as an
example)

--
Regards,
Tom Ogilvy


"Joel" wrote:

> Try this
> If sitename = osheet.clean(Range("A" & counter)) Then
>
> "A1pro" wrote:
>
> > I've got two spreadsheets that have a column with the same data, more or less.
> > i.e. spreadsheet 1 has "Gloucester" and the spreadsheet 2 "Gloucester "
> >
> > so i've got this bit of macro
> > where sitename comes from spreadsheet 1 and is a string and
> > osheet.Range("A" & counter) comes from spreadsheet 2.
> >
> > For counter = 1 To 50 Step 1
> >
> > If sitename = osheet.Range("A" & counter) Then
> >
> > do stuff
> >
> > Next counter
> >
> > The problem is that with the example above, i.e. spreadsheet 1 has
> > "Gloucester" and the spreadsheet 2 "Gloucester ", the if will never be true
> >
> > Is there any way of getting around this.
> >
> > Maybe removing the empty spaces at the end of a cell?
> >
> > TIA

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Vlookup and match part of text in a cell Joe Phan Microsoft Excel Worksheet Functions 3 4th May 2010 04:11 AM
Search/Match/Find ANY part of string to ANY part of Cell Value TWhizTom Microsoft Excel Worksheet Functions 0 21st Jul 2008 08:16 PM
string/regex: extracting the context of a string match around the found search term? Microsoft C# .NET 3 5th Jul 2007 02:55 PM
need to search row by part of string yuvalbra Microsoft Access Queries 1 24th Jun 2007 01:48 PM
If Match Part of Text Within Cell, Then Blank =?Utf-8?B?U3RldmVD?= Microsoft Excel Worksheet Functions 3 12th May 2006 03:16 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:56 AM.