Solved

Excel convert date to int macro

Posted on 2014-01-29
9
592 Views
Last Modified: 2014-01-29
Hi,

I have a excel file with date column in many different format.

My database accept it as int(10), so example - 1387270706

Can you advice how i can convert my date(different format of date) to the above format.?

Thanks,
date.csv
0
Comment
Question by:magento
  • 5
  • 4
9 Comments
 
LVL 35

Expert Comment

by:mvidas
ID: 39818732
Hi magento,

I assume you're using unix date format, which I think is the number of seconds since Jan 1, 1970. If so, you can get it using the DateDiff function ("date difference") and specify seconds. You can easily incorporate it into a macro, or if you want to have a worksheet function just use:
Function ExcelDateToInt10(dateIn As Variant)
 ExcelDateToInt10 = DateDiff("s", #1/1/1970#, dateIn)
End Function

Open in new window

Matt

EDIT: Changed dateIn to Variant from double, to account for some formats
0
 
LVL 5

Author Comment

by:magento
ID: 39818747
how to run this as a macro please?
0
 
LVL 35

Accepted Solution

by:
mvidas earned 500 total points
ID: 39818807
If you want the results in your file you have two options.

1) Open your file, right-click the worksheet tab at the bottom and go to View Code, in the VBA editor go to Insert then Module, and paste in the above code. Close the VBA editor, and use it like a normal function in your workbook: =ExcelDateToInt10(B2)

2) If you want to run a macro to replace the cell contents with the converted number, run the following (changing the ranges as necessary):
Sub AnExample()
 Dim CLL As Range, dateOut As Variant
 For Each CLL In Range("B2:B1861").Cells
  dateOut = "ERROR: " & CLL.Text
  On Error Resume Next
  dateOut = DateDiff("s", #1/1/1970#, CLL.Value)
  On Error GoTo 0
  If dateOut < 0 Then dateOut = "ERROR: " & CLL.Text
  CLL.Value = dateOut
 Next
 Range("B2:B1861").NumberFormat = "General"
End Sub

Open in new window

Note that in some cases it will display "ERROR: " and the cell's contents instead of the current number. It will do this for invalid dates, like "ERROR: Jan18" or "ERROR: Sep 11, 200"
0
 
LVL 5

Author Comment

by:magento
ID: 39819045
I have a date like below, but it didnt get converted after the macro

11-Oct-07

Please advice
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Expert Comment

by:mvidas
ID: 39819089
There is no reason that wouldn't have been converted, at least as given there. Is it possible the O in "Oct" is really a zero, or the zero in "07" is a capital "O" or something?

If not, can you show me exactly what the cell says?
If you did it my #1 way above (and got a #VALUE! response from the formula), can you use
="ERROR: "&B2
and substitute the cell address for B2, and paste me the result?

If you did my #2 way above, can you paste for me the result there? I'm guessing there must be a strange character or something in the cell which is messing it up.
0
 
LVL 5

Author Comment

by:magento
ID: 39819113
Hi ,

I tried your macro , and all cells in column B has been changed correctly and only the few as provided above shows  the same value.

TimeStamp_original      TimeStamp_converted
19-Jan-14      1390167180
20-Jan-14      1390208340
Jan 26       Jan 26 
27-Jan-14      27-Jan-14
27-Jan-14      27-Jan-14


Thanks
0
 
LVL 5

Author Comment

by:magento
ID: 39819118
The file is attached for ur reference
date-ee.csv
0
 
LVL 35

Expert Comment

by:mvidas
ID: 39819144
When I ran the code on your attachment, the only errors I got were:
ERROR:  Jul 21, 201
ERROR:  May 12, 200
ERROR:  Sep 17, 201
ERROR: Dec 30, 201
ERROR: Jul 15, 201
ERROR: Jul 15, 201
ERROR: May 18, 201
ERROR: ug 4, 2007
ERROR: ul 8, 2007

My guess as to why some cells didn't get converted could be because your input range wasn't updated? My original code predefined the range to work until row 1861, is your other file longer?

Even cells like "Jan 26" should convert without issue, Excel should interpret that as the current year.
0
 
LVL 5

Author Comment

by:magento
ID: 39819944
My stupid, i didnt changed the values. Thank you.

I havent tried ur 1st solution .(I know it should work)

The 2nd solution works like charm.

Thanks again.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now