1

I'm having trouble finding a way to remove floating integers from a cell without removing numbers attached to the end of my string. Could I get some help as to how to approach this issue?

For example, in the image attached, instead of:
john123 456 hamilton, I want:
john123 hamilton

image

1 Answer 1

1

This can be done using regular expressions. You will match on the data you want to remove, then replace this data with an empty string.

Since you didn't provide any code, all I can do you for is provide you with a function that you can implement into your own project. This function can be used in VBA or as a worksheet function, such as =ReplaceFloatingIntegers(A1).

You will need to add a reference to Microsoft VBScript Regular Expressions 5.5 by going to Tools, References in the VBE menu.

Function ReplaceFloatingIntegers(Byval inputString As String) As String

    With New RegExp
        .Global = True
        .MultiLine = True
        .Pattern = "(\b\d+\b\s?)"
        If .Test(inputString) Then
            ReplaceFloatingIntegers = .Replace(inputString, "")
        Else
            ReplaceFloatingIntegers = inputString
        End If
    End With

End Function

Breaking down the pattern

  • ( ... ) This is a capturing group. Anything captured in this group will be able to be replaced with the .Replace() function.
  • \b This is a word boundary. We use this because we want to test from the edge to edge of any 'words' (which includes words that contain only digits in our case).
  • \d+\b This will match any digit (\d), one to unlimited + times, to the next word boundary\b
  • \s? will match a single whitespace character, but it's optional ? if this character exists

You can look at this personalized Regex101 page to see how this matches your data. Anything matched here is replaced with an empty string.

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

Comments

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.