Extracting numbers from text strings

There is often a need to extract numbers from the start or end of text strings.  It’s not too hard to do with on-sheet formulas, but after having done it a few thousand times I decided it would be worth spending 5 minutes writing two VBA User Defined Functions (UDFs) to do the job.  Here they are:

NumRight() will extract a number from the right hand end of a text string, with the number delimited by a space by default, or optionally any other character:

Function NumRight(NumStrings As Variant, Optional Delim As String = " ") As Variant
Dim i As Long, numrows As Long, OutA() As Variant, j As Long, NumDig As Long, Numstring As String, StringLen As Long

    If TypeName(NumStrings) = "Range" Then NumStrings = NumStrings.Value2
    If TypeName(NumStrings) = "String" Then
        numrows = 1
    Else
        numrows = UBound(NumStrings)
    End If

    ReDim OutA(1 To numrows, 1 To 1)
    On Error Resume Next
    For i = 1 To numrows
        If IsArray(NumStrings) = True Then
            Numstring = NumStrings(i, 1)
        Else
            Numstring = NumStrings
        End If
        StringLen = Len(Numstring)
        j = 1
        Do While Left(Right(Numstring, j), 1) <> Delim
            If j >= StringLen Then Exit Do
            j = j + 1
        Loop
        If j >= StringLen Then
            OutA(i, 1) = ""
        Else
            OutA(i, 1) = CDbl(Right(Numstring, j - 1))
        End If
    Next i
    NumRight = OutA
End Function

Numleft() is similar, but extracts a number from the left hand end.

Function NumLeft(NumStrings As Variant, Optional Delim As String = " ") As Variant
Dim i As Long, numrows As Long, OutA() As Variant, j As Long, NumDig As Long, Numstring As String, StringLen As Long

    If TypeName(NumStrings) = "Range" Then NumStrings = NumStrings.Value2
    If TypeName(NumStrings) = "String" Then
        numrows = 1
    Else

        numrows = UBound(NumStrings)
    End If

    ReDim OutA(1 To numrows, 1 To 1)
    On Error Resume Next
    For i = 1 To numrows
        If IsArray(NumStrings) = True Then
            Numstring = NumStrings(i, 1)
        Else
            Numstring = NumStrings
        End If

        StringLen = Len(Numstring)
        j = 1
        Do While Right(Left(Numstring, j), 1) <> Delim
            If j >= StringLen Then Exit Do
            j = j + 1
        Loop
        If j >= StringLen Then
            OutA(i, 1) = ""
        Else
            OutA(i, 1) = CDbl(Left(Numstring, j - 1))
        End If

    Next i
    NumLeft = OutA
End Function

For both functions the input “NumStrings” may be either a single cell or a single column range. In the latter case the function must be entered as an array function to return all the results:

  • Enter the function, with the input range being a single column range
  • Select the range of output rows, with the function in the top row
  • Press F2 (Edit)
  • Press Ctrl-Shift-Enter

The functions may be found in GetNum.xlsb, and have also been added to Text-in2.xlsb.

Here’s what they look like in use:

NumLeft and NumRight functions

NumLeft and NumRight functions

Posted in Excel, UDFs, VBA | Tagged , , , , , , | 13 Comments

1 Million Page Views

At some time in the next couple of hours or so the number of page views here will pass the 1 million mark, so congratulations in advance one millionth page viewer.

Stats by country4

I thought it would be interesting to see where all these viewers of 1 million pages come from, so here are the figures (since Feb 2012, when WordPress started publishing the daily count by country):

By Total Page Views

By Total Page Views

Unsurprisingly, the list sorted by total page views is headed by the English speaking countries, with the USA well ahead and Australia in third place, but more surprisingly:

By Page views / Million Population

By Page views / Million Population

, when sorted by page views per million population the USA falls to 21st place, Australia remains 3rd, headed by Latvia well ahead in first place, followed by Iceland.  Singapore is the first Asian country, ahead of the UK and Canada.  Hong Kong takes the second Asian spot in 13th place, but down at the bottom of the list:

By Page views / Million Population

By Page views / Million Population

mainland China comes in at 190th out of 192, with only 80 views from close to 20% of the World’s population.

So greetings to my fan or fans in Latvia and Iceland, I hope you find the blog continues to be worth reading!

Posted in Computing - general | Tagged , | 3 Comments

Moving Load Spreadsheet – update and how to use

As noted in the comments on the previous post on this topic, the moving load function (and also the associated continuous beam functions) have been modified to make data entry more flexible; the output results are unchanged.  The modified spreadsheet may be downloaded from ConBeamU Download, including full open-source code.

Further details of the use of the MovLoadU function are given below, with the example of a typical 3 span bridge consisting of precast concrete girders with in-situ concrete link slabs at the internal piers, and using the AS 5100 M1600 truck loading.

The definition of the 3 spans and the truck load is shown in the screen-shot below:

Definition of 3 span beam with link slabs and M1600 Vehicle

Definition of 3 span beam with link slabs and M1600 Vehicle

To carry out the moving load analysis:

  • Define a number of vehicle positions, with the X value of axle 1 (cells H24 down in the screen shot below – click the image for a larger size view)
  • For each point where the effect of the moving load is to be analysed, enter in a 2 row range the output required (one of “moment”, “shear”, “slope” or “deflection”), and the position of the point, as shown in cells I21:Q22 in the screen shot below.
  • The MovLoadU function will return the selected output actions for each vehicle position listed.  The function can either be used with the ranges selected in the example, or re-entered (as an array function) anywhere in the spreadsheet.
MovLoadU Output

MovLoadU Output

In the screen shot above the action type (“moment”) has been entered above every output column. This may be replaced with a single entry in the left-most column, with the columns to the right of the same type being left blank:

Output action type entered in column I only

Output action type entered in column I only

The “moment” text may also be aligned centrally over the columns to which it applies, using the “Center Across Selection” alignement:

MovLoad2-3

To find the maximum bending moment in span 1 the location of the output actions, and the vehicle positions may be specified at closer centres:

Location of maximum moment

Location of maximum moment

Alternatively the Excel Solver may be used to adjust the output location to maximise or minimise the value of the selected action. In the screenshot below the Solver is set up to maximise the bending moment in the central span (cell K18) by adjusting the output location in cell K22:

Solver set-up to find maximum moment position in central span

Solver set-up to find maximum moment position in central span

After using the Excel Solver to locate the position of maximum moments, shears and deflections at the 9 locations shown above, the resulting output of actions and deflections can be plotted against vehicle position, as shown below:

Maximum Bending Moments

Maximum Bending Moments

Maximum Shear

Maximum Shear

Maximum Deflection

Maximum Deflection

Posted in Beam Bending, Excel, Frame Analysis, Newton, UDFs, VBA | Tagged , , , , , , , , | 7 Comments

Martin Sharp, Disraeli Gears, and Tales of Brave Ulysses

When I heard on the radio today of the death of Australian artist Martin Sharp I thought I wasn’t familiar with his work.  I was wrong; he was one of the founders of Oz magazine, which all the cool guys read when I was at school, and through a chance meeting with Eric Clapton he provided the artwork for Cream’s 1967 album Disraeli Gears:

Disraeli Gears

Disraeli Gears

He also wrote the words for Tales of Brave Ulysses (on the back of a napkin). Hear the story here:

And listen to the full song un-interrupted here:

The image used in this video is Ulysses and the Sirens by Herbert James Draper

You thought the leaden winter would bring you down forever,
But you rode upon a steamer to the violence of the sun.
And the colours of the sea bind your eyes with trembling mermaids,
And you touch the distant beaches with tales of brave Ulysses,
How his naked ears were tortured by the sirens sweetly singing,
For the sparkling waves are calling you to kiss their white laced lips.
And you see a girl’s brown body dancing through the turquoise,
And her footprints make you follow where the sky loves the sea.
And when your fingers find her, she drowns you in her body,
Carving deep blue ripples in the tissues of your mind.
The tiny purple fishes run laughing through your fingers,
And you want to take her with you to the hard land of the winter.
Her name is Aphrodite and she rides a crimson shell,
And you know you cannot leave her for you touched the distant sands
With tales of brave Ulysses, how his naked ears were tortured
By the sirens sweetly singing.
The tiny purple fishes run lauging through your fingers,
And you want to take her with you to the hard land of the winter

Farewell Martin Sharp, it seems I knew you better than I realised.

 

Posted in Bach | Tagged , , , , , | Leave a comment

Aspects of Love from Buffy Sainte-Marie

Buffy Sainte-Marie was (and continues to be) one of the great North American singer-song writers of the post-war era, but she never achieved the recognition accorded to others of her era such as Joan Baez and Joni Mitchell.  According to her Wikipedia article she was subject to active and personal censorship during the 70’s by Lyndon B. Johnson and his government.

Here are three very different samples of her work, taken from her 1965 album “Many a Mile”, which was I think the third LP I ever bought.  The first tells of the innocence of young love, the second the exact opposite, and the third something somewhere in between.

Posted in Bach | Tagged , , , , | Leave a comment