Formatting Cells in Visual Basic

mtp9302

New Member
Joined
Aug 1, 2006
Messages
22
Hi, I have an application where I am using a UserForm to have the spreadsheet user type in several pieces of data. I have successfully used this and the data has been translated onto the spreadsheet as desired, except for the formatting. The data that is being translated consists of two strings, a date, a number, and a currency value. The strings and date come out fine. The number and currency value come up with those little green earmark errors, indicating that the cell contain numbers stored as text. I put the following lines of code in the UserForm code to fix this, but they didn't do anything (the mileage in E5 is the number and the cost in F5 is the currency):

Range("E5").Value = MileageData
Selection.NumberFormat = "0"
Range("F5").Value = CostData
Selection.NumberFormat = "$#,##0.00"

I think I need to convert the text to a number, like the green earmark error suggests, but I don't know how to code this. I tried to record a macro to do it, but it just gave me the commands I have listed above. If anyone has any ideas on how to accomplish this, I'd be most appreciative for any information. Thanks.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try this.
Code:
With Range("E5")
      .Value = Val(MileageData)
      .NumberFormat = "0"
End With

With Range("F5")
       .Value = Val(CostData)
       .NumberFormat = "$#,##0.00"
End With
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,194
Members
449,072
Latest member
DW Draft

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