Count number of cells with Space & "Text" and "text" & space

drom

Well-known Member
Joined
Mar 20, 2005
Messages
527
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Hi and Thanks in advance!

I have a Column with sentences and/or...

I am Trying to get using VBA how many of the cells in a range of cells (a column, Eg: A5:A1594)
  • Have a string ending with: " " & "Hello"
  • Have a string starting with: "Hello "
  • Conmtaing the string : " Hello "
  • Having the string with: "*" & "Hello" & "*" 'So Hello no matter where 'I know how to get this one

I am trying to use:
wCell="Hello"
set Rng=range("A5:A1594")


VBA Code:
        xHowManyStartingWithHello = 0:               xHowManyStartingWithHello = Application.WorksheetFunction.WorksheetFunction(Rng, "*" & " " & wCell)   'Should be: 53, I get = 0
        xHowManyEndingWithHello = 0:                 xHowManyEndingWithHello = Application.WorksheetFunction.WorksheetFunction(Rng, wCell & " *")          'Should be: 5,  I get = 0
        xHowManyHelloInTotal = 0:                    xHowManyHelloInTotal = Application.WorksheetFunction.CountIf(Rng, "*" & wCell & "*")                  'Should be: 64, I get = 64
        xHowManyHelloInside = 0:                     xHowManyHelloInside = Application.WorksheetFunction.WorksheetFunction(Rng, "* " &  wCell & " *" )     'Should be: 6,  I get = 0

So I am doing something wrong

I do not want to use for Each cell in Rng.cells ...



But I do not get the right answer
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
The lines that don't work are missing Countif, you've typed WorksheetFunction twice instead.

Also, you have the first 2 around the wrong way. It should be "* " & wCell for ending with and wCell & " *" for starting with.

Final tip, for the total you should add the other 3 results together instead of counting again. If you put the spaces into that one it will not work, but without them there is a risk of partial matches being counted in error (for example if wCell contained "apple" then "pineapple" would be counted by your current total line but not by the others).

VBA Code:
        xHowManyStartingWithHello = Application.WorksheetFunction.Countif(Rng, wCell & " *")
        xHowManyEndingWithHello = Application.WorksheetFunction.Countif(Rng, "* " & wCell)
        xHowManyHelloInside = Application.WorksheetFunction.Countif(Rng, "* " &  wCell & " *" )
        xHowManyHelloInTotal = xHowManyStartingWithHello +  xHowManyEndingWithHello + xHowManyHelloInside
If that fails then it likely means that wCell also contains spaces which would cause the code to look for non-existent double spaces in the count range.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top