Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 40
Default Comparing cell contents via VB

Hello,

I was wondering if anyone could suggest a way of writing code in VB to
compare the texts in cells.

For example if, in Column A, going down from row 1 to 4, the values/strings
were;

Cell A1 had the word Apple
Cell A2 had the word Peachy
Cell A3 had the word Plopped
Cell A4 had the word Hate

If I wanted to know how many "p"s appeared in each of the words, and the
answer went in the adjacent column, what code could i use?

So the end result would then have column B as,

Cell B1 has the result 2
Cell B2 has the result 1
Cell B3 has the result 3
Cell B4 has the result 0


Many thanks for your help - please make the code as simple or as complicated
as you will, or as long or as short as you see fit. Thanks

Rajen
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 39
Default Comparing cell contents via VB

On Jun 29, 10:44 am, RajenRajput1
wrote:
Hello,

I was wondering if anyone could suggest a way of writing code in VB to
compare the texts in cells.

For example if, in Column A, going down from row 1 to 4, the values/strings
were;

Cell A1 had the word Apple
Cell A2 had the word Peachy
Cell A3 had the word Plopped
Cell A4 had the word Hate

If I wanted to know how many "p"s appeared in each of the words, and the
answer went in the adjacent column, what code could i use?

So the end result would then have column B as,

Cell B1 has the result 2
Cell B2 has the result 1
Cell B3 has the result 3
Cell B4 has the result 0

Many thanks for your help - please make the code as simple or as complicated
as you will, or as long or as short as you see fit. Thanks

Rajen


You don't need VBA to do what you're proposing, but you can use it if
you like.
If you put the following formula in cell B1, it'll return the same
result:
LEN(A1)-LEN(SUBSTITUTE(A1,"p",""))

In VBA, you could use:
Sub Find_Letter
Dim strletter as string
Dim rng as range

strletter = "p"
For each rng in selection
rng.offset(0,1).value = len(rng.value)-
len(replace(rng.value,strletter,vbnullstring))
Next rng

End Sub

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 39
Default Comparing cell contents via VB

On Jun 29, 10:44 am, RajenRajput1
wrote:
Hello,

I was wondering if anyone could suggest a way of writing code in VB to
compare the texts in cells.

For example if, in Column A, going down from row 1 to 4, the values/strings
were;

Cell A1 had the word Apple
Cell A2 had the word Peachy
Cell A3 had the word Plopped
Cell A4 had the word Hate

If I wanted to know how many "p"s appeared in each of the words, and the
answer went in the adjacent column, what code could i use?

So the end result would then have column B as,

Cell B1 has the result 2
Cell B2 has the result 1
Cell B3 has the result 3
Cell B4 has the result 0

Many thanks for your help - please make the code as simple or as complicated
as you will, or as long or as short as you see fit. Thanks

Rajen

You don't need VBA to do what you're proposing, but you can use it if
you like.
If you put the following formula in cell B1, it'll return the same
result:
LEN(A1)-LEN(SUBSTITUTE(A1,"p",""))

In VBA, you could use:
Sub Find_Letter
Dim strletter as string
Dim rng as range

strletter = "p"
For each rng in selection
rng.offset(0,1).value = len(rng.value)-
len(replace(rng.value,strletter,vbnullstring))
Next rng

End Sub

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 40
Default Comparing cell contents via VB

That's really good - I'm gonna mess around with it now.

Coming from a QBasic background (ancient), some of the terms in the code I
am unfamiliar with, but I'll play and see what they do.

Looks good though - Any other suggestions from anyone are welcome

Rajen

"meatshield" wrote:

On Jun 29, 10:44 am, RajenRajput1
wrote:
Hello,

I was wondering if anyone could suggest a way of writing code in VB to
compare the texts in cells.

For example if, in Column A, going down from row 1 to 4, the values/strings
were;

Cell A1 had the word Apple
Cell A2 had the word Peachy
Cell A3 had the word Plopped
Cell A4 had the word Hate

If I wanted to know how many "p"s appeared in each of the words, and the
answer went in the adjacent column, what code could i use?

So the end result would then have column B as,

Cell B1 has the result 2
Cell B2 has the result 1
Cell B3 has the result 3
Cell B4 has the result 0

Many thanks for your help - please make the code as simple or as complicated
as you will, or as long or as short as you see fit. Thanks

Rajen

You don't need VBA to do what you're proposing, but you can use it if
you like.
If you put the following formula in cell B1, it'll return the same
result:
LEN(A1)-LEN(SUBSTITUTE(A1,"p",""))

In VBA, you could use:
Sub Find_Letter
Dim strletter as string
Dim rng as range

strletter = "p"
For each rng in selection
rng.offset(0,1).value = len(rng.value)-
len(replace(rng.value,strletter,vbnullstring))
Next rng

End Sub


  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 40
Default Comparing cell contents via VB

Your formula code "Substitute" is brilliant. Never used it before, but I
understand the logic. Brilliant way of doing it.

"meatshield" wrote:

On Jun 29, 10:44 am, RajenRajput1
wrote:
Hello,

I was wondering if anyone could suggest a way of writing code in VB to
compare the texts in cells.

For example if, in Column A, going down from row 1 to 4, the values/strings
were;

Cell A1 had the word Apple
Cell A2 had the word Peachy
Cell A3 had the word Plopped
Cell A4 had the word Hate

If I wanted to know how many "p"s appeared in each of the words, and the
answer went in the adjacent column, what code could i use?

So the end result would then have column B as,

Cell B1 has the result 2
Cell B2 has the result 1
Cell B3 has the result 3
Cell B4 has the result 0

Many thanks for your help - please make the code as simple or as complicated
as you will, or as long or as short as you see fit. Thanks

Rajen


You don't need VBA to do what you're proposing, but you can use it if
you like.
If you put the following formula in cell B1, it'll return the same
result:
LEN(A1)-LEN(SUBSTITUTE(A1,"p",""))

In VBA, you could use:
Sub Find_Letter
Dim strletter as string
Dim rng as range

strletter = "p"
For each rng in selection
rng.offset(0,1).value = len(rng.value)-
len(replace(rng.value,strletter,vbnullstring))
Next rng

End Sub




  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 39
Default Comparing cell contents via VB

On Jun 29, 11:56 am, RajenRajput1
wrote:
Your formula code "Substitute" is brilliant. Never used it before, but I
understand the logic. Brilliant way of doing it.

"meatshield" wrote:
On Jun 29, 10:44 am, RajenRajput1
wrote:
Hello,


I was wondering if anyone could suggest a way of writing code in VB to
compare the texts in cells.


For example if, in Column A, going down from row 1 to 4, the values/strings
were;


Cell A1 had the word Apple
Cell A2 had the word Peachy
Cell A3 had the word Plopped
Cell A4 had the word Hate


If I wanted to know how many "p"s appeared in each of the words, and the
answer went in the adjacent column, what code could i use?


So the end result would then have column B as,


Cell B1 has the result 2
Cell B2 has the result 1
Cell B3 has the result 3
Cell B4 has the result 0


Many thanks for your help - please make the code as simple or as complicated
as you will, or as long or as short as you see fit. Thanks


Rajen


You don't need VBA to do what you're proposing, but you can use it if
you like.
If you put the following formula in cell B1, it'll return the same
result:
LEN(A1)-LEN(SUBSTITUTE(A1,"p",""))


In VBA, you could use:
Sub Find_Letter
Dim strletter as string
Dim rng as range


strletter = "p"
For each rng in selection
rng.offset(0,1).value = len(rng.value)-
len(replace(rng.value,strletter,vbnullstring))
Next rng


End Sub


I wish I could take credit for thinking of it. =) I came across that
little trick before and filed it away because I thought it was pretty
clever.

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
Comparing Cell Contents in 2 or More Columns ConfusedNHouston Excel Discussion (Misc queries) 2 June 18th 07 03:08 PM
Comparing Field Contents Wayne Taylor Excel Discussion (Misc queries) 2 February 26th 07 10:40 AM
Comparing cell contents with different reference cells Martin B Excel Worksheet Functions 3 November 22nd 06 07:10 PM
Comparing contents of two spreadsheets and outputting results to a brx Excel Worksheet Functions 5 March 17th 05 01:44 AM
Comparing Workbook contents SMC Excel Discussion (Misc queries) 1 January 5th 05 09:48 PM


All times are GMT +1. The time now is 04:50 PM.

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"