Formatting default value of input box

marrick13

New Member
Joined
Oct 7, 2005
Messages
14
I have a userform that reads data from several worksheets (in the same workbook as the userform). Those values represent dollar amounts and are formatted in the sheet with two decimals. I want to display them as the default entry in an input box so one can see what's already stored as the cost for that item

I've tried several variations, as follows ('"EntryItemAmt" is both the worksheet range name and the VBA variable that's read as the input box default):

EntryItemAmt = Worksheets("Formulas").Range(Format("EntryItemAmt", "0.00"))
EntryItemAmt = Worksheets("Formulas").Format(Range("EntryItemAmt", "0.00"))
EntryItemAmt = Worksheets("Formulas").Range("EntryItemAmt", "0.00").NumberFormat = "0.00"

and none of them work. If the value in the worksheet is, say, 4.16, it displays in the inputbox as "4", and if it's 4.73 in the worksheet, it displays as "5".

Is there a way to have the values show as the input box default as 4.16 and 4.73 instead of 4 or 5?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi marrick13,

If you're referring to an input box on the UserForm, then you probably have a TextBox control.

Below are 3 ways to populate a TextBox control with a default formatted to two decimal places.

If your worksheet cell is already formatted to two decimals; you can read it directly into the TextBox:
Code:
Private Sub UserForm_Initialize()
    Me.TextBox1 = Worksheets("Formula").Range("EntryItemAmt").Text
End Sub

If your Variable is a String; format prior to assigning the value to the String
Code:
Private Sub UserForm_Initialize()
    Dim sEntryItemAmt As String
    
    sEntryItemAmt = Format(Worksheets("Formula").Range("EntryItemAmt"), "0.00")
    Me.TextBox1 = sEntryItemAmt
    
End Sub


If your Variable is a Double; format when assigning the Variable's value to the TextBox
Code:
Private Sub UserForm_Initialize()
    Dim dEntryItemAmt As Double
    
    dEntryItemAmt = Worksheets("Formula").Range("EntryItemAmt")
    Me.TextBox1 = Format(dEntryItemAmt, "0.00")
    
End Sub


If you're actually using the InputBox function, then the syntax would be similar.
Code:
Sub MyMacro()
    Dim dEntryItemAmt As Double
    Dim vResult As Variant
    
    dEntryItemAmt = Worksheets("Formula").Range("EntryItemAmt")
    vResult = InputBox("Enter amount", "MyTitle", Format(dEntryItemAmt, "0.00"))
    
End Sub
 
Upvote 0
Jerry,

Thanks so much - I am using an input box with the userform, not a textbox, and was able to format the default for 2 decimals-but it wasn't reading the data from the worksheet that way. Your suggestion to declare the dEntryItemAmt As Double is what did the trick (the vResult can be a string rather than a variant).

Powerful though Excel is, I didn't have to go through this with Lotus 123. This project is converting a simple automated database application from Lotus (with macros) to Excel, and each time I read a cell in Lotus into a variable, it displays exactly as written. Of course, one doesn't declare variables in Lotus, either...

Thanks again for your help!
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

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