Using VBA to Remove Time from a Date in Excel – 3 Methods

 

Dataset to remove time from date in excel vba

The above image is the sample dataset.


Method 1 – Embed VBA to Delete Time from Dates in a Column in Excel

Remove time in Column D.

Steps:

  • Press Alt + F11 or go to Developer -> Visual Basic to open Visual Basic Editor.

  • Click Insert -> Module.

  • Enter the following code into the code window.
Option Explicit
Sub DeleteTime()
Dim iRange As Long
Dim iCount As Long
iRange = Range("D" & Rows.Count).End(xlUp).Row 'column D has the date values
    For iCount = 5 To iRange 'from row number 5 our date values start
        With Range("D" & iCount)
            .NumberFormat = "dd/mm/yyyy"
            .Value = DateValue(.Value)
        End With
    Next iCount
End Sub

 

Remove time from date of a column in excel vba

  • Press F5 or click Run -> Run Sub/UserForm. You can also click the small Run icon to run the macro.

This is the output.

Result of remove time from date of a column in excel vba

 

Read More: Date Variable in VBA Codes


Method 2 – Applying a VBA Macro to Remove Time from a Range of Dates

Steps:

  • Open the Visual Basic Editor in the Developer tab and Insert a Module in the code window.
  • Enter the following code:
Option Explicit
Sub RemoveTime()
Dim iRange As Range
Dim iData As Range
On Error Resume Next
Set iData = Application.Selection
Set iData = Application.InputBox("Select Range", "Excel", iData.Address, Type:=8)
For Each iRange In iData
    iRange.Value = VBA.DateValue(iRange.Value)
Next
iData.NumberFormat = "dd/mm/yyyy"
End Sub

 

Remove time from a range of date in excel vba

  • Run the macro.
  • A pop-up box will ask you to enter the range. Drag the whole range. Here, D5:D9.
  • Click OK.

Selecting range to remove time from date in excel vba

This is the output.

Result of remove time from a range of date in excel vba

 

Read More: Excel VBA Textbox to Format Date


Method 3 – Using a Macro to Separate Date from Time in Excel

Consider the following image.

To separate the dates in Column D and place them in Column F:

 

Steps:

  • Open the Visual Basic Editor in the Developer tab and Insert a Module in the code window.
  • Enter the following code:
Option Explicit
Sub SeparateDate()
    Dim iCount As Integer
    With Worksheets("Separate")
        'to check if D5 is in date format or in text format
        iCount = Abs(Not IsDate(.Cells(5, 4).Value))
        With .Range(.Cells(5 + iCount, "D"), .Cells(.Rows.Count, "D").End(xlUp))
            .TextToColumns Destination:=.Cells(1, "C"), DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, xlDMYFormat), Array(10, xlSkipColumn))
            'to assign a custom number format (optional)
            .Offset(0, 2).NumberFormat = "[color5]dd-mm-yyyy_)"
            'to autofit the column width (optional)
            .Offset(0, 2).EntireColumn.AutoFit
        End With
    End With
End Sub

 

Remove time from date and place differently in excel vba

  • Run the macro.

This is the output.

 


Download Practice Workbook

Download the free practice Excel workbook.

 

Get FREE Advanced Excel Exercises with Solutions!
Sanjida Ahmed
Sanjida Ahmed

Sanjida Ahmed, who graduated from Daffodil International University with a degree in Software Engineering, has worked with SOFTEKO since 2021. She has written over 100 articles on Excel & VBA and, since 2022, has worked as the Project Manager of the Excel Extension Development Project in the Software Development Department. Since starting this software development, she has established an outstanding workflow encompassing a full SDLC. She always tries to create a bridge between her skills and interests in... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo