View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Michiel via OfficeKB.com Michiel via OfficeKB.com is offline
external usenet poster
 
Posts: 48
Default Rounding in VBA - Any ideas?

Thanks again all for your help.

I used the approach with the string and created this function.
I think it works perfactly. IF someone thinks it does not then please tell me!


Function MathRound(dblInput As Double, Optional intDigits As Integer = 0) As
Double
'This function performs a mathematical rounding. Of the dblInput value.
'It will not round like the round function of VB.
'IF specified it will round towards the intDigits digits.
'EXample: MathRound(1.5) - 2 And MathRound(2.5) - 3
Dim strTemp As String
Dim dblTemp As Double
Dim lngExpon As Long
Dim lngPos As Long

Dim n As Long

lngExpon = 10 ^ intDigits 'The factor to multiply with in case more than 0
digits
dblTemp = (dblInput * lngExpon) + 0.5 'Correction factor to truncate the
number correctly
strTemp = CStr(dblTemp) 'Hold the value in string to truncate it

lngPos = InStr(1, strTemp, ".") 'find digit in string
If lngPos 0 Then strTemp = Left(strTemp, lngPos - 1) 'Truncate
dblTemp = CDbl(strTemp) 'Return number back to double

MathRound = dblTemp / lngExpon 'Calculate back to required number of digits.

End Function

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...tions/200809/1