View Single Post
 
Old 08-28-2011, 05:28 PM
judicial85 judicial85 is offline Windows 7 Office 2010 (Version 14.0)
Advanced Beginner
 
Join Date: Nov 2010
Posts: 64
judicial85 is on a distinguished road
Default

Not perfect/efficient, but seems to do the trick:

Code:
 Public Function RoundBond(inbond As String)
    
    Dim front As String, back As String
    Dim brackLen As Integer, NumDec As Integer
    
    'Take only the bond length inside the brackets
    back = "." & Mid(Left(inbond, Len(inbond) - 1), InStr(1, inbond, "(") + 1, InStr(1, inbond, ")")) 'Add fake decimal for rounding purposes
    brackLen = Len(back) - 1
    
    'Function's ability is based on the length inside the bracket:
    If brackLen = 1 Then
        RoundBond = inbond
        Exit Function
    Else
        'Round standard deviation to 1 decimal
        back = Round(back, 1)
        back = Mid(back, 3)  'Get rid of fake decimal (VBA not rounding integers)
        
        'Take only the bond length before the brackets
        front = Mid(inbond, 1, InStr(1, inbond, "(") - 1)
        
        'Find the post-decimal numbers & how long they are
    '    Debug.Print Mid(front, InStr(1, front, ".") + 1) & " = decimals before bracket"
     '   Debug.Print Len(Trim(Str(Mid(front, InStr(1, front, ".") + 1)))) & " = # decimals"
        NumDec = Len(Trim(Str(Mid(front, InStr(1, front, ".") + 1))))
    
        'Round front accordingly to only 1 standard deviation (Plus the issue of maintaining 0s)
        If Left(Right(front, 2), 1) = "0" And Right(front, 1) < 6 Then
            front = Round(front, NumDec - brackLen + 1) & "0"
        ElseIf Left(Right(front, 2), 1) = "9" And Right(front, 1) > 4 Then
            front = Round(front, NumDec - brackLen + 1) & "0"
        Else
            front = Round(front, NumDec - brackLen + 1)
        End If
        RoundBond = front & "(" & back & ")"
    End If
    End Function
Reply With Quote