VBA - Using NumberFormat on a range but only on cells containing Dates

30136353

Board Regular
Joined
Aug 14, 2019
Messages
105
Hi Guys,
I have the below code which is working great, but the part of the code which changes the number format in Column D to "d/m", I need this to only work on cells containing dates. Within Column D I have address numbers, some are automatically held as dates, but some may be single numbers like "6". After I format using the above 6 would show as 6/1.... I understand dates are essnaitally large numbers, so if there is way to only Numberformat cells greater tha
VBA Code:
    Sub AddressFix()

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

 
    Columns("A:M").HorizontalAlignment = xlCenter
    Columns("D:D").Replace What:="0", Replacement:="", LookAt:=xlPart
    Columns("D:D").Replace What:=" ", Replacement:="", LookAt:=xlPart

    Dim c  As Range
    For Each c In Range("D1:D" & Cells(Rows.Count, "D").End(xlUp).Row)
    If c.Value = "" Then c.Value = c.Offset(, -1).Value
    Next
    
    Columns("A").ColumnWidth = 17
    Range("D2:D200").NumberFormat = "d/m"
     Range("C2:C200").ClearContents
     End Sub
n say 2000, then this would work. But I'm not sure hwo to write this within the code...


Thanks,
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
How is your date data stored and formatter to start with? If it is entered as dates and you just want to change the formatting this should work. You have to use a loop, which will take a little bit longer than what you have now.

Excel dates are not "essentially large numbers." The underlying value is the number of days since 1/1/1900. Today's date, for example, is 43,819. That is not that big a number. But Excel does distinguish whether a value is a number that is in that range vs. a date.

Rich (BB code):
   Sub AddressFix()

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False


    Columns("A:M").HorizontalAlignment = xlCenter
    Columns("D:D").Replace What:="0", Replacement:="", LookAt:=xlPart
    Columns("D:D").Replace What:=" ", Replacement:="", LookAt:=xlPart

    Dim c  As Range
    For Each c In Range("D1:D" & Cells(Rows.Count, "D").End(xlUp).Row)
    If c.Value = "" Then c.Value = c.Offset(, -1).Value
    Next
   
    Columns("A").ColumnWidth = 17
    For Each c In Range("D2:D200")
      If IsDate(c) Then
         c.NumberFormat = "d/m"
      End If
   Next c
     Range("C2:C200").ClearContents
     End Sub
 
Last edited:
Upvote 0
How is your date data stored and formatter to start with? If it is entered as dates and you just want to change the formatting this should work. You have to use a loop, which will take a little bit longer than what you have now.

Excel dates are not "essentially large numbers." The underlying value is the number of days since 1/1/1900. Today's date, for example, is 43,819. That is not that big a number. But Excel does distinguish whether a value is a number that is in that range vs. a date.

Rich (BB code):
   Sub AddressFix()

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False


    Columns("A:M").HorizontalAlignment = xlCenter
    Columns("D:D").Replace What:="0", Replacement:="", LookAt:=xlPart
    Columns("D:D").Replace What:=" ", Replacement:="", LookAt:=xlPart

    Dim c  As Range
    For Each c In Range("D1:D" & Cells(Rows.Count, "D").End(xlUp).Row)
    If c.Value = "" Then c.Value = c.Offset(, -1).Value
    Next
  
    Columns("A").ColumnWidth = 17
    For Each c In Range("D2:D200")
      If IsDate(c) Then
         c.NumberFormat = "d/m"
      End If
   Next c
     Range("C2:C200").ClearContents
     End Sub
That worked a treat, thanks for the help.
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,395
Members
449,081
Latest member
JAMES KECULAH

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