Solved

Excel convert date to int macro

Posted on 2014-01-29
9
579 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to open Excel file 15 39
VBA Code Mixed Combining Two User Forms 7 38
Excel 6 18
Formula to convert ranked data 2 10
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

17 Experts available now in Live!

Get 1:1 Help Now