Results 1 to 4 of 4

Thread: How to get Float type from general type in VBA
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Sep 2019
    Posts
    6
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default How to get Float type from general type in VBA

    Hi,

    I want to extract amount value in my worksheet. So every number after $ is to be shown in output and their is an edge case whenever the number is less then 1 it should be shown in output . I have created a dollarperc function in VBA to get the output. It's working whenever i encounter a $ value but it shows 0 for decimal entries. PFA my macro enabled workbook. Please help, i'm badly stuck here.

    Worksheet--------------------------

    Entries Output
    $0 0
    $10-$15 10
    10% , $5 5
    0.5 0
    ------------------------------------
    Code
    Code:
    Function dollarperc(ByVal str As String) As Double
    Dim Matches As Object
    Dim Amount  As Double
    
    
    With CreateObject("VBScript.RegExp") '$number-number
        .Global = False
        .Pattern = "^\$(\d+)-"
        
        If .test(str) Then
            Set Matches = .Execute(str)
            Amount = Matches(0).submatches(0)
            dollarperc = Amount
        End If
    End With
    
    
    With CreateObject("VBScript.RegExp") '$number
        .Global = False
        .Pattern = "^\$(\d+)"
        
        If .test(str) Then
            Set Matches = .Execute(str)
            Amount = Matches(0).submatches(0)
            dollarperc = Amount
        End If
    End With
    
    
    With CreateObject("VBScript.RegExp") '% or $number
        .Global = False
        .Pattern = ", \$(\d+)"
        
        If .test(str) Then
            Set Matches = .Execute(str)
            Amount = Matches(0).submatches(0)
            dollarperc = Amount
        End If
    End With
    End Function
    Last edited by Peter_SSs; Oct 10th, 2019 at 04:26 AM. Reason: Added Code tags

  2. #2
    MrExcel MVP
    Moderator
    Peter_SSs's Avatar
    Join Date
    May 2005
    Location
    Macksville, Australia
    Posts
    41,361
    Post Thanks / Like
    Mentioned
    100 Post(s)
    Tagged
    21 Thread(s)

    Default Re: How to get Float type from general type in VBA

    It seems that you have shown the output from your UDF & said that it is not doing exactly what you want. What would be helpful is to know exactly what output you do want, and what the layout of that output should be. So, can you post the sample data again and show the full desired output?


    BTW, when posting code, please use code tags to preserve the code indentation. I have done that for you in your post and my signature block below tells you how.
    Last edited by Peter_SSs; Oct 10th, 2019 at 04:33 AM.
    Hope this helps, good luck.
    Peter
    Excel 365 - Windows 10
    - Want to help your helpers by posting a small, copyable, screen shot directly in your post? Look here
    - If posting vba code, please use Code Tags - like this [code]Paste code here[/code] - or use the # key in the Reply window
    - Read: Forum Rules, Forum Use Guidelines, & FAQ

  3. #3
    Board Regular Kyle123's Avatar
    Join Date
    Jan 2012
    Location
    Leeds, UK
    Posts
    2,646
    Post Thanks / Like
    Mentioned
    10 Post(s)
    Tagged
    2 Thread(s)

    Default Re: How to get Float type from general type in VBA

    Please post what you want the output to look like for each of the examples above, rather than what your code actually outputs

  4. #4
    Board Regular
    Join Date
    Mar 2015
    Location
    Syria
    Posts
    324
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)

    Default Re: How to get Float type from general type in VBA

    Hi
    Just guess
    Code:
    Function dollarperc(ByVal str As String) As Double
    Dim Matches As Object
    Dim Amount  As Double
    With CreateObject("VBScript.RegExp") '$number
        .Global = True
        .Pattern = "((\$)(\d+))|(\d+\..)"
        
        If .test(str) Then
            Set Matches = .Execute(str)
            Amount = Matches(0).submatches(2)
         If Matches(0).submatches(2) = "" Then Amount = Matches(0).submatches(3)
        End If
    End With
    dollarperc = Amount
    End Function

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •