Slang's Anti-Singularity
  • Slanghome
  • Slangblog
  • Slangpuzzles
    • CurrentPuzzles
    • ScholarPuzzle
    • PastPuzzles

Counting Occurrences of One String in Another Using VBA

5/14/2013

0 Comments

 
I needed to determine the number of times one particular string occurred as part of a larger string. Specifically, I needed to determine how many tabs were at the start of a line of text in order to convert to a properly nested indent (i.e., for each tab, I needed to indent .5").

Poking around, I knew I could parse out the string and iterate through a counter for each time I found the string. Cumbersome, but doable. I'm lazy, and didn't want to type that many lines of code, so I thought about it a little more and thought the built in Split() function could be leveraged to do what I need.

Indeed, that was the case and the resulting code is shown below:

Public Function CountCharacter(ByVal theString As String, ByVal strSearchChar As String) As Integer
Dim iCount As Integer
Dim myarray() As String
     myarray = Split(theString, strSearchChar)
     iCount = UBound(myarray)
     CountCharacter = iCount
End Function

The code does exactly what I hoped, is case-sensitive, and can be used to count the number of occurrences of any text element in any other text element. In my specific case, to search for the number of tabs at the start of a line, I used a range object to identify the text and passed the vbTab constant as the search character:

     MsgBox "The number of tabs in the line is " & CountCharacters(myRange.Text, vbTab)

Of course, a selection object's text attribute can be passed as well, or any string variables if you've already done some processing on a piece of text from a document.

The end result is a fast, clean and small amount of code. That, I liked. If you are interested, the code that converted the tabs to indents was equally small (altRange is the string from the start of the line to the first non-tab character, and wholeRange is the paragraph where altRange is found):

i = CountCharacter(altRange.Text, vbTab)
altRange.Delete
wholeRange.ParagraphFormat.LeftIndent = InchesToPoints(0.5 * i)

0 Comments

Take this job and...

5/7/2013

1 Comment

 
I understand that to show one's age is often likened to showing a hint of one's tightie whities, but I actually recall listening to the original Johnny Paycheck version of this song... on vinyl when it was initially released. Not that I am a fan of country music any more (it's morphed into something different these days), but the intent and oomph of the song came to the fore when my investment paid off. In spades!

It's only a matter of a few hours now before I march into my boss's office and announce my retirement. Why? Well, I came into a spot of cash over the weekend. Not just a few dollars. Not just a few thousand dollars.

No.

I am now a member of the ultra rich. I can burn hundred dollar bills to heat my house. Hell, I can burn down houses to keep warm and not worry about cost.
Picture
Picture
Yep, you see rightly, There's an awful lot of zeroes there. I am now a hundred trillionaire! In DOLLARS! My only concern is finding a place that has enough cash to cover it. You know, in case I want a coffee or something.

See ya, suckers!
1 Comment

"Busy weekend? Nah, just sat around. You?"

5/6/2013

1 Comment

 
Picture
I lied. It was a busy weekend.

Saturday kicked off with a 16-mile trail run split into a 2-mile out and back to see Cascade Falls (there at left with Cascade Lake in the distance), then a 14 mile out and back from Eagle Pt. Campground to D.L. Bliss State Park and back. Then it was off to a fundraiser for the kids school at probably the largest single-family dwelling I have ever been in. 

Sunday, I worked as a volunteer for a local charity race in the morning, and still had to take care of the normal weekend activities (marshaling homework, laundry, shopping, cooking). For the race, I had agreed to do the timing, and course marking for a 5K, so there wasn't a lot of rest involved from the activities of the day before, and it was an early morning (on site at 0600) after a late night of festivities. But first, Saturday!

I had never visited Cascade Falls, and the runoff was near peak this weekend, so I thought it would be a good time to look. As it turns out, the trail is extremely rocky, and a lot of it wasn't runnable. But the parts that were runnable, were fun:

Picture
After that quick out and back we popped down the road a few miles and followed the entire Bay to Bliss race route. Once again, the scenery was spectabulous, though I didn't see a single Sasquatch. I was nearly as disappointed in that as I was with my legs. 
Picture
Picture
Picture
Picture
Legs weren't feeling it this weekend, and I was happy to simply finish vertically, with only a one trip and near shin-bash on a rock.I was significantly behind the other runners. I guess DFL is better than DFD*. As the saying goes, "good days and bad days..." 

The gala event in the evening was a fund raiser for my kids' school. The home was absolutely beautiful as were the grounds. Unfortunately, the weather didn't hold up well, and we had to do the dinner inside rather than outside. At my house, that would be a significant issue, as I am not sure my entire house could hold 150 people standing up, let alone sitting down to a dinner. 

However, this home accommodated all of us at tables in the great space area. Solely. I figured when the space wasn't filled with people and tables, they host roller hockey games. It was that big. There was an auction to raise money and I participated up to the $100 level. Unfortunately for me, I was unsuccessful at the winning bid on several items; they went for $10,000 or more. 

I was successful in bidding on one item, though. I'll post about it and my grand plan for it another time.

The race was a nice 5K on a flat course by the Carson River. Although marking it was going to be painful enough with my feet and legs as unhappy as they were from the previous day's running, added to it was the alcohol- fueled late night with little sleep and even less restful sleep. 

I woke at 0430 to the sound of pounding rain. Really? Really?! It's the stinking high desert! We are supposed to only get snow in winter! 

I dragged my ass to the venue, and - with the rain falling - marked the course. By the time the race itself started, the sun had finally broken through, so the competitors had really good conditions, as the rain packed the often-sandy route. I heard many comment as they finished that they had a beautiful, and peaceful run. 

I was happy to have been a part of their positive experience. :-) Hearing that made the rain and cold of marking the course inconsequential.

*Dead Fucking Dead
1 Comment

    RSS Feed

    Author

    Just a guy out exploring the world. Former world-class never-was endurance runner.

    ​Hit me up, and we'll catch a beer or coffee in your town.


    Follow @slang4201

    Archives

    December 2022
    November 2022
    October 2022
    September 2022
    August 2022
    July 2022
    June 2022
    May 2022
    April 2022
    March 2022
    February 2022
    January 2022
    December 2021
    November 2021
    October 2021
    September 2021
    August 2021
    May 2021
    October 2020
    September 2020
    December 2019
    November 2019
    October 2019
    July 2019
    October 2018
    September 2018
    May 2018
    April 2018
    November 2017
    October 2017
    July 2017
    June 2017
    June 2015
    August 2014
    January 2014
    October 2013
    September 2013
    June 2013
    May 2013
    April 2013
    March 2013
    February 2013
    January 2013
    December 2012
    November 2012
    October 2012
    September 2012
    August 2012
    July 2012
    June 2012
    May 2012
    April 2012
    March 2012
    February 2012
    January 2012
    December 2011
    November 2011
    October 2011
    August 2011
    March 2011
    October 2010
    July 2010
    January 2009
    December 2008
    October 2008

    Categories

    All
    2011
    Alfama
    Alternate Energy
    Android
    Angela Sullivan
    Angkor Wat
    Animals
    Antiques
    Apple
    Ash Canyon
    Astronomy
    AT&T
    Australia 2022
    Bailout
    Battery
    Bicycling
    Biometrics
    Books
    Brisbane
    Broome
    Cairns
    Cambodia
    Canary Islands
    Carrier Iq
    Carson City
    Cascais
    Centennial
    C Hill
    C-Hill
    Christmas
    Climate
    Clothing
    Coding
    Colorado
    Columbus
    Cramps
    Curiosity
    Dad
    Dardanelles Lake
    Darwin
    Dell
    Dick's Lake
    Dilbert
    Diving
    Eagle Lake
    Earworms
    Eating Problems
    Eclipse
    Economy
    Ecuador
    Education
    Eldorado Canyon
    El Valle
    Energy
    Errors
    Espionage
    Europe 2019
    Evi
    Fallon
    Family
    Fontanillis Lake
    Food
    Gamboa
    Garmin
    Geocaching
    Goals
    Google
    Google Earth
    Grouse Lake
    Hiking
    Ho Chi Minh City
    Hoi An
    Inov8
    Investing
    Ipad
    Iphone
    Iron Mountain
    Katherine
    Kuala Lumpur
    Lanzarote
    Legislatures
    Lisbon
    Mac
    Malaysia 2022
    Market
    Market Drop
    Mars
    Mctarnahan
    Medicine
    Microsoft Word
    Motivation
    Mountain Biking
    Moving Minutes
    Music
    Nevada Day
    Nfc
    Ohio
    Olympics
    Openoffice
    Opportunity
    Panama 2018
    Panama 2022
    Paper Airplane
    People
    Perth
    Playa Coronado
    Politics
    Portugal
    Prison Hill
    Privacy
    Puts
    Puzzles
    Quito
    Race
    Rant
    Reno
    Retrospective
    Roosevelt
    Running
    Running Dynamics
    Saddest Cities
    Safe & Sober
    Science
    Scuba
    Shoes
    Shopping
    Sicily
    Siem Reap
    Sierra
    Sierra Canyon
    Sintra
    Slangsploration
    Snl
    Soccer
    Software
    Spasms
    Spirit
    Sullivan Canyon
    Svn
    Tahoe
    Tahoe Rim Trail
    Taormina
    Taxes
    Technology
    Transit
    Travel
    Trees
    Vba
    Velma Lakes
    Venus
    Verizon
    Vietnam
    Violin
    Watches
    Weather
    Wolframalpha
    Words
    Wrestling
    Writing
    Xkcd
    Yawbe
    Yoga

This is ALL MINE, I tell you! copyright 2010-2022