Creating a VBA macro that changes specific string to a number

zeekiehafa

New Member
Joined
May 15, 2012
Messages
9
Hello all,

I am having some difficulty with a pdf that I converted to an excel document because I wanted to use the data from the pdf tables in a different program I am currently working on. However, the data is in the improper format. For example, in the table it reads 2-1/8 as string and I want it to be the number value 2.125 . Likewise if the value in the table reads 5-1/4 I want it to automatically convert it to something that will be read as the number 5.25

I've been scratching my head over this for quite some time and I would appreciate it if anyone has some helpful comments.

P.S. If you figure out a code for this I would appreciate a careful description of how it works. Thank you. :)
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
How about ...

Select the column and replace the hyphen with a space.

Then do Data > Text to columns, Finish.
 
Upvote 0
Actually I was hoping for a solution a little bit more automatic than that. I know how to write a macro to go through tables and search for the values that need changing, but I want the macro to search and change as it goes.
 
Upvote 0
Well then, maybe

Code:
Sub x()
    Dim cell As Range
    Dim v As Variant
    
    For Each cell In Intersect(Selection.Cells, ActiveSheet.UsedRange)
        If VarType(cell.Value) = vbString Then
            v = Evaluate(Replace(cell.Value, "-", " "))
            If VarType(v) = vbDouble Then cell.Value = v
        End If
    Next cell
End Sub
 
Upvote 0
Thanks, that appears to work. Would you care to explain what the code is doing? Thanks again.

-zeek
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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