Date Format error. date or text.

Combat Womble

New Member
Joined
Nov 18, 2015
Messages
27
Hello

I recieved a load of files and use macros to sort clean etc before add the data to other files. These files are then used for data analysis. The date can be an important part of this analysis. I have descovered an error in which the date isn't a date and when the macro sorts a sheet by date the result is incorrect.

So, the error occurs when the input date is a cell that looks like 12-12-2015 (from now on I am going to call it a hyphenated cell), when the input cells look like 12/12/2015 (from now on I am going to call it a slash cell) everything works fine.

Before the macro starts, when I click on the hyphenated cell, excel tells me its a date (home ribbon, number). After the macro has finished excel still tells me its a date, but it has not been ordered by date (unlike the others). Also if i expand the column, then the hyphenated cells are left justified and the slash cells are right justified.

I have tried the following:
1. replace the hyphens with slashes
VBA Code:
ActiveCell.value = Replace(ActiveCell.Value, "-", "/")
2. changing the cell format
Code:
Selection.NumberFormat = "dd/mm/yy"
'and'
Selection.NumberFormat = "m/d/yyyy"
'and'
ActiveCell.NumberFormat = "dd/mm/yy;@"
3. doing 1 and 2
Code:
Selection = Format(Replace(ActiveCell.Value, "-", "/"), "dd/mm/yyyy")
4 remove contents of cell, change cell format add date
Code:
If InStr(ActiveCell.Value, "-") > 0 Then
    arrayA = Split(ActiveCell, "-")
    ActiveCell.ClearContents
    ActiveCell.NumberFormat = "dd/mm/yy;@"
    ActiveCell.Value = arrayA(0) + "/" + arrayA(1) + "/" + arrayA(2)
End If

Any pointers in the right direction would be amazing and appreciated

edit:

i forgot to mention that I tried selecting the column and changing the format to date. I did this by recording a macro and copying its code.

Combat Womble
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
1,091
Office Version
  1. 2016
Platform
  1. Windows
ActiveCell.Value = CDate(Replace(ActiveCell.Value, "-", "/"))
or
ActiveCell.Value = DateValue(ActiveCell.Value)
or
ActiveCell.Value = ActiveCell.Value * 1
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,129,727
Messages
5,638,025
Members
416,999
Latest member
smulttjukken

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