Please help me create a macro that converts dates written with names to dates written with numbers

Good evening,

I regularly receive spreadsheets with dates written like this:

'19-APR-2020

or

19-APR-2020

I can’t use such formats in my calculations so I would like to convert them with one click (macro) to normal date format. Can you please help me create such macro?

EDIT: I receive Excel spreadsheets from the client with multiple columns, one of them being the “Date” column with entries such as examples above. I need to copy values from the date column in their spreadsheet to my spreadsheet which also has a Date column. But the values need to be “real” dates so I can work with them. I uploaded a sample, please take a look. My locale is Croatian.

02.xls

EDIT2: uploaded the non-working macro made by following Lupp’s instructions for troubleshooting

macro Lupp

EDIT3: uploaded the non-working macro made by following Keme’s steps

macro keme

Doesn’t DataText to Columns... fit your task?

Yes, it does for the cells that begin with apostrophe (my first example) but it doesn’t for my second example (without apostrophe).

Your locale?
In an English locale the second “thing” might be a date represented by a number (as is usual in spreadsheets) and just formatted in that nonsense style. (The default formats used in en-locales are even worse! And the most idiotic “date format” in the world is the one currently used by this site.)
Anyway: If you actually need to convert dates from any text format using letters for the months, you need to provide a complete list of the names for months (or abbreviations) used by your “dates” on the one hand and by your locale on the other hand if someone should be able to help you beyond the suggestion by @mikekaganski. .

Could I convert my second example to have an apostrophe like my first example and how?

To avoid more guessing I would suggest you attach a sample.ods showing the problem for you.
Please do so by editing your original question and appending a few lines to it. Use the tool whose icon is showing a paperclip.

Thank you, I uploaded samples.

-1- I couldn’t find any example of the apostrophed type.
(-2- I still am missing a complete list of the texts abbreviating the months. An example containin dates spread over all the year was prteferrable.)
-3- My installed LibO is set to UI and locale en-UK, but if I change the “language” on the ‘Numbers’ tab of the cell formatting dialog to ‘German’(e.g.), the cell will automatically recognise a date entered with text parts being used in German. Why shouldn’t this work for you:
Select the column of dates, set the Numbers format language to English (some version), call F&R, enable RegEx, enter .* behind Find: and & behind Replace:, run Replace All. The re-insertion of the text should trigger a new attempt of “recognition” with the effect that you get dates repesented in the common way by numbers.

-1. The whole 01.xls file is with apostrophe. But the bigger problem file is 02.xls, the one without the apostrophe.

-2. This problem has started few months ago so I don’t have the whole year behind me. I can assume the list of abbreviations will follow English language until the end:

JAN
FEB
MAR
APR
MAY
JUN
JUL
AUG
SEP
OCT
NOV
DEC

-3. Changing the language for the Date column to English as you’ve suggested creates the oh so wanted apostrophe in the 02.xls which makes is then possible to do the F&R you’ve suggested.

I will test some more and get back with results. Thanks!

Your solution "Select the column of dates, set the Numbers format language to English (some version), call F&R, enable RegEx, enter .* behind Find: and & behind Replace:, run Replace All." works perfect but when I record the macro the “set the Numbers format language to English (some version)” part is missing. It doesn’t get recorded so when I execute the macro nothing happens. I’ve uploaded the macro so you can see it and maybe spot something. Does recording a change of language in a macro work for you?

I hope you can find time and try to create a macro on my 02.xls sample.

Suggestion

Depending on context, the DATEVALUE() function may be useful. Look into that.

Existing functions make your solution portable and predictable, which will contribute to robustness. Creating similar solutions ad hoc often introduces additional source(s) of error.

Without more specific info about how you receive your dates, how they are transferred to Calc and how your data are otherwise laid out, it is hard to provide any specific advice.

EDIT:

Based on your comment indicating that you get multiple dates from a client, laid out in a tabular setup

I would add a column (*) when you have acquired the client data.

DATEVALUE() works on text content, which is indicated by the apostrophe you mention. Dates properly entered will be internally represented as the same number, regardless of formatting. When you create a formula in a new cell, that same numerical representation will receive the default formatting of your choice. So your new column will need to use DATEVALUE() if submitted date is represented as text, and the data (original “date value”) as entered otherwise.

See attached file for a proposed solution.

(*) All data conversions of this kind (interpreting “human-friendly” data formats) carry some risk of error. When you keep the converted date with the original instead of replacing, it is easier to catch and correct such errors.

To your other question

Yes, if you must.

You could convert everything to text values by using the TEXT() function, and take it from there. As far as I can see, that would require a few additional steps, which also means a couple of places to make mistakes.

While converting everything to text, so “all is what it looks like”, may seem logical, this removes the exact date representation first. The important content for calculations, sorting and grouping is that exact, “presentation independent”, numerical date representation which I have suggested you create by the use of DATEVALUE() and which you will also achieve by the text-to-columns procedure (and which, by default, will receive a visual, human-readable date representation unless you deliberately format the cells otherwise).

EDIT 2:

Based on your attached sample files

The dates in your files are all text.

To convert:

  • Select the column of dates
  • Menu item Data - text to columns
  • Click the column header in the preview pane (bottom of the dialog)
  • Select the appropriate date format for column type (dropdown just above the preview pane)
  • OK

This worked fine for me without changing locale for file or cells. None of the cells carried the apostrophe you mentioned, but I inserted one manually in a couple of cells, and they were also converted correctly.

My locale is Norwegian, but English month names were interpreted correctly. Croatian month names are significantly different from English names, which may be the cause if the above procedure does not work. If so, try to change the cell language/locale setting and run text to columns again.

Setting language/locale for a cell range:

See menu item Format - Cells. There is a language selection in the Text tab, and one in the Numbers tab. Either one set to English (USA) may work. You will just have to try. The one in the Numbers tab will select a different default date format (the one Lupp expressed rather hostile emotions towards :wink: ) but as mentioned earlier, formatting is presentation and can be changed at will, as long as content is properly set to a number representing a date.

Thanks, Keme. To answer your questions: I receive Excel spreadsheets from the client with multiple columns, one of them being the “Date” column with entries such as examples above. I need to copy vaues from their date column to my spreadsheet which also has a Date column. But the values need to be “real” dates so I can work with them.

About your DATEVALUE() solution - do I need to insert a new column next to my client’s Date column to make it work? Or is there a way to convert the text to numbers in the existing column?

My second question - could I convert my second example to have an apostrophe like my first example and how? That would be the easiest solution as then I could use the Data → Text to columns… solution.

The DATEVALUE() solution file you’ve uploaded doesn’t work for dates in my second example (written without an apostrophe). I have uploaded two typical samples from the client’s spreadsheet. My locale is Croatian if it matters. Thank you

Sorry! My initial suggestion was based on a misunderstanding of mine. Your mention of apostrophe made me think that your dates were a list of “locale compliant” dates entered as text, intermixed with proper date values. When a cell contains a text string which constitutes valid numerical input, Calc “tags” it as text by prepending an apostrophe when in “edit context”.

From your posted files, DATEVALUE() will not work for this date format. However, there is not one value there which is not correctly handled by Text to columns, as per @mikekaganski’s first response. Manually inserted apostrophe (as per your initial “sample”) also does not interfere with date conversion in this way.

It is strange that date is interpreted nicely with text to columns, but DATEVALUE() does not work. However, as far as I can tell from what you have submitted, the first answer was the right one. If not, submit a file with “offending data” (dates that will not convert by text-to-columns).

The text-to-columns solution converts perfect but just as with Lupp’s solution the macro doesn’t record all the steps!

Take a look at the recorded macro below and tell me if it looks all right to you. I can’t see the following steps:

“-Click the column header in the preview pane (bottom of the dialog)
-Select the appropriate date format for column type (dropdown just above the preview pane)”

I’ve uploaded the macro code above in my original post. Can you record a macro with all the steps?

I hope you can find time and try to create a macro on my 02.xls sample.

In addition to what I told in comments, you can use a formula as shown in this attached file made from one of those attached to the question by editing.
(I never would use .xls myself.)
Of course, the inline constant array of month abbreviations is a crutch.
There are so many ways. …

(I really get tired of wasting time again and again with this nightmare of locales (>200 meanwhile) and the incredibly silly refusal by many to exclusively use ISO 8601 conforming date and time formats. Who are we?)
If still interested in related “macros” see also this tutorial. It was written long ago for StarBasic, but most of it is still valid also in LibreOffice. It gives lots of relevant information in addition.

By the way: There are more than 230 locales today. Some of them come with the same set of predefined NumberFormat_s. An indepedent locale, if used in a single cell, imports 69 of them in addition.