|
-
September 6th, 2001, 07:40 AM
#2
Re: VBA: Roundfunction in Excel 97
'Maybe you can write it. The following is an example you can improve
private Sub CommandButton1_Click()
MsgBox "15.342 rounded to 2 dec. = " & fnctRound(15.342, 2)
End Sub
public Function fnctRound(byval toRoundNumber as Variant, byval number_num as Integer) as Variant
Dim posPoint as Integer
Dim tmpNumber as string
If IsNumeric(toRoundNumber) then
tmpNumber = CStr(toRoundNumber)
posPoint = InStr(1, tmpNumber, ".", vbBinaryCompare)
If posPoint = 0 then
fnctRound = toRoundNumber
else
Dim decNumber as string, i as Integer
If number_num > 0 then
decNumber = "0."
for i = 1 to number_num
decNumber = decNumber & "0"
next i
decNumber = decNumber & "5"
else
decNumber = 0
End If
toRoundNumber = toRoundNumber + CDbl(decNumber)
tmpNumber = CStr(toRoundNumber)
If posPoint = 1 then
tmpNumber = "0" & tmpNumber
posPoint = 2
End If
'save int part of number
fnctRound = Left(tmpNumber, posPoint - 1)
'choose dec part
If number_num > 0 then
fnctRound = fnctRound & mid$(tmpNumber, posPoint, number_num + 1)
End If
End If
else
MsgBox "Not a number " & toRoundNumber
End If
End Function
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|