#1
|
|||
|
|||
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. |
#2
|
|||
|
|||
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 |
Tags |
macro, rounding, statistics |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 |