Strip/remove time from date using VBA

coffeeman12

New Member
Joined
Sep 19, 2020
Messages
8
Office Version
  1. 2016
Platform
  1. MacOS
I have been using int(cell) to convert dates with time to untimed dates, but this is messy in that I need to fill two additional columns with data. First to do the conversion, than another to paste the absolute cell values rather than a formula.

I'd like to do this in a single step in the same column (happens to be column "C") using VBA. I've tried a few methods people have posted, but none that seem to work for my simple situation.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try this with a copy of your data. Adjust the date format in the code to suit your date format.

VBA Code:
Sub Remove_Time()
    With Range("C2", Range("C" & Rows.Count).End(xlUp))
      .TextToColumns DataType:=xlDelimited, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 9), Array(3, 9))
      .NumberFormat = "d/m/yyyy"
    End With
End Sub
 
Upvote 0
Hi there,

Change the 'strMyCol' variable (if needed) and then run this while on the sheet in question:

VBA Code:
Option Explicit
Sub Macro1()

    Dim strMyCol As String
    Dim lngLastRow As Long
    Dim rngMyCell As Range
    
    Application.ScreenUpdating = False
    
    strMyCol = "C" 'Column with dates. Change to suit.
    lngLastRow = Cells(Rows.Count, strMyCol).End(xlUp).Row
    
    For Each rngMyCell In Range(strMyCol & "1:" & strMyCol & lngLastRow)
        'If there's a date in the current cell of 'strMyCol' column, then...
        If IsDate(rngMyCell) = True Then
            '...format it as 'dd/mm/yyyy' in the column to its immediate right. Change to suit.
             rngMyCell.Offset(0, 1).Value = Format(rngMyCell, "dd/mm/yyyy")
        End If
    Next rngMyCell
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,982
Members
449,201
Latest member
Lunzwe73

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