views:

47

answers:

2

Hi !

I'm currently writing a code in VBA to retrieve prices for some financial models. The problem I have is in the excel spreadsheet where I have special Ascii characters like ⅞ ¼ etc... I would need using VBA to transform this in 7/8 1/4 etc...

How could I do that ?

Thanks for your help

A: 

Create a lookup table. When it finds one of these special characters set the value appropriately (example "1/2" = 0.5) and if not use the Val() function to get the value.

jalexiou
+1  A: 

If you want literal string replacement, use the Replace function thus:

Sub changeit()
    Dim w As Worksheet
    Dim r As Range
    For Each r In Application.Selection
        r.Value = Replace(r.Value, Chr$(188), "1/4")
        r.Value = Replace(r.Value, Chr$(189), "1/2")
        r.Value = Replace(r.Value, Chr$(190), "3/4")
    Next
End Sub

et cetera. (I have done each one separately to make it easier to read.) Alternatively, you could replace the literal strings "1/4" as @jalexiou suggests with 0.25 etc., ideally using a lookup table. Not sure what the Chr$ code is for the seven-eighths though.

sasfrog
This solution is working well ! Many thanks ! Where could I find the code for 1/8 3/8 5/8 and 7/8 ?
BlackLabrador
Ok, I have the Unicode HEX Values 1/3 = 2153, 2/3 = 2154, 1/8 = 215B, 3/8 = 215C, 5/8 215D, 7/8 = 215E. But I can't use Chr$() for these...
BlackLabrador
BlackLabrador
Nice work. Make sure you provide upvotes/accept answers once you're happy! :)
sasfrog