seattletimebandit
Board Regular
- Joined
- Apr 11, 2013
- Messages
- 69
Hello all, first time here.
I have a table of data in which a lot of data appears as "<0.500 U".
I have to strip out the "<" sign, but keep the "U". I have a macro that does this (see below), but the format of the number changes to "0.50 U". I need to be able to retain the trailing zero as "0.500 U". Plus the sig figs can be different in other cells, but I need to keep the formatting for them as well:
Example of formats I need to keep:
0.500 U (always has a "U")
0.50 U (always has a "U")
0.2 U (always has a "U")
2.13 (usually has no "U", but could from time to time)
10.1 (usually has no "U", but could from time to time)
1,125 (never has a "U")
The following macro is used to find the leading "<" signs and insert a trailing "U" if not in the cell, then strip out all the "<'s" from the entire table. Numbers that need a thousands separator do not have decimals (and also don't have a "<" or "U") and some have decimals of one, two, or three significant digits. But when I run the macro, the trailing zero disappears from the "0.500 U" to "0.50 U" after running the macro below.
So I need to be able to ignore the text ("U") and format the numbers based on my criteria above. I did get the thousands separator bit to work
but it formats all numbers to not have decimals, 'cause that's what I told it to do.
I just can't figure out how to format a cell with text.
Thanks for the help!
I have a table of data in which a lot of data appears as "<0.500 U".
I have to strip out the "<" sign, but keep the "U". I have a macro that does this (see below), but the format of the number changes to "0.50 U". I need to be able to retain the trailing zero as "0.500 U". Plus the sig figs can be different in other cells, but I need to keep the formatting for them as well:
Example of formats I need to keep:
0.500 U (always has a "U")
0.50 U (always has a "U")
0.2 U (always has a "U")
2.13 (usually has no "U", but could from time to time)
10.1 (usually has no "U", but could from time to time)
1,125 (never has a "U")
The following macro is used to find the leading "<" signs and insert a trailing "U" if not in the cell, then strip out all the "<'s" from the entire table. Numbers that need a thousands separator do not have decimals (and also don't have a "<" or "U") and some have decimals of one, two, or three significant digits. But when I run the macro, the trailing zero disappears from the "0.500 U" to "0.50 U" after running the macro below.
So I need to be able to ignore the text ("U") and format the numbers based on my criteria above. I did get the thousands separator bit to work
but it formats all numbers to not have decimals, 'cause that's what I told it to do.
Thanks for the help!
Code:
Sub ChangeQualifiers()
' created 04/10/13
Dim c As Range
Application.ScreenUpdating = False
For Each c In Selection
If Left(c.Value, 1) = "U" Then ' find only existing "U" qualifiers
c.Value = Right(c.Value, Len(c.Value) - 1) ' if "U's" are found then delete all "<"
c.Value = Left(c.Value, Len(c.Value) - 1) & " " & (c.Value) ' then add a space between the number and the "U"
ElseIf Left(c.Value, 1) = "<" Then ' now look for remaining "<'s", this is in case the data has a "<" but no "U"
c.Value = Left(c.Value, Len(c.Value) - 1) & " U" ' if found then add a "U" with a leading space
c.Value = Right(c.Value, Len(c.Value) - 1) ' then delete all remaining "<'s"
ElseIf Left(c.Value, 1) <> "U" And Left(c.Value, 4) > "999" Then 'fix this, not working, formats numbers with decimals as whole numbers
c.NumberFormat = "#,##0 "
End If
' now format to line up right justified
With c
.HorizontalAlignment = xlRight
.VerticalAlignment = xlCenter
End With
' indent
c.InsertIndent 1
' finally, bold all data that does not have a qualifer
If Right(c.Value, 1) = "U" Then
c.Font.Bold = False
Else: c.Font.Bold = True
End If
Next c
End Sub