How to get Float type from general type in VBA

Vicasso

New Member
Joined
Sep 9, 2019
Messages
6
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--------------------------

EntriesOutput
$00
$10-$1510
10% , $55
0.50

<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>
</tbody>
------------------------------------
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 a moderator:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
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:
Upvote 0
Please post what you want the output to look like for each of the examples above, rather than what your code actually outputs
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,479
Messages
6,113,897
Members
448,530
Latest member
yatong2008

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top