Confusion Around Application.DecimalSeparator

MartinS

Active Member
Joined
Jun 17, 2003
Messages
489
Office Version
  1. 365
Platform
  1. Windows
OK, so I have a userform with a fair number of input controls (TextBoxes). To attempt to reduce the chance of users inputting invalid data, I have applied an KeyPress class event to validate the key entered, and where a decimal is allowed, append the decimal separator. What i am trying to do is deal with users who have different number settings for a decimal separator, i.e. in the Netherlands, where this application will be used, some have their Formats set to Dutch (Netherlands), which uses a comma for a decimal separator and a period for a thousands separator, but also some will use English (United States) which uses a period for the decimal separator and a comma for the thousands separator.
My plan was to determine if the user had the UseSystemSeparators option checked - if not, find out the symbols from a formatted number, and if they did, then take the Application.DecimalSeparator.
Whichever character is used is then appended to the 'allowed list' of characters. But what I am seeing is, irrespective of which country for Format is set, it sees the Application.DecimalSeparator as a comma - even if the workbook displays a period as the decimal separator. All I wanted it to do was reflect what Excel used, so am not sure where I went wrong!
My KeyPress event is as follows:
Code:
Private Sub TextBoxEvents_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim CtrlDataVal As ControlValidation
Dim strValidChars As String
Dim sDecimalSeparator As String
CtrlDataVal = GetControlData(TextBoxEvents.Name)
strValidChars = CtrlDataVal.ValidChars
'\* if the control has decimal places...
If CtrlDataVal.HasDecimals Then
    If Not Application.UseSystemSeparators Then
        sDecimalSeparator = Mid(Format(1000, "#,##0.00"), 6, 1)
        strValidChars = strValidChars & sDecimalSeparator
    Else
        strValidChars = strValidChars & Application.DecimalSeparator
    End If
End If
If Not strValidChars = "" Then KeyAscii = ValidateText(KeyAscii, strValidChars, False)
End Sub
For Reference, ControlValidation is a Type which contains properties for each control, such as minimum and maximum allowed values, what the allowed characters are (for numbers only) and whether the input allows decimal places.
I know by changing the code within the If statement to the code below, that should resolve the issue, but I'm at a loss as to why Excel and the userform display the same decimal separator, but do not allow input of the same character!
Code:
If CtrlDataVal.HasDecimals Then
   sDecimalSeparator = Mid(Format(1000, "#,##0.00"), 6, 1)
   strValidChars = strValidChars & sDecimalSeparator
End If
So at the moment (with the English (US) Format set), my numbers in Excel show as 1,000.00, and the values in my userform display in the same format, but when I attempt to input, say, 2.5 into a textbox, I can only enter 2,5 which is accepted and the form inputs can be saved back to the workbook without issue.
Can anyone she light on why the original code doesn't use the correct decimal separator when Excel is set up to mirror the system?
Many thanks
Martin
 
Last edited:
Thanks, though isn't Format a quicker way to achieve the same?
Code:
sDecimalSeparator = Mid(Format(1000, "#,##0.00"), 6, 1)
That would tell you the decimal separator the computer is using which is not necessarily the same as what Excel using... what I proposed should tell you what Excel itself is using.
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Just validate on tab change and change the backcolour of the textboxes to make them easy to spot.

You could also take a leaf out of the web design book and put the validation message next to the offending control

What I'm doing already is marking inputs that have changed since they were loaded, so it may not take much work to validate them at the same time...
Not enough room for messages, it's a bit of a squeeze!

Thanks again
 
Upvote 0

Forum statistics

Threads
1,217,414
Messages
6,136,494
Members
450,016
Latest member
murarj

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