Only show decimals as required

andysh

Board Regular
Joined
Nov 8, 2019
Messages
111
Hi,

I'm trying to format cells in a worksheet so that for all columns from col B and row 3 downwards the cells only show decimals as required (max 2 decimal places) e.g.
12
12.1
12.12

Column A and rows 1 and 2 are text and dates so don't need the format.

I've found the below code but it's not doing quite what I want, can anyone suggest any tweaks to make it fit my needs?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 Dim Cell As Range
 For Each Cell In Intersect(Target, Columns("C"))
   If Not Cell.Value Like "*[!0-9]*" Then
     Cell.NumberFormat = "General"
   ElseIf Not Cell.Value Like "*[!0-9.]*" And Not Cell.Value Like "*.*.*" Then
     Cell.NumberFormat = "0.00"
   Else
     Cell.NumberFormat = "General"
   End If
 Next
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Are you trying to do this on existing data that is already populated on a sheet, or for new data being entered into the sheet?
 
Upvote 0
Since the code you quoted was Worksheet_Change code, which is to only be used on new data being entered in, I will assume that is what you are looking for.

Here is some VBA code that will do what you want:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
    
'   Exit if updates not in column C
    Set rng = Intersect(Target, Columns("C:C"))
    If rng Is Nothing Then Exit Sub
    
'   Loop through all cells just updated
    For Each cell In rng
'       Only proceed if update below row 2
        If cell.Row > 2 Then
            If cell.Value = Round(cell.Value, 0) Then
                cell.NumberFormat = "0"
            Else
                If cell.Value = Round(cell.Value, 1) Then
                    cell.NumberFormat = "0.0"
                Else
                    cell.NumberFormat = "0.00"
                End If
            End If
        End If
    Next cell

End Sub
 
Upvote 0
If you want some code to update existing data, here is what that code would look like:
VBA Code:
Sub FixData()

    Dim lr As Long
    Dim r As Long
    Dim cell As Range
    
    Application.ScreenUpdating = False
    
'   Find last row in column C with data
    lr = Cells(Rows.Count, "C").End(xlUp).Row
    
'   Exit sub if no data to update
    If lr < 3 Then Exit Sub
    
'   Loop through all rows of data
    For r = 3 To lr
        Set cell = Cells(r, "C")
        If cell.Value = Round(cell.Value, 0) Then
            cell.NumberFormat = "0"
        Else
            If cell.Value = Round(cell.Value, 1) Then
                cell.NumberFormat = "0.0"
            Else
                cell.NumberFormat = "0.00"
            End If
        End If
    Next r

    Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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