Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-27-2011, 01:02 PM
judicial85 judicial85 is offline Formatting Standard Deviations (Macro) Windows 7 Formatting Standard Deviations (Macro) Office 2010 (Version 14.0)
Advanced Beginner
Formatting Standard Deviations (Macro)
 
Join Date: Nov 2010
Posts: 64
judicial85 is on a distinguished road
Default Formatting Standard Deviations (Macro)

Hello all,



I was wondering what the best way to round standard deviations in excel would be? I have a series of columns as follows:
"2.4567(1)"
"3.2345(12)"
"3.2435(5)

and I would need the macro to "round" 3.2345(12) into 3.234(1) to fit with the rest. The list always has a random order and quantity of values needing rounding, so I would have to allow it to recognize ...(xy) versus ...(z).

Thanks for your advice! ~Bryan~

PS - Due to the brackets, these are initially considered strings in their columns.
Reply With Quote
  #2  
Old 08-28-2011, 05:28 PM
judicial85 judicial85 is offline Formatting Standard Deviations (Macro) Windows 7 Formatting Standard Deviations (Macro) Office 2010 (Version 14.0)
Advanced Beginner
Formatting Standard Deviations (Macro)
 
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
Reply

Tags
macro, rounding, statistics



Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting Standard Deviations (Macro) Need macro to merge files in Word and preserve formatting Carolin Word VBA 3 12-14-2014 04:01 AM
Formatting macro Ulodesk Word Tables 9 04-10-2012 06:37 PM
Change standard form topcoder234234 Project 0 07-13-2011 09:18 AM
How to set standard format for all TDV Word 0 07-02-2011 09:41 PM
company standard letter nicklarzn Word 0 12-17-2008 02:35 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:01 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft