Inputbox: collecting and using value to 3 decimal places

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
336
Office Version
  1. 2019
I have code that gathers data for new customers. The customer's default price is collected with
VBA Code:
DefaultPrice = InputBox("Enter DEFAULT PRICE", "New Customer")
This is a price accurate to 3 decimal places. Most commonly, the value is 19.091
Hovering over DefaultPrice in the VBA editor shows the code has indeed collected 19.091 as the value
The value is entered into the customer table with the code
VBA Code:
Sheets("Customers").Cells(LastCustomerRow + 1, 9) = DefaultPrice
The column being pasted into is formatted to Currency with 3 decimal places, however this cell displays $19.090 after pasting and the formula bar simply 19.09 (2 decimal places)

How can I retain the third decimal place?

Something like
VBA Code:
Sheets("Customers").Cells(LastCustomerRow + 1, 9) = DefaultPrice.NumberFormat = "$#,##0.000"
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi,
Need to coerce the output from InputBox which is Text to Double

VBA Code:
Sub Yameez()
    Dim DefaultPrice As Variant
    
    Do
        DefaultPrice = InputBox("Enter DEFAULT PRICE", "New Customer")
        'cancel pressed
        If StrPtr(DefaultPrice) = 0 Then Exit Sub
    Loop Until IsNumeric(DefaultPrice)
    
    DefaultPrice = CDbl(DefaultPrice)
    
    With Sheets("Customers").Cells(LastCustomerRow + 1, 9)
        .Value = DefaultPrice
        .NumberFormat = "$#,##0.000"
    End With
End Sub

Dave
 
Upvote 0
Alternatively, you could consider using the Application.InputBox Method where you can specify the data type

VBA Code:
Sub Yameez()
    DefaultPrice = Application.InputBox("Enter DEFAULT PRICE", "New Customer", , , , , , 1)
    'cancel pressed
    If DefaultPrice = False Then Exit Sub
 
   With Sheets("Customers").Cells(LastCustomerRow + 1, 9)
        .Value = DefaultPrice
        .NumberFormat = "$#,##0.000"
    End With

End Sub

Dave
 
Upvote 0
in your regional setting, is a comma or a point your decimal separator ? (mine is ,)
Try this one with your 19.019 in a version with the comma and the point in your normal keyboard and numeric keyboard.
This works correct with the "," in the normal keyboard and "," in the numeric keyboard
VBA Code:
Sub Jamezz()
     DefaultPrice = Application.InputBox("Enter DEFAULT PRICE", "New Customer", , , , , , 1)
     Range("A1").Value = DefaultPrice
End Sub
 
Upvote 0
Thanks for the replies. My decimal place is a point (.) I am trying to enter (and retain) 19 dollars and 1.9 cents
I have strange results: I made a Test sheet and used these two codes:
VBA Code:
DefaultPrice = Application.InputBox("Enter DEFAULT PRICE", "New Customer", , , , , , 1)
With Sheets("Test").Cells(LastCustomerRow + 1, 9)
        .Value = DefaultPrice
        .NumberFormat = "$#,##0.000"
    End With
VBA Code:
DefaultPrice = InputBox("Enter DEFAULT PRICE (LUC or Retail)", "New Customer")
With Sheets("Test").Cells(LastCustomerRow + 1, 9)
        .Value = DefaultPrice
        .NumberFormat = "$#,##0.000"
    End With
Both placed $19.091 in the cell, but when I tried pasting the code into the rest of my new customer macro, I got $19.090 (and the formula bar just shows 19.09)
I removed the default cell formatting (currency with 3 decimal places), but that did not change the behaviour.

The full code of the macro is:
VBA Code:
Sub NewCustomer()

ScreenUpdating = False

If IsEmpty(Range("SaleCustomer")) Then
MsgBox ("Please enter Customer name")
Exit Sub
End If

Dim NewCustomer As String, Address As String, LastCustomerRow As Long, PaymentMethod As String
Dim Suburb As String, State As String, Postcode As String, ABN As String, Email As String
Dim RowE As Long, DefaultPrice As Currency, InvoiceType As String

NewCustomer = Sheets("Sale").Range("SaleCustomer").Value

Address = InputBox("Enter ADDRESS (no Suburb, State, Postcode)", "New Customer")
Suburb = InputBox("Enter SUBURB", "New Customer")
State = InputBox("Enter STATE ABBREVIATION", "New Customer")
Postcode = InputBox("Enter POSTCODE", "New Customer")
ABN = InputBox("Enter ABN (with spaces)", "New Customer")
Email = InputBox("Enter EMAIL", "New Customer")
PaymentMethod = InputBox("Enter DEFAULT PAYMENT METHOD" & vbNewLine & "For credit card, enter Shopify", "New Customer", Default:="Direct Debit")
'DefaultPrice = InputBox("Enter DEFAULT PRICE (LUC or Retail)", "New Customer")
DefaultPrice = Application.InputBox("Enter DEFAULT PRICE", "New Customer", Type:=1)
InvoiceType = InputBox("Enter INVOICE TYPE (Email or None)", "New Customer")

LastCustomerRow = Sheets("Customers").Range("A" & Rows.Count).End(xlUp).Row

Sheets("Customers").Unprotect
Sheets("Customers").Activate
Sheets("Customers").Cells(LastCustomerRow + 1, 1) = NewCustomer
Sheets("Customers").Cells(LastCustomerRow + 1, 2) = Address
Sheets("Customers").Cells(LastCustomerRow + 1, 3) = Suburb
Sheets("Customers").Cells(LastCustomerRow + 1, 4) = State
Sheets("Customers").Cells(LastCustomerRow + 1, 5) = Postcode
Sheets("Customers").Cells(LastCustomerRow + 1, 6) = ABN
Sheets("Customers").Cells(LastCustomerRow + 1, 7) = PaymentMethod
Sheets("Customers").Cells(LastCustomerRow + 1, 8) = Email

   With Sheets("Customers").Cells(LastCustomerRow + 1, 9)
        .Value = DefaultPrice
        .NumberFormat = "$#,##0.000"
    End With

Sheets("Customers").Cells(LastCustomerRow + 1, 10) = InvoiceType

Sheets("Customers").Range("A2:J" & LastCustomerRow + 1).Select
    ActiveWorkbook.Worksheets("Customers").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Customers").Sort.SortFields.Add2 Key:=Range( _
        "A2:A6"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Customers").Sort
        .SetRange Range("A2", "J" & LastCustomerRow + 1)
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

Sheets("Customers").Protect

Sheets("Data").Unprotect
RowE = Sheets("Data").Range("E" & Rows.Count).End(xlUp).Row
Sheets("Data").Range("E2", "E" & RowE + 1).FillDown

Sheets("Sale").Activate

ScreenUpdating = True

End Sub
 
Upvote 0
stupid idea, suppose your cell start with format "text" before you give it the value.

set the numberformat first, before assigning the value (swap both).
 
Upvote 0
Try changing your variable data type which will also allow the managing of Cancel button being pressed

VBA Code:
 Dim DefaultPrice As Variant
    DefaultPrice = Application.InputBox("Enter DEFAULT PRICE", "New Customer", , , , , , 1)
    'cancel pressed
    If DefaultPrice = False Then Exit Sub

Dave
 
Upvote 0
Solution
Dave, it seems the variable type was the key. When I did my test code above I forgot to declare the DefaultPrice variable type. I presume not doing so automatically meant it became a variant, and thus the code worked as a test, but not when I specifically declared it as currency.

Thanks for your help guys.
 
Upvote 0
Correct, undeclared variables are of Variant data type by default.

Glad we were able to help & resolve your issue but would suggest that you look at managing the Cancel Button in your code

Dave
 
Upvote 0

Forum statistics

Threads
1,215,216
Messages
6,123,669
Members
449,114
Latest member
aides

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