Solved

Excel convert date to int macro

Posted on 2014-01-29
9
623 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 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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

728 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