0

I have a list of items:

  • B100Q91, B75NX2, BR100, XN20ZN..

I want to remove the first set of numbers in every item, so that it looks like this:

  • BQ91, BNX2, BR, XNZN..

My approach looks like this:

Function RemoveFirstNumbers(Txt As String) As String
With CreateObject("VBScript.RegExp")
Dim posn As Integer
posn = GetPositionOfFirstNumericCharacter(Txt)
1
If (IsError(posn)) = True Then Replace(Txt, 1, 1) As String
Dim posn As Integer
Else
End With
End Function
End If
GoTo 1

with 

Public Function GetPositionOfFirstNumericCharacter(ByVal s As String) As Integer
Dim i As Integer
    For i = 1 To Len(s)
        Dim currentCharacter As String
        currentCharacter = Mid(s, i, 1)
        If IsNumeric(currentCharacter) = True Then
            GetPositionOfFirstNumericCharacter = i
            Exit Function
        End If
    Next i
End Function
1
  • Note that vb.net is not the same as VBA and you should tag regexp - I've made those changes for you. Commented Dec 3, 2020 at 21:57

2 Answers 2

2

EDIT to package the below as a function

Function ReplaceFirstDigits(sLookupExpression) as String
    Dim oReg As Object

    Set oReg = CreateObject("VBScript.Regexp")

    With oReg
        .Global = True
        .ignorecase = True
        .MultiLine = False
        .Pattern = "([A-Za-z]+)(\d+)(\w*)"
    End With

    ReplaceFirstDigits = oReg.Replace(sLookupExpression, "$1$3")
End Function

Then you can call the function directly from the spreadsheet with a standard syntax, such as =ReplaceFirstDigits(A1)

If you wanted to use a regular expression, code such as the following might do the trick for you.

Sub Test()
    Dim oReg As Object
    
    Set oReg = CreateObject("VBScript.Regexp")
    
    With oReg
        .Global = True
        .ignorecase = True
        .MultiLine = False
        .Pattern = "([A-Za-z]+)(\d+)(\w*)"
    End With
    
    Debug.Print oReg.Replace(Selection, "$1$3")

End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

You can simplify that with .Global=False and .Pattern="\d+" Multiline is not needed. Your output would come from .Replace(Selection, "") With .Global=False, only the first set of digits will be matched.
Hi, thanks for taking the time. How can I call this macro in excel?
@Quant1337 You can create a function using the interior logic. Replace Sub Test with Function Test (or replace 'Test' with something more descriptive). Then return the value from the function. I'll make an edit to my post showing these changes.
1

EDITED:

If you don't want to use RegExp, just straight VBA:

Option Explicit

Function RemoveFirstNumber(strInput As String) As String
    Dim strRet As String
    
    Dim bFirstNumber As Boolean
    bFirstNumber = False
    
    Dim strChar As String
    Dim nChar As Integer
    
    For nChar = 1 To Len(strInput)
        strChar = Mid(strInput, nChar, 1)
        
        If IsNumeric(strChar) Then
            bFirstNumber = True
        Else
            If bFirstNumber Then 
               strRet = strRet & Mid(strInput, nChar)
               Exit For
            End If
            
            strRet = strRet & strChar
        End If
    Next nChar
    
    RemoveFirstNumber = strRet
End Function

enter image description here

2 Comments

Hi your code works, but only on some examples. Examples like BR100D, BRX100W, where only one letter is on the right side don't work - do you have an idea why?
@Quant1337 Ah yes ... didn’t test that case! I’ve edited the code, so hopefully this works better (not at my PC right now).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.