?
Solved

Excel convert date to int macro

Posted on 2014-01-29
9
Medium Priority
?
628 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

764 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