String Manipulation

omnisautnihil

New Member
Joined
May 13, 2011
Messages
19
I am trying to write a very robust program for a template file I can upload for my company. I want the admin to be able to set a form for the entry values as such:

ANYTHING####

This program is to evaluate and fix the entries given by users who fail to follow the form. For example if the form says:

IVHMS34_SRS####

and the user enters: SRS140 or 720 or IVHMSSRS360

I want to fix them to say the corresponding correct form.

The problem is there are no rules. It can be any number of characters before the numbers and the numbers can be any length after the other characters. There is no deliniator that is set in stone. That form could say IVHMS_###.

I feel like the easiest way is to ask the admin for the string portion.

So my question is this: (Assuming counting from the right is the best approach) How would I find the end of the number portion to add the string portion. I know how to & things together but its finding the end of the number portion that is confusing me. (I really dont want to do a two level loop looking at each character to see if its the first non-number.)
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
So, if I understand you correctly, it sounds like you could have any amount of letters followed by any amount of numbers, and you just want to extract the numeric portion.

If that is correct, a UDF such as this should work:
Code:
Function GetNumber(myEntry As String) As String
 
    Dim myLen As Long
    Dim i As Long
    Dim myStart As Long
    
    myLen = Len(myEntry)
    If myLen > 1 Then
        For i = 1 To myLen
            If Not (IsNumeric(Right(myEntry, i))) Then
                myStart = i - 1
                Exit For
            End If
            If i = myLen Then myStart = myLen
        Next i
        GetNumber = Right(myEntry, i - 1)
    End If
    
End Function
 
Upvote 0
Code:
Sub RobustProgram()

    ' Tools -> Reference -> Microsoft VBScript Regular Expressions 5.5

    Dim sPatternPart As String, sDigits As String
    Dim re As New RegExp
    Dim userInput As Variant
    
    sPatternPart = "IVHMS34_SRS" 'Control text part
    sDigits = "\d{4}" 'Control amount of digits to appear. In this case - four digits.
    re.Pattern = sPatternPart & sDigits
    
    userInput = Application.InputBox("Enter string", "String", Type:=2)
    If userInput = False Then Exit Sub
    
    If Not re.Test(userInput) Then
        MsgBox "Incorrect input", vbCritical, "Error"
        Exit Sub
    End If
    
    ' Further processing.

End Sub
 
Upvote 0
Code:
Function GetNumber() As String

    ' Tools -> Reference -> Microsoft VBScript Regular Expressions 5.5
    
    Dim sPatternPart As String, sDigits As String
    Dim re As New RegExp, mc As MatchCollection
    Dim userInput As Variant

    sPatternPart = "IVHMS34_SRS"
    sDigits = "(\d{4})" 'Catch number
    re.Pattern = sPatternPart & sDigits
    
    userInput = Application.InputBox("Enter string", "String", Type:=2)
    If userInput = False Then Exit Function
    
    If Not re.Test(userInput) Then
        MsgBox "Incorrect input", vbCritical, "Error"
        Exit Function
    End If
    
    ' Get number.
    Set mc = re.Execute(userInput)(0).SubMatches(0)
    GetNumber = mc(0).SubMatches(0)

End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,716
Members
452,939
Latest member
WCrawford

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