Dir scheint nicht bewusst zu sein, dass diese API Funktion ausschließlich mit Unicode funktioniert - VBA kann zwar mit Unicode umgehen, redet aber per Default in ASCII, d.h. man muss hier StrConv() nutzen.
Option Explicit
Private Declare PtrSafe Function GetNumberFormatEx Lib "kernel32" ( _
ByVal lpLocaleName As String, _
ByVal dwFlags As Long, _
ByVal lpValue As String, _
ByRef lpFormat As Any, _
ByVal lpNumberStr As String, _
ByVal cchNumber As Long _
) As Long
Private Const LOCALE_NOUSEROVERRIDE As Long = &H80000000
Sub Example()
Dim strLocal As String
Dim strValue As String
strLocal = "de-DE"
strValue = "-12345.67"
'liefert: "-12.345,67"
'---------------------
strLocal = StrConv(strLocal, vbUnicode)
strValue = StrConv(strValue, vbUnicode)
Dim strBuffer As String
Dim lngResult As Long
lngResult = GetNumberFormatEx(strLocal, LOCALE_NOUSEROVERRIDE, strValue, ByVal 0, vbNullString, 0)
If lngResult <= 0 Then
Call MsgBox("Unerwarteter Fehler aufgetreten.", vbCritical, "Fehler: " & Err.LastDllError)
Exit Sub
End If
lngResult = 2 * lngResult
strBuffer = String$(lngResult, vbNullChar)
lngResult = GetNumberFormatEx(strLocal, LOCALE_NOUSEROVERRIDE, strValue, ByVal 0, strBuffer, lngResult)
If lngResult <= 0 Then
Call MsgBox("Unerwarteter Fehler aufgetreten.", vbCritical, "Fehler: " & Err.LastDllError)
Exit Sub
End If
strBuffer = StrConv(strBuffer, vbFromUnicode)
strValue = Left$(strBuffer, lngResult - 1)
Call MsgBox( _
"Formatierter Wert:" & vbNewLine & _
"[" & strValue & "]", _
vbInformation, _
"Ergebnis")
End Sub
Grüße
|