1

I have a table in Excel and one of its columns contains string of the type: 125j, 0j, 12j, etc.

I want the value of the cells (in this column) to be replace by the numbers (i.e. I want to drop the "j") using the VBA. Here is what I have:

Dim reg As Object
Set reg = CreateObject("VBScript.RegExp")
reg.IgnoreCase = True
reg.Pattern = "^[0-9]+"

If reg.test(Cells(i, 4).Value) Then
    Cells(i, 4).Value = reg.Execute(Cells(i, 4).Value)
End If

But it doesn't work, do anyone knows either how to crrect it or to obtain the same result from another command?

Thank you

1 Answer 1

1

The result of the .Execute method is a MatchCollection object. You can use that explicitly:

Dim reg As Object
Dim allMatches As Object

Set reg = CreateObject("VBScript.RegExp")
reg.IgnoreCase = True
reg.Pattern = "^[0-9]+"

If reg.test(cells(i, 4).Value) Then
    Set allMatches = reg.Execute(cells(i, 4).Value)
    cells(i, 4).Value = allMatches(0)
End If

Or implicitly:

    cells(i, 4).Value = reg.Execute(cells(i, 4).Value)(0)
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.