Force Text To Value VBA Help

wom2000

New Member
Joined
May 11, 2007
Messages
47
Hi,

I found the following code online that allows a user to force text to values in excel. This code works great except for a couple of issues. When I run the procedure, it coverts all my text to number within the sheet referenced, but it blows away my formulas and screws up my percentages.

Can someone help me modify this code so that it will only apply to what ever area I "highlight" on the sheet with my mouse? Or can you help me to fix the code so it does not blow away my formulas or messes up my percents. Thanks in advance for your brilliance.

Below is the current code that needs modification.

Sub ForceValueCode()

Dim LastPlace, Z As Variant, X As Variant, NewNumb
ActiveSheet.Select
LastPlace = ActiveCell.SpecialCells(xlLastCell).Address
ActiveSheet.Range(Cells(1, 1), LastPlace).Select
Z = Selection.Address
For Each X In ActiveSheet.Range(Z)
If Len(X) > 0 Then
NewNumb = Application.Text(X, "0")
X.FormulaR1C1 = NewNumb
Else
X.FormulaR1C1 = ""
End If
Next
Range(Z).Select
Selection.NumberFormat = "0"
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try Changing This Line

For Each X In ActiveSheet.Range(Z)

With

For Each X In ActiveSheet.Range(Z).SpecialCells(xlCellTypeConstants)

Hope that helps...
 
Upvote 0
Thank you. This fixes the issue with regards to blowing away my formulas, however I still have the issue with regards to percentages. For example: if I have a value of 0.5 in the cell, when I run the code it coverts my 0.5 to 1 and if I have a value of .3 it converts it to 0.0. How can I modify the code to address this as well? Thank you.
 
Upvote 0
This line is setting the number format to 0 decimal places, so the numbers get rounded
Selection.NumberFormat = "0"


try changing it to
Selection.NumberFormat = "0.00"
 
Upvote 0
It worked! Thanks. I had to change the other reference to number format as well, but it worked so thank you very much for your help.
 
Upvote 0

Forum statistics

Threads
1,214,913
Messages
6,122,207
Members
449,074
Latest member
cancansova

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