You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
What I am trying to do is search a currentregion for all dates stored as text and convert them to proper dates using the datevalue function. However when the code picks up a cell value that is anything other than a date stored as text then I get a run-time error 13 type mismatch and I am unable to get around it. Any ideas?
This was my attempt
Sub TextToDate()
Dim rngCurrentCell As Excel.Range
Dim dateTemp
Range("A10").Select
For Each rngCurrentCell In ActiveCell.CurrentRegion.Cells
If IsError(DateValue(rngCurrentCell)) = False Then
dateTemp = DateValue(rngCurrentCell)
rngCurrentCell = dateTemp
rngCurrentCell.NumberFormat = "dd/mm/yyyy;@"
End If
Range("A10").Select
For Each rngCurrentCell In ActiveCell.CurrentRegion.Cells
On Error Resume Next'********************
If IsError(DateValue(rngCurrentCell)) = True Then '************
Else'********************
dateTemp = DateValue(rngCurrentCell)
rngCurrentCell = dateTemp
rngCurrentCell.NumberFormat = "dd/mm/yyyy;@"
End If
Next rngCurrentCell