Wildcards in a pattern using a variable s in a text string to find a single value

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
917
Office Version
  1. 365
Platform
  1. Windows
In this simple sentence which is in a userform textbox:
Code:
He he left his home and traveled eastward toward the land of the east to settle there. Although there were many beasts in that
area he still wanted to live east of where he came from, becoming the least favorite of all his brothers.
The sentence above has five occurrences containing 'east'. Two, eastward and beasts have 'east' contained within the word.
I just want to find all occurenes of the single, standalone word 'east', not contained within any other words.
This code finds every single occurrence:

Code:
"*" & s & "*"
eastward
east
beasts
east
least
The correct wildcard pattern would give only two occurrences of 'east':
east
east

Worked on this using many combinations of quotes, asterikes, etc., still not getting the correct wildcard pattern.
The variable s is the value taken from a userform textbox in the image below
Dim s As String
s = Me.TextBox1.Value
cnt = Application.CountIf(Range("E1", Range("E" & Rows.count).End(xlUp)), "*" & s & "*")

Thanks for anyone's help.
cr
 

Attachments

  • USERFORM TEXTBOX VALUE .png
    USERFORM TEXTBOX VALUE .png
    20.4 KB · Views: 4

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I have not used a userform textbox but here are a couple of ideas to try. It is also not clear whether your count should be case-sensitive or not.

VBA Code:
Sub Count_v1()
  Dim s As String, myword As String
  Dim cnt As Long
 
  myword = "east"
  s = "He he left his home and traveled eastward toward the land of the east to settle there. Although there were many beasts in that area he still wanted to live east of where he came from, becoming the least favorite of all his brothers."
  s = " " & Replace(s, " ", "  ") & " "
  cnt = (Len(s) - Len(Replace(s, " " & myword & " ", ""))) / (Len(myword) + 2)
End Sub

Sub Count_v2()
  Dim s As String, myword As String
  Dim cnt As Long
 
  myword = "east"
  s = "He he left his home and traveled eastward toward the land of the east to settle there. Although there were many beasts in that area he still wanted to live east of where he came from, becoming the least favorite of all his brothers."
  With CreateObject("VBScript.RegExp")
    .Global = True
    .IgnoreCase = True
    .Pattern = "\b" & myword & "\b"
    cnt = .Execute(s).Count
  End Wit

In this sort of task, punctuation can often be a problem. For example, the first code above would count zero for east if the text was "He travelled east."
 
Upvote 0
Hi Peter thx for quick response - what seems simple has been a labor of love - may I make a comment - how else would an Excel or any other
app whether in C# or anything else accept a user input value and processed as a variable in any other way than the traditional "enter any value in the
textbox and press enter or Find value"? Maybe I'm missing something. Anyway, this code line came from another experienced Excel member and there seems to be no issues with case sensitivity.
Thx very much for taking time to help with this. :)
cr
 
Upvote 0
how else would an Excel or any other
app whether in C# or anything else accept a user input value and processed as a variable in any other way than the traditional "enter any value in the
textbox and press enter or Find value"?
I have no idea what you are getting at.

Thx very much for taking time to help with this. :)
You are welcome.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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