Vba:显示带点而不是昏迷的小数

时间:2017-03-01 13:18:11

标签: excel vba excel-vba

我希望宏中的所有数字都显示为点而非昏迷

For Instance此显示" 0,03"但我想" 0.03":

Dim MyNumber As Single
MyNumber = 0.03
MsgBox (MyNumber)

我尝试了一组 工作的代码:

  • 这仍显示昏迷:

    Application.DecimalSeparator = "."
    
  • 这仍然显示昏迷,并不适用于整个宏

    MyNumber = Format(MyNumber, "##0.00")
    
  • 这会显示点而不是昏迷,但不适用于整个宏

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

谢谢!

4 个答案:

答案 0 :(得分:3)

这说起来容易做起来难。在VBA编辑器中,小数点分隔符是点。但是,MsgBox函数(和Format函数)将使用Windows区域设置而不是Excel设置来格式化其结果。

为了让MsgBox使用您选择的格式设置显示数字,您需要创建一个具有所需格式值的字符串。

这是一种方法:

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

请注意,如果要同时使用小数和千位分隔符,并且如果要进行交换,例如逗号和点,则需要执行此操作两次,以免用点替换所有逗号,或者副-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

答案 1 :(得分:1)

作为第一步,必须声明来自Kernel32动态链接库的三个函数:

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
  • 第一个将获得用户默认ID
  • 第二个将有助于获取当地人的价值(并因此恢复)
  • 第三个将有助于设置当地人的价值

小数的局部类型值是14(有些人喜欢写& HE - 十六进制E - )

然后程序必须如下所示:

' 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)

让我们注意到,遗憾的是,如果程序体失败,设置不会恢复......但这仍然可以解决问题

答案 2 :(得分:0)

很可能您的Excel设置导致了冲突。

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

然后尝试你的代码。

答案 3 :(得分:0)

我今天也注意到了。
具体来说,使用 FormatNumber 函数。 因为我不想采用@Jonathan 的(非常有趣的)方法并更改我的 [lipp]ing 系统设置只是为了控制数字的显示方式(说真的,Office,你怎么了??),我写道在检测到是否有必要后,使 FormatNumber 使用应用设置而不是系统设置的包装函数:

Public Function FormatNumberFixed(Expression As Variant, Optional NumDigitsAfterDecimal As Integer = -1, _
Optional IncludeLeadingDigit As Integer = -2, Optional UseParensForNegativeNumbers As Integer = -2, _
Optional GroupDigits As Integer = -2)
    FormatNumberFixed = FormatNumber(Expression, NumDigitsAfterDecimal, IncludeLeadingDigit, UseParensForNegativeNumbers, GroupDigits)
    Dim s As String
    s = FormatNumber(0.5, 1)
    If Mid(s, 2, 1) = Application.ThousandsSeparator Then ' WTF, thousands separator used as decimal separator
        FormatNumberFixed = Replace(FormatNumberFixed, Application.ThousandsSeparator, "%TMP%")
        FormatNumberFixed = Replace(FormatNumberFixed, Application.DecimalSeparator, Application.ThousandsSeparator)
        FormatNumberFixed = Replace(FormatNumberFixed, "%TMP%", Application.DecimalSeparator)
    End If
End Function

这可用于根据 Excel 中的设置“按预期”格式化数字。