Need help with formatting numbers in cells that have text using VBA

seattletimebandit

Board Regular
Joined
Apr 11, 2013
Messages
69
I have a spreadsheet table that has alphanumeric text values. Some of the numbers need to be formatted to be consistent with data in other cells (example of correct format: "0.500" and "1.10", incorrect format: "0.5" and "1.1", need to add trailing zeros).

I have a macro that can check formatting of actual numbers based on value of adjacent cell, but I'm having trouble with text values and modifying the number format.

The code from another table format (note that numbers with trailing zeros after decimal point differ based on the value of the number. The table I pasted below shows the inconsistencies of Columns 3, 5, 7, and 9).

As I stated above, note that the alpha characters in my new table are in the same cell contrary to my code below that was looking for letter values in adjacent cells.

Code:
Sub FormatDataChecking()
    
    For rw = 1 To 2000
        
'This will format the columns used for checking data against the hardcopy data
'Format and add a "<" sign to any numbers in Range D that has a "U" flag in Range E
'NOTE: DO NOt RUN THIS CODE UNTIL AFTER YOU HAVE TRANSPOSED DATA INTO FINAL TABLE COLUMNS
'IF THE DATA IS FORMATTED IN THE DATA CHECKING TABLE, IT WILL PREVENT THE Sub 'FormatTableColumns CODE TO NOT WORK PROPERLY
        
        If Range("D" & rw) < 1 And Range("E" & rw) = "U" Then _
            Range("D" & rw).NumberFormat = """< ""#,##0.00 "
        
        If Range("D" & rw) >= 10 And Range("E" & rw) = "U" Then _
            Range("D" & rw).NumberFormat = """< ""#,### "
        
        If Range("D" & rw) >= 1 And Range("D" & rw) <= 9.9 And _
                Range("E" & rw) = "U" Then _
                Range("D" & rw).NumberFormat = """< ""#,#0.0 "
          
'Format numbers in Range D that has an empty cell "" or a "D" data qualifier in Range E
                
        If Range("D" & rw) < 1 And Range("E" & rw) = "" Then _
            Range("D" & rw).NumberFormat = "#,##0.00 "
        
        If Range("D" & rw) >= 1 And Range("D" & rw) <= 9.9 And _
            Range("I" & rw) = "" Then Range("D" & rw).NumberFormat = "#,#0.0 "
        
        If Range("D" & rw) >= 1 And Range("D" & rw) <= 9.9 And Range("E" & rw) = "D" Then _
            Range("D" & rw).NumberFormat = "#,###0.0 "
        
        If Range("D" & rw) >= 10 And Range("E" & rw) = "H" Then _
            Range("D" & rw).NumberFormat = "#,### "
            
        If Range("D" & rw) >= 10 And Range("E" & rw) = "" Then _
            Range("D" & rw).NumberFormat = "#,### "
    
    Next
  
End Sub








MW-01DMW-01DMW-02DMW-02DMW-03DMW-03DMW-04DMW-04D
CONSTITUENTCleanupLevel4/11/201110/18/20114/11/201110/19/20114/13/201110/17/20114/14/201110/17/2011
Dichlorodifluoromethane1001 U1.00 U1 U1.00 U1 U1.00 U1 U1.00 U
Chloromethane0.5 U0.500 U0.5 U0.500 U0.5 U0.500 U0.5 U0.500 U
Vinyl chloride1000.2 U0.2 U3.231.124.623.590.671.1
Bromomethane0.5 U0.5 U0.500 U0.500 U0.500 U0.500 U
Trichlorofluoromethane0.5 U0.5 U0.500 U0.500 U0.500 U0.500 U
Chloroethane0.910.5 U0.5 U0.500 U0.520.500 U0.500 U0.500 U
1,1-Dichloroethene100017.20.9812.34.2416.812.30.863.46
Acetone2 U2 U2 U2.00 U2.00 U2.00 U2.00 U2.00 U
Methylene chloride253.680.862.840.642.491.840.500 U0.6
trans-1,2-Dichloroethene18464.912461.312511161.760.2
1,1-Dichloroethane2757.0816455.32631745.8750.8
2,2-Dichloropropane1 U1 U1 U1.00 U1.00 U1.00 U1.00 U1.00 U
cis-1,2-Dichloroethene1100270010922008622480186090.8703
Chloroform1 U1 U1 U1.00 U1.00 U1.00 U1.00 U1.00 U

<tbody>
</tbody>
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
JoeMo,

I thought the first post didn't go through! my browser was hanging for quite awhile, so I closed it. I couldn't find it posted, so I did it again. Weird, but thanks for letting me know.

stb
 
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,258
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top