VBA code for Conditional numeric formats


Posted by Daniel on August 23, 2000 12:37 AM

I need help with a macro that sets conditional numeric formats for a predefined area. I.e. if value is > 10 then use 0.00 format. If the value is < 10 use 0.0 format and some other value-based conditions.
Any help is appreciated.

Posted by David on August 23, 0100 2:27 AM

try this
Sub Numberformat()

numrows = 10
numCols = 10
For rownum = 1 To numrows
For colnum = 1 To numCols
If Cells(rownum, colnum) < 10 Then
Cells(rownum, colnum).Numberformat = "0.00"
Else
Cells(rownum, colnum).Numberformat = "0.000"
End If
Next colnum
Next rownum
End Sub



Posted by Daniel on August 23, 0100 7:16 AM

I solved it myself but thanks anyway David (Apparantly i presenterd my problem inaccurate)!Heres my solution if it is helpful for anyone...

Range("j5:o60").Select
Dim Cell As Range
For Each Cell In Selection
Select Case Cell
Case "" 'värdet är precis 10
Cell.NumberFormat = "##0"
Case Is > 10 'värdet är större än 10
Cell.NumberFormat = "##0"
Case Is < 10
Cell.NumberFormat = "##0.0"
Case Else
Cell.NumberFormat = "##0.0"
End Select
Next
End Sub