richD
richD

Reputation: 1

Keeping a date format when converting to text in VBA

I am struggling with handling dates within excel/VBA

I have a spreadsheet with a date column, displaying in UK format as expected.

I am then manipulating the spreadsheet and saving it as a text file in order to load the data into another system.

My problem comes when I save the file as it does two things, first due to the US bias of Excel, it attempts to "flip" the dates so for example 1st July 2012 goes from 01/07/2012 to 07/01/2012

Secondly when I then save the file and re-open the text file, it seems to have then dropped the zeros so my date now not only reads the wrong way round, but with no zeros so is showing 7/1/2012

The system I am importing to is very specific about the format of the dates it accepts.

I have tried all sorts

ActiveCell.FormulaR1C1 = "=TEXT(RC[-1],""dd/mm/yyyy"")"

Cells(x, 3).Value = Left(CStr(Cells(x, 3).Value), 2) + "/" + Mid(CStr(Cells(x, 3).Value), 4, 2) + "/" + Right(CStr(Cells(x, 3).Value), 4)

Within Excel both of these do the trick in keeping the format I require, it is when I then save the file as text I get the problems above.

I almost want to pad the date with zeros and force the save as a literal string, but I am running out of ideas.

I am sure this is a simple fix, but I need some other eyes on the problem so any help is appreciated

Upvotes: 0

Views: 3528

Answers (1)

Doug Rae
Doug Rae

Reputation: 51

I had the same problem when saving to .csr file to import into Access. The answer is to add Local:=True to the SaveAs statement. This keep the dates as UK format.

Below is the code I used:

' save data to csv file
ChDir "C:\Documents and Settings\Doug\My Documents\Fitness\LogCard Data"
ActiveWorkbook.SaveAs Filename:= _
 "C:\Documents and Settings\Doug\My Documents\Fitness\LogCard Data\LogCard_access.csv" _
    , FileFormat:=xlCSV, CreateBackup:=False, Local:=True

Upvotes: 5

Related Questions