Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9207675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:26:23+00:00 2026-06-18T00:26:23+00:00

Q: Which Excel VBA RegEx expression should I use to count decimal places? Desired

  • 0

Q: Which Excel VBA RegEx expression should I use to count decimal places?

Desired output
This is a table with some example input strings and the desired output.

INPUT       DESIRED OUTPUT
string      max value   unit    decimal places
--------------------------------------------------
200A        200         A       0
110kV       110         kV      0
38,1MW      38,1        MW      1
38,1Mvar    38,1        Mvar    1
0-130°C     130         C       0
20-130°C    130         C       0
2000A       2000        A       0
10 kV       10          V       0
34,6MW      34,6        MW      1
34,6Mvar    34,6        Mvar    1
600A        600         A       0
114,3 MW    114,3       MW      1
114,3 Mvar  114,3       Mvar    1
2500A       2500        A       0
300A        300         A       0
500 A       500         A       0
100A        100         A       0

What I have done so far
Here you see a standalone Excel VBA code. The first sub is only for testing.
The function RegEx is my issue.

Sub TestRegEx()
    strArray = Array("200 A", "200 A", "110 kV", "38,1MW", "38,1Mvar", _
            "0-130°C", "2000 A", "10 kV", "34,6MW", "34,6Mvar", "600 A", _
            "114,3 MW", "114,3 Mvar", "2500 A", "300 A", "500 A", "100 A")

    For i = 0 To UBound(strArray)
       Call RegEx(strArray(i))
    Next
End Sub

Function RegEx(ByVal strInput As String)

    Dim objRegEx As Object
    Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.IgnoreCase = True
    objRegEx.Global = True

    'Show me the value
    objRegEx.Pattern = "[^0-9_,]"
    Debug.Print objRegEx.Replace(strInput, "")

    'Show me the unit
    objRegEx.Pattern = "[^A-Z_°]"
    Debug.Print objRegEx.Replace(strInput, "")

    'Show me the decimal places
    objRegEx.Pattern = "?????"
    Debug.Print objRegEx.Replace(strInput, "")

End Function

Hints

  • I want to avoid normal VBA methods like InStr or looping through every character.
  • Maybe the VBA Len method can be used to count the length of a string. But it will fail if there are zero decimal places, or?
  • For me, regular expressions are hard to read. All my knowledge is based on this MSDN article.

I am thankful for any help to my initial question on how to count decimal places.


PS: If you have suggestions for my other two RegEx strings for unit and value, please let me know. For example my value regex expression fails on “20-130°C”. I don’t know how to remove everything left from “-” to extract the value “130” only.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:26:25+00:00Added an answer on June 18, 2026 at 12:26 am

    Try this:

    Function RegExTest(ByVal strInput As String)
        Dim objRegEx As Object, ret$
        Set objRegEx = CreateObject("VBScript.RegExp")
        With objRegEx
            .IgnoreCase = True
            .Global = True
            'printing value
            .Pattern = "[^0-9_,]"
            Debug.Print .Replace(strInput, "")
            ret = .Replace(strInput, "") & vbTab
    
            'printing unit
            .Pattern = "[^A-Z_°]"
            Debug.Print .Replace(strInput, "")
            ret = ret & .Replace(strInput, "") & vbTab
    
            'printing decimal places
            .Pattern = ","
            Debug.Print .Execute(strInput).Count
            ret = ret & CStr(.Execute(strInput).Count)
        End With
        RegExTest = ret
    End Function
    

    UPDATE

    To remove parts before -, use the below function:

    Function RegExTest(ByVal strInput As String)
        Dim objRegEx As Object, ret$
        Set objRegEx = CreateObject("VBScript.RegExp")
        With objRegEx
            .IgnoreCase = True
            .Global = True
    
            'printing value
            .Pattern = "[^0-9_,-]"'///<--do not delete hyphen now.
            Dim temp$
            temp = .Replace(strInput, "")
            .Pattern = "^.*?-"'///<-remove hyphen according to your need
            temp = .Replace(temp, "")
            Debug.Print temp
            ret = temp & vbTab
    
    
            'printing unit
            .Pattern = "[^A-Z_°]"
            Debug.Print .Replace(strInput, "")
            ret = ret & .Replace(strInput, "") & vbTab
    
    
            'printing decimal places
            .Pattern = ","
            Debug.Print .Execute(strInput).Count
            ret = ret & CStr(.Execute(strInput).Count)
        End With
        RegExTest = ret
    End Function
    

    UPDATE 2

    The function to count decimal places:

    Function GetDecPlaces(strInput$, Optional decmark$ = ",") As Integer
        'add a reference to
        'Microsoft VBScript Regular Expressions 5.5
        Dim re As New VBScript_RegExp_55.RegExp, mt As Match
        With re
            .Global = True
            .Pattern = decmark & "\d+"
            For Each mt In .Execute(strInput)
                .Pattern = "\d"
                GetDecPlaces = .Execute(mt.Value).Count
            Next mt
        End With
    End Function
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write some vba code in Excel to query a table
I have an EXCEL VBA function which should return the address of the first
Possible Duplicate: In need of JavaScript Solution for Exporting table to Excel which works
I have this excel file which I have been able to write the data
I have an Excel Workbook which has some VSTO-based c# code. Given a known
I have some URLs in an Excel file which I have saved as a
My requirement is I have a Excel which contains some data. I would like
I am porting a Excel-VBA based app which uses Userforms over to a C#
Excel VBA: I am trying to get to some activex option buttons through the
I have the following INSTR condition in Excel VBA which doesn't work (all the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.