Convert time and date which are text to proper time and date

uk747

Well-known Member
Joined
Jul 20, 2011
Messages
764
Office Version
  1. 365
Platform
  1. Windows
I have times in H1:Q1 which are formatted as hh:mm but are actually text
E.g. 04:20. 05:11

I also have dates in h3:Q3 which are formatted as dd-mm-yy are also text

I would like VBA to convert the times to proper times and dates to proper dates

Thanks
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

EFANYoutube

Active Member
Joined
May 19, 2017
Messages
380
Office Version
  1. 365
Platform
  1. Windows
Hi uk747

Just use =DateValue() & =TimeValue()
 

uk747

Well-known Member
Joined
Jul 20, 2011
Messages
764
Office Version
  1. 365
Platform
  1. Windows
Thanks
I know about the formulas, but how would I use it within VBA to convert the 2 ranges
 

EFANYoutube

Active Member
Joined
May 19, 2017
Messages
380
Office Version
  1. 365
Platform
  1. Windows
Thanks
I know about the formulas, but how would I use it within VBA to convert the 2 ranges
It depends what you want to do, do you want the value of the cell to be replaced by the true time/date?
 

EFANYoutube

Active Member
Joined
May 19, 2017
Messages
380
Office Version
  1. 365
Platform
  1. Windows
Heres a function you can use
VBA Code:
Function ConvDateTime()
    Dim cell As Range
    For Each cell In ThisWorkbook.Sheets("Sheet1").Range("H1:Q1").Cells
        cell.Value = TimeValue(cell.Value)
    Next cell
    For Each cell In ThisWorkbook.Sheets("Sheet1").Range("H3:Q3").Cells
        cell.Value = DateValue(cell.Value)
    Next cell
End Function
 

uk747

Well-known Member
Joined
Jul 20, 2011
Messages
764
Office Version
  1. 365
Platform
  1. Windows
Thanks

If I have it in a function, do I just call it from a sub
 

EFANYoutube

Active Member
Joined
May 19, 2017
Messages
380
Office Version
  1. 365
Platform
  1. Windows
Yes correct like

Sub calldt
ConvDateTime()
End sub
 

Forum statistics

Threads
1,176,185
Messages
5,901,805
Members
434,918
Latest member
kpk92

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
Top