Excel changes dates where the day is lower than the month (e.g 2nd april) to the reverse (e.g 4th feb)

StackOverflow https://stackoverflow.com//questions/25077178

  •  02-01-2020
  •  | 
  •  

Question

I am having some toruble with excel and dates. The situation is as follows: We have a monthly report with a number of tabs which the macro imports into a central spreadsheet, it has about 120 columns. After importing, I need to add some formulas in the final few columns that are relevant to the data but not included in the data, and one of these is the period number (we work for a client with the year starting on April 1st and 13 periods of 4 weeks in the year). To acomplish this, I have a seperate sheet with all of the dates in column A, then periods and other data in the following columns. A simple Vlookup looks up the date and returns the period.

However, some of the dates come stored as text (and so the vlookup has #N/A), simple fix, paste special - add a blank cell and voila it works. And this does work if I do it manually. But this is meant to do all the work for the user so I tried to do it programatically as per the code below:

'Sheets("dates and Periods").Activate
'Range("P1").Copy
'Sheets("Data").Activate
'Range(Cells(2, 5), Cells(lastrow, 5)).PasteSpecial xlPasteValues, xlPasteSpecialOperationAdd
'Range(Cells(2, 5), Cells(lastrow, 5)).NumberFormat = "dd/mm/yyyy"

I get the blank cell from "Dates and Periods" because I know it will definitely be blank.

Now when I run this code, it works for most values. However for all dates in column E where the day is smaller than the month, e.g 1st April, 4th May etc, it switches the day and month around, and this is not formatting, by changing to long date I can see it has changed the data around.

Is there a way around this? I have looked for a solution but all of the problems I could find when searching were to do with the 1904 date system or with formatting.

Any help would be appreciated.

Solution: [breaking it into parts and putting it back together]

For Each cell In Range(Cells(2, 5), Cells(lastrow, 5))

cell.Value = Mid(cell, 4, 2) & "/" & Left(cell, 2) & "/" & Right(cell, 4)

Next cell
Was it helpful?

Solution

i have had this problem many times. the answer is usually specific to the solution, but you can try converting the date to a string in dd-mmm-yyyy format, then back again.

OTHER TIPS

When you enter something that looks like a date into an Excel cell, it will parse it according to the Windows Regional Settings. Strings that don't fit get entered as text, strings that do get turned into Excel dates. This latter will usually not be the same as the intended date.

I would suggest correcting this during the import process. Exactly how to do this depends on specifics you have not included, but the process is to use the Data/Import process and go through the Text Import Wizard. That wizard will allow you to specify the format for each column; including the date format for E. That will ensure that all of your dates are properly understood by Excel as being dates. So if the incoming date format is MDY, you would specify MDY; and Excel would properly store it, and then display it either in your default setting, or in whatever way you format that column.

Another option, if feasible, would be to alter the report generator so that it generates dates that are in the same format as the short date format of your Windows Regional Settings

I import a large number of files emailed to me into data bases and this is consistently a problem. The issue isn't caused by incorrect data, bad formatting or any such thing, but simply an error in excel (I believe Microsoft call it a feature), where it tries to understand dates in the American format and automatically 'corrects' anything in finds. It is made worse because if a date doesn't work in the American format then it converts it into a text file leaving you with some dates formatted one way, some another.

The only solution I have found is to alter the data on the source sheet so that Excel cannot understand it as a date in any way, then import it, then convert it back, a huge phaff. Microsoft please sort this out, it's wasting so much time programming workarounds!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top