{"id":55253,"date":"2008-06-28T02:20:00","date_gmt":"2008-06-28T02:20:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/06\/28\/hey-scripting-guy-how-can-i-prevent-office-excel-from-turning-my-imported-numbers-into-dates\/"},"modified":"2008-06-28T02:20:00","modified_gmt":"2008-06-28T02:20:00","slug":"hey-scripting-guy-how-can-i-prevent-office-excel-from-turning-my-imported-numbers-into-dates","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-prevent-office-excel-from-turning-my-imported-numbers-into-dates\/","title":{"rendered":"Hey, Scripting Guy! How Can I Prevent Office Excel From Turning My Imported Numbers Into Dates?"},"content":{"rendered":"<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" class=\"nearGraphic\" \/><\/p>\n<p>Hey, Scripting Guy! I have a CSV file, and every time I open that file in Office Excel my numbers get converted to dates. How can I import numbers as numbers?<\/p>\n<p>&#8212; JR<\/p>\n<p><img decoding=\"async\" height=\"5\" width=\"5\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" alt=\"Spacer\" border=\"0\" \/><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" class=\"nearGraphic\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" height=\"288\" width=\"120\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" align=\"right\" alt=\"Script Center\" border=\"0\" title=\"Script Center\" class=\"farGraphic\" \/><\/a><\/p>\n<p>Hey, JR. You know, just yesterday the Scripting Guy who writes this column learned about <a target=\"_blank\" href=\"http:\/\/www.yelonyc.com\/\"><b>Yelo<\/b><\/a>, an interesting little company based in New York City. So what does Yelo do? Well, primarily what they do is offer rooms (&ldquo;Yelo cabs&rdquo;) where people go and take a nap. That&rsquo;s right: if you live in New York City you can pop into the Yelo offices, write them a check for $15, and they&rsquo;ll let you take a nap in one of their rooms. (You can also get a 40-minute nap for $28.) Needless to say, this has to be the greatest business model in history: people pay you for the opportunity to <i>sleep<\/i>. Revolutionary.<\/p>\n<p>Well, OK, maybe not <i>that<\/i> revolutionary; after all, people has been paying money for years to sleep through one of Dennis Miller&rsquo;s standup routines. But we don&rsquo;t think he <i>intended<\/i> for them to sleep through those routines.<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"END\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\"><b>Note<\/b>. Admittedly, the Scripting Guy who writes this column has never slept through a Dennis Miller standup routine; for that matter, he&rsquo;s never even <i>seen<\/i> a Dennis Miller standup routine. (Listening to him on Monday Night Football was more than enough.) However, the Scripting Guy who writes this column <i>did<\/i> pay $40 a ticket to sleep through the Nutcracker Ballet, if that helps.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>In all fairness, we should point out that you get more than just a nap when you visit Yelo; instead, you get a power nap. What&rsquo;s the difference between a nap and a power nap? About $15 for every 20 minutes, as near as we can tell.<\/p>\n<p>OK, so we&rsquo;re being a little mean; we have nothing against naps or against Yelo. In fact, we&rsquo;re jealous. After all, the Scripting Guy who writes this column has been lulling people to sleep for years, and has never gotten a penny for it.<\/p>\n<p>Speaking of which, looks like it&rsquo;s time to address today&rsquo;s question. JR has a CSV (comma-separated values) file that looks like this:<\/p>\n<pre class=\"codeSample\"><pre class=\"codeSample\">1-2\n2-3\n3-4\n4-3<\/pre>\n<p>That seems innocuous enough, but each time he opens this file Microsoft Excel &hellip; helpfully &hellip; converts these values to dates:<\/p>\n<pre class=\"codeSample\">2-Jan\n3-Feb\n4-Mar\n6-May<\/pre>\n<p>And yes, that <i>is<\/i> very nice of Excel, isn&rsquo;t it? However, that isn&rsquo;t what JR wants to do; instead, he wants Excel to leave those values exactly as they are. How can he do that? Why, by running the following script, of course:<\/p>\n<pre class=\"codeSample\"><pre class=\"codeSample\">Const xlTextFormat = 2\n\nSet objExcel = CreateObject(\"Excel.Application\")\nobjExcel.Visible = True\n\nobjExcel.Workbooks.OpenText \"C:\\Scripts\\Test.txt\",,,,,,,,,,,, _\n    Array(Array(1, xlTextFormat))<\/pre>\n<p>As you can see, this isn&rsquo;t a particularly long script (although that last line <i>is<\/i> a bit of a doozy, isn&rsquo;t it?). We start things out by defining a constant named xlTextFormat and setting the value to 2; we&rsquo;ll use this constant to tell Excel that we want to format our imported values as text rather than dates. Why text? Shouldn&rsquo;t we be formatting these values as <i>numbers<\/i>?<\/p>\n<p>Believe it or not, the answer to that is no, we shouldn&rsquo;t. Why not? Because 1-2 and 2-3 aren&rsquo;t actually numbers. (If you don&rsquo;t believe us, trying counting to 2-3. It&rsquo;s going to be awhile before you get there.) If you give Excel something that looks like a number then it will try to convert that something <i>into<\/i> a number. But as far as Excel is concerned, 1-2 doesn&rsquo;t look like a number; instead, it looks like a date. That&rsquo;s why Excel keeps transforming JR&rsquo;s data into date-time values; Excel thinks those <i>are<\/i> date-time values.<\/p>\n<p>Incidentally, this has nothing to do with the fact that we&rsquo;re using a script to import these values. Open up Excel, type the following in cell A1, and then press ENTER:<\/p>\n<pre class=\"codeSample\">1-2<\/pre>\n<p>On the Scripting Guys&rsquo; test machine (running US English) Excel immediately converts 1-2 to the following:<\/p>\n<pre class=\"codeSample\">2-Jan<\/pre>\n<p>Hmmm &hellip;. OK, what if we apply a numeric format to cell A1. Do that, and our value will change to this:<\/p>\n<pre class=\"codeSample\">39449.00<\/pre>\n<p>Where did <i>that<\/i> come from? Well, as far as Excel is concerned, history began on January 1, 1900; Excel has marked that as Day 1. That means that January 2, 1900 was Day 2; January 3, 1900 was Day 3; and, by extension, January 2, 2008 was Day 39449.<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"EFF\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\"><b>Note<\/b>. To test this, type 1\/1\/1900 in cell A1; Excel should then display the value 1.00. We next tried typing in the Scripting Editor&rsquo;s birth date, but all we got back was a negative number. Wonder what <i>that<\/i> means &hellip;.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>As it turns out, the only way to display the value 1-2 is to format the cell as text. But we can&rsquo;t apply that formatting after the fact; that is, we can&rsquo;t import the values and then format the cells as text. If we try that, Excel will simply take the value 39449.00 (the number equivalent of January 2, 2008) and convert <i>that<\/i> to a string value:<\/p>\n<pre class=\"codeSample\">39449<\/pre>\n<p>Not really what we had in mind. Instead, we have to explicitly format those values as text up front. And that &ndash; to make a long story just a tiny bit longer &ndash; is why we need to define the constant xlTextFormat.<\/p>\n<p>Don&rsquo;t you like it when the explanation for the first line of code is 150 times longer than the entire script?<\/p>\n<p>Fortunately, very little explanation is required for the next two lines of code: in those lines we simply create an instance of the <b>Excel.Application<\/b> object, then set the <b>Visible<\/b> property to True. That simply gives us a running instance of Excel that we can see on screen.<\/p>\n<p>That also brings us to our last line of code<\/p>\n<pre class=\"codeSample\">objExcel.Workbooks.OpenText \"C:\\Scripts\\Test.txt\",,,,,,,,,,,, _\n    Array(Array(1, xlTextFormat))<\/pre>\n<p>All we&rsquo;re doing here is using the <b>OpenText<\/b> method to open our CSV file (C:\\Scripts\\Test.txt). OK, but then what are all those commas following the file path for? Well, as we noted a minute ago, we need to specifically import our file as text. Excel has a parameter (FieldInfo) that enables us to specify a data type when opening a text file. The FieldInfo parameter must be the 13<sup>th<\/sup> parameter passed to the OpenText method; the first parameter is the file path, the second, optional parameter is the file origin; etc. (For a complete list of parameters, see Excel&rsquo;s <a target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa195814(office.11).aspx\"><b>VBA Language Reference<\/b><\/a>). Other than the first (the file path) and the 13<sup>th <\/sup>(FieldInfo) parameters we don&rsquo;t need to use any special values; for parameters 2 through 12 we can just go with the default values. However, we still need to use a placeholder (a blank space) to represent those parameters. For example, what if our last line of code looked like this:<\/p>\n<pre class=\"codeSample\">objExcel.Workbooks.OpenText \"C:\\Scripts\\Test.txt\", Array(Array(1, xlTextFormat))<\/pre>\n<p>It&rsquo;s a cleaner-looking line of code, but it&rsquo;s also doomed to fail: that&rsquo;s because we made FieldInfo the <i>second<\/i> parameter, which means Excel will think this is the file origin. It&rsquo;s not, and, as a result, our script is going to blow up. That&rsquo;s why we put in all those commas: they represent parameters for which we&rsquo;re simply using the default value.<\/p>\n<p>Then what about our 13<sup>th<\/sup> parameter:<\/p>\n<pre class=\"codeSample\">Array(Array(1, xlTextFormat))<\/pre>\n<p>As it turns out, field information must be passed as an array of arrays; that&rsquo;s why we have the mini-array <b>Array(1, xlTextFormat)<\/b> enclosed in another call to the <b>Array<\/b> function. As you might expect, each mini-array represents a field in our CSV file. The first value in the array represents the field number; in this case we have a 1 because we&rsquo;re dealing with the first (and only) field in the CSV file. The second value in the array represents the data type; we need to import this data as text, so we use the constant xlTextFormat.<\/p>\n<p>And that&rsquo;s all we have to do. What&rsquo;s going to happen when you run this script? Give it a try and see for yourself.<\/p>\n<p>Admittedly, that&rsquo;s a pretty simple case; after all, our text file only has one field. But what if our CSV file has <i>multiple<\/i> fields? For example, what if our file looks like this:<\/p>\n<pre class=\"codeSample\"><pre class=\"codeSample\">1-2,A,5-6\n2-3,B,7-8\n3-4,C,9-10\n4-3,D,11-12<\/pre>\n<p>In that case you need to do two things. First, you need to set the OpenText method&rsquo;s ninth parameter to True; that tells Excel that we are using the comma as our field delimiter. (Again, see the <a target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa195814(office.11).aspx\"><b>VBA Language Reference<\/b><\/a> for more information.) In addition, we need to include information for <i>all<\/i> the fields in our call to OpenText. Notice how our revised script includes mini-arrays for fields 1, 2, <i>and<\/i> 3:<\/p>\n<pre class=\"codeSample\">Const xlTextFormat = 2\n\nSet objExcel = CreateObject(\"Excel.Application\")\nobjExcel.Visible = True\n\nobjExcel.Workbooks.OpenText \"C:\\Scripts\\Test.txt\",,,,,,,,True,,,, _\n    Array(Array(1, xlTextFormat), Array(2, xlTextFormat), Array(3, xlTextFormat))<\/pre>\n<p>Give <i>that<\/i> a try and see what happens.<\/p>\n<p>That should do it, JR; if it doesn&rsquo;t please let us know. In the meantime, the Scripting Guys have done a little investigating and discovered that the folks at Yelo might be on to something: there is research that suggests that people who take naps 3 times a week live longer, healthier lives. As a matter of fact, a number of companies are now making nap rooms available to their employees. Does that include Microsoft? Well, the Scripting Guys <i>have<\/i> been sleeping on the job for years now. But, again, we don&rsquo;t think that was the intention.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a CSV file, and every time I open that file in Office Excel my numbers get converted to dates. How can I import numbers as numbers? &#8212; JR Hey, JR. You know, just yesterday the Scripting Guy who writes this column learned about Yelo, an interesting little company based in [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[169,711,48,49,3,4,5],"class_list":["post-55253","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-csv-and-other-delimited-files","tag-excel-application","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a CSV file, and every time I open that file in Office Excel my numbers get converted to dates. How can I import numbers as numbers? &#8212; JR Hey, JR. You know, just yesterday the Scripting Guy who writes this column learned about Yelo, an interesting little company based in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=55253"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55253\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=55253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}