Magic_Doctor
XLDnaute Barbatruc
Bonsoir,
J'essaye d'automatiser des mises en forme du style "NumberFormat".
J'ai tenté de la manière suivante :
Merci d'avance pour tout conseil.
J'essaye d'automatiser des mises en forme du style "NumberFormat".
J'ai tenté de la manière suivante :
VB:
Function HowLong(dNum As Double) As Byte
'Renvoie le nombre de chiffres après la virgule
Dim SepDec$, tmp, posDec
SepDec = Application.International(xlDecimalSeparator)
tmp = CStr(dNum)
posDec = InStr(tmp, SepDec)
If posDec = 0 Then
HowLong = 0
Else
HowLong = Len(tmp) - Len(Right(tmp, posDec))
End If
End Function
VB:
Function MFDecApVirg(num As Double, max As Byte, Optional suf As String) As String
'Retourne la syntaxe de MF
'- num : le chiffre à traiter
'- max : le nombre maximal de décimales après la virgule
'- suf : un éventuel suffixe (%, mL...)
Application.Volatile
Dim x As Byte
x = HowLong(num)
If x > max Then x = max
If HowLong(num) = 0 Then
MFDecApVirg = Chr(34) & 0 & Chr(34) & Chr(34)
Else
MFDecApVirg = Chr(34) & "0" & Chr(46) & Application.WorksheetFunction.Rept(0, x) & Chr(34) & Chr(34)
End If
If IsMissing(suf) Then
MFDecApVirg = MFDecApVirg
Else
MFDecApVirg = MFDecApVirg & suf & Chr(34) & Chr(34) & Chr(34)
End If
End Function
VB:
Sub geronimo()
'[I6] = 35,6879
'si j'écris :
[I6].NumberFormat = "0.00"" mL""" 'ça marche, me renvoie 35,69 mL
'[I6].NumberFormat = MFDecApVirg([I6].Value, 2, " mL") 'me renvoie : 0.00 mL
'pourtant MFDecApVirg([I6].Value, 2, " mL") = "0.00"" mL"""
End Sub
Merci d'avance pour tout conseil.
Dernière édition: