Tuesday, January 19, 2010

Date Format Fix in Excel 2003

Due to an issue in Excel 2003, sometime the date formats are not updated in the cells, which leads to the problem, that a cell shows a date but is not read as a date by macros and formulas.

You could go through each cell, press 'F2' and 'Enter' and Excel would finally identify the content of the cell as a date. But this you would do manually and not by script.

With the following VBA script for Excel, the format can be updated:


Sub UpdateColumnFormat()

Dim usedRangeOfRows As Integer
usedRangeOfRows = ActiveSheet.UsedRange.Rows.Count

'Date
Columns("A:A").Select
Dim e As Range
Dim currentValueE As Date
For Each e In Range("A2:A" & usedRangeOfRows).Cells
e.Select
currentValueE = ActiveCell.Value
ActiveCell.FormulaR1C1 = currentValueE
Next

End Sub


This problem occurs regularly, when I copy data from one sheet to another by VBA scripts.

With the above script, I could fix (at least work around it) this issue.

Enjoy :)

No comments: