Speed up code: convert datetime to just date

always_confused

Board Regular
Joined
Feb 19, 2021
Messages
68
Office Version
  1. 2016
Platform
  1. Windows
Hello, I am trying to convert about 75 000 lines of datetime formate data (YYYY/MM/DD HH:MM:SS) into just dates (YYYY/MM/DD). The only way I have found to do this it to concatenate the YEAR(), MONTH(), DAY() parts of the data in one column into another column. However, with so many rows, this takes FOREVER and makes Excel stop responding. There must be a better way to do this, but I just don't know what it is. I need to do this only for date on the row where another value is <> 0. Here's my code:
VBA Code:
Dim lastrow As Long
lastrow = Sheets("lot").Range("A" & Rows.count).End(xlUp).Row 

For i = lastrow
If Sheets("sheet1").Range("A" & i ).value <> 0 Then
Sheets("sheet2").Range("A" & Rows.count).End(xlUp).Offset(1, 0).value = Year(Sheets("sheet1").Range("B" & i).value) & "/" & Month(Sheets("sheet1").Range("B" & i).value) & "/" & Day(Sheets("sheet1").Range("B" & i).value)
End If
Next i
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
You should be able to use DateValue Function to do what you want as this will ignore the Time part of the date.

Untested but try following & see if helps

VBA Code:
Sub ChangeDate()
    Dim lastrow     As Long, i As Long
   
    Application.Calculation = xlCalculationManual
    With Worksheets("lot")
        lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
       
        For i = 1 To lastrow
            With .Cells(i, 1)
                If IsDate(.Value) Then
                    .Value = DateValue(.Value)
                    .NumberFormat = "YYYY/MM/DD"
                End If
            End With
        Next i
    End With
    Application.Calculation = xlCalculationAutomatic
End Sub

Adjust code to meet project need as required

Dave
 
Upvote 0
Hello, I am trying to convert about 75 000 lines of datetime formate data (YYYY/MM/DD HH:MM:SS) into just dates (YYYY/MM/DD). The only way I have found to do this it to concatenate the YEAR(), MONTH(), DAY() parts of the data in one column into another column. However, with so many rows, this takes FOREVER and makes Excel stop responding. There must be a better way to do this, but I just don't know what it is. I need to do this only for date on the row where another value is <> 0. Here's my code:
VBA Code:
Dim lastrow As Long
lastrow = Sheets("lot").Range("A" & Rows.count).End(xlUp).Row

For i = lastrow
If Sheets("sheet1").Range("A" & i ).value <> 0 Then
Sheets("sheet2").Range("A" & Rows.count).End(xlUp).Offset(1, 0).value = Year(Sheets("sheet1").Range("B" & i).value) & "/" & Month(Sheets("sheet1").Range("B" & i).value) & "/" & Day(Sheets("sheet1").Range("B" & i).value)
End If
Next i

TYPO : Here's the correct code

VBA Code:
Dim lastrow As Long
lastrow = Sheets("sheet1").Range("A" & Rows.count).End(xlUp).Row

For i = lastrow
If Sheets("sheet1").Range("A" & i ).value <> 0 Then
Sheets("sheet2").Range("A" & Rows.count).End(xlUp).Offset(1, 0).value = Year(Sheets("sheet1").Range("B" & i).value) & "/" & Month(Sheets("sheet1").Range("B" & i).value) & "/" & Day(Sheets("sheet1").Range("B" & i).value)
End If
Next i
 
Upvote 0
You should be able to use DateValue Function to do what you want as this will ignore the Time part of the date.

Untested but try following & see if helps

VBA Code:
Sub ChangeDate()
    Dim lastrow     As Long, i As Long
 
    Application.Calculation = xlCalculationManual
    With Worksheets("lot")
        lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
     
        For i = 1 To lastrow
            With .Cells(i, 1)
                If IsDate(.Value) Then
                    .Value = DateValue(.Value)
                    .NumberFormat = "YYYY/MM/DD"
                End If
            End With
        Next i
    End With
    Application.Calculation = xlCalculationAutomatic
End Sub

Adjust code to meet project need as required

Dave
I get a type mismatch at the
VBA Code:
 .value = DateValue(.value)
do I need to format the calls as date first maybe?
 
Last edited:
Upvote 0
I get a type mismatch at the
VBA Code:
 .value = DateValue(.value)
do I need to format the calls as date first maybe?

this line
VBA Code:
If IsDate(.Value) Then

should determine if cell contains a date & if so, DateValue should work without erroring
but to understand what is going on, It would be helpful if you a sample of your worksheet using MrExcel Addin XL2BB - Excel Range to BBCode

Dave
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,123
Members
453,021
Latest member
Justyna P

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