Vba: display decimals with point and not coma

喜欢而已 提交于 2020-03-03 03:00:50

问题


I would like to have all numbers in a macro displayed with point and not coma

For Instance This display "0,03" but I would like "0.03":

Dim MyNumber As Single
MyNumber = 0.03
MsgBox (MyNumber)

I have tried a set of codes that does not work:

  • this still displays the Coma:

    Application.DecimalSeparator = "."
    
  • this still displays the Coma and does not apply to the whole macro

    MyNumber = Format(MyNumber, "##0.00")
    
  • this displays dot instead of coma but does not apply to the whole macro

    MsgBox (Replace(MyNumber, ",", "."))
    

Thank you!


回答1:


This is easier said than done. In the VBA editor, the decimal separator is the dot. However, the MsgBox function (and the Format function) will use the windows regional settings, and not the Excel settings, to format its results.

In order to have the MsgBox display a number using format settings of your choice, you need to create a string that has the value formatted as you want.

Here is one way of doing that:


Option Explicit
Sub dural()
 Dim S As String
 Dim D As Double
 Const myDecSep As String = "."

D = 1234.56

S = Format(D, "0.00") 'will format using the system separator

S = Replace(S, Application.DecimalSeparator, myDecSep)

MsgBox S

End Sub

Note that if you want to use both decimal and thousands separators, and if you are interchanging, for example, the comma and the dot, you need to do this twice so as not to replace all your commas with dots, or vice-versa


Option Explicit
Sub dural()
 Dim S As String
 Dim D As Double
 Const myDecSep As String = "."
 Const myThousSep As String = ","

D = 1234.56

S = Format(D, "#,##0.00")

S = Replace(S, Application.DecimalSeparator, Chr(1))
S = Replace(S, Application.ThousandsSeparator, Chr(2))
S = Replace(S, Chr(1), myDecSep)
S = Replace(S, Chr(2), myThousSep)

MsgBox S

End Sub




回答2:


As a first step, the three functions bellows from Kernel32 Dynamic-link library have to be declared:

Private Declare Function GetUserDefaultLCID% Lib "kernel32" ()
Private Declare Function GetLocaleInfoA Lib "kernel32" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
Private Declare Function SetLocaleInfoA Lib "kernel32" ( ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String) As Boolean
  • The first will get the user default id
  • The second will help to get the value of the locals (and therefore restore)
  • The third will help to set the value of the locals

The local type value for the decimals is 14 (some likes to write &HE - hexadecimal E -)

Then the program has to look like this:

' record the settings in the variable LocalSettingsDecimal
Dim LocalSettingsDecimal As String
Dim Buffer As String
Buffer = String(256, 0)
Dim le As Integer
le = GetLocaleInfoA(GetUserDefaultLCID(), 14, Buffer, Len(Buffer))
LocalSettingsDecimal = Left(Buffer, le - 1)

' force decimal settings to '.'
Call SetLocaleInfoA(GetUserDefaultLCID(), 14, ".")

' body
Dim MyNumber As Single
MyNumber = 0.03
MsgBox (MyNumber)

' set back the decimal settings
Call SetLocaleInfoA(GetUserDefaultLCID(), 14, LocalSettingsDecimal)

Let's note that, unfortunately, In case the program body fail, the settings are not restored... but this still answer the issue




回答3:


Most likely your Excel settings are causing a conflict.

1.    On the File tab, click the Options button
2.    Click the Advanced tab
3.    Clear/Uncheck the "Use system separators" option.

Then try your code.



来源:https://stackoverflow.com/questions/42532857/vba-display-decimals-with-point-and-not-coma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!