{"id":55783,"date":"2008-04-15T01:07:00","date_gmt":"2008-04-15T01:07:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/04\/15\/hey-scripting-guy-how-can-i-import-a-fixed-width-data-file-into-microsoft-excel\/"},"modified":"2008-04-15T01:07:00","modified_gmt":"2008-04-15T01:07:00","slug":"hey-scripting-guy-how-can-i-import-a-fixed-width-data-file-into-microsoft-excel","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-import-a-fixed-width-data-file-into-microsoft-excel\/","title":{"rendered":"Hey, Scripting Guy! How Can I Import a Fixed-Width Data File into Microsoft Excel?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/p>\n<p>Hey, Scripting Guy! How can I import a fixed-width data file into Microsoft Excel?<br \/>&#8212; RS<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\" \/><\/a> <\/p>\n<p>Hey, RS. This column is being written on a Friday morning, and so far it\u2019s been a very weird morning at that. For one thing, it\u2019s not raining and \u2013 if you look closely \u2013 you can even see the sun. (It\u2019s been awhile, but we\u2019d recognize the sun anywhere.) Even weirder, the temperature is supposed to climb up into the 60s by this afternoon, and could even hit 70 degrees Fahrenheit on Saturday. 60-degree weather in April? In <i>Seattle<\/i>? We must be dreaming.<\/p>\n<table class=\"dataTable\" id=\"E5C\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. Actually, 60-degree weather in April isn\u2019t all that unusual for Seattle. Or at least it didn\u2019t used to be: on April 11, 2004, for example, the temperature actually reached <i>80<\/i> degrees. On the other hand, in 1911 the high temperature for April 11<sup>th<\/sup> was a mere 41 degrees. Although the Scripting Editor insists that it felt much warmer than that.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>We\u2019d give you more details on the current weather conditions if it weren\u2019t for the fact that the Scripting Guy who writes this column doesn\u2019t have a window office; that means that he can only keep tabs on the weather by getting up to go to the bathroom and then glancing out someone else\u2019s window. Admittedly, this would usually be the point where the Scripting Guy who writes this column would make some smart remark about window offices. He\u2019s not going to do that, however; needless to say, window offices are nothing to joke about at Microsoft. People will kill\u2013 and for all we know people <i>have<\/i> killed \u2013 just so they could get an office with a window.<\/p>\n<p>Three interesting notes about window offices:<\/p>\n<table class=\"numberedList\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<tbody>\n<tr vAlign=\"top\">\n<td class=\"listNumber\" noWrap align=\"right\">\n<p>1.<\/p>\n<\/td>\n<td class=\"\">\n<p>Getting a window office is like being crowned king. When you become king, you become king for life; when you get a window office you get a window office for life. No one ever has a window office taken away from them, ever. Well, no one except the Scripting Guy who writes this column, who had a window office for several months before they decided that office should go to someone who actually has some value around the company.<\/p>\n<\/td>\n<\/tr>\n<tr vAlign=\"top\">\n<td class=\"listNumber\" noWrap align=\"right\">\n<p>2.<\/p>\n<\/td>\n<td class=\"\">\n<p>People will kill \u2013 and for all we know people have killed \u2013 just so they could get an office with a window. Once they do get a window office approximately 87% of those people keep the blinds pulled at all times because they don\u2019t like the glare.<\/p>\n<\/td>\n<\/tr>\n<tr vAlign=\"top\">\n<td class=\"listNumber\" noWrap align=\"right\">\n<p>3.<\/p>\n<\/td>\n<td class=\"\">\n<p>At the risk of sounding like a sore loser, the Scripting Guy who writes this column believes that window offices are only given to <a href=\"http:\/\/dictionary.reference.com\/browse\/brown-nose\" target=\"_blank\"><b>brown-nosers, toadies, and sycophants<\/b><\/a>. Does the Scripting Editor have a window office? Take a wild guess.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>On the bright side, not having a window to stare out of <i>does<\/i> give the Scripting Guy who writes this column plenty of time to write this column. With that in mind, let\u2019s take a look at the kind of fixed-width text file that RS would like to open in Excel:<\/p>\n<pre class=\"codeSample\">Region                                             Subnet                                             Country\n-------------------------------------------------- -------------------------------------------------- --------------\nAMIS                                               192.168.0.0                                         American Samoa\nAMIS                                               192.168.1.0                                         American Samoa\nAMIS                                               192.168.10.0                                        American Samoa\nAMIS                                               192.168.11.0                                        American Samoa\nAMIS                                               192.168.12.0                                        American Samoa\nAMIS                                               192.168.13.0                                        American Samoa\nAMIS                                               192.168.14.0                                        American Samoa\nAMIS                                               192.168.15.0                                        American Samoa\nAMIS                                               192.168.2.0                                         American Samoa\nAMIS                                               192.168.3.0                                         American Samoa\n<\/pre>\n<p>How can we open a file like that in Excel? Well, here\u2019s one way:<\/p>\n<pre class=\"codeSample\">Const xlFixedWidth = 2\nConst xlTextFormat = 2\n\nSet objExcel = CreateObject(\"Excel.Application\")\nobjExcel.Visible = True\n\nobjExcel.Workbooks.OpenText \"C:\\Scripts\\Test.txt\",,3,xlFixedWidth,,,,,,,,, _\n    Array(Array(0, xlTextFormat),Array(51, xlTextFormat),Array(102, xlTextFormat))\n<\/pre>\n<p>Yes, we know: it <i>is<\/i> hard to believe that this script will correctly open a fixed-width file, isn\u2019t it? But maybe things will clear up when we explain how it works.<\/p>\n<p>No, check that: things will <i>definitely<\/i> clear up when we explain how it works.<\/p>\n<p>Maybe.<\/p>\n<p>As you can see, we start out by defining a pair of constants, setting the value of each of these constants to 2. The constant xlFixedWidth tells the script that we\u2019re going to open a fixed-width text file; alternatively, we could create a constant named xlDelimited (with a value of 1) and use that to open a delimited text file (e.g., a comma- or tab-separated values file). The constant xlTextFormat tells the script the data type for each of the fields in our text file. Because all three of our fields are text fields we need to define \u2013 and use \u2013 only this one constant. However, there are other data types and there are other constants you might need to use. For more information, look for the xlColumnDataType enumeration in the <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/aa221100(office.11).aspx\" target=\"_blank\"><b>Microsoft Excel VBA Language Reference<\/b><\/a>.<\/p>\n<p>After we define the constants we create an instance of the <b>Excel.Application<\/b> object and then set the <b>Visible<\/b> property to True; that gives us a running instance of Microsoft Excel that we can see on screen. And that \u2013 cue the ominous music \u2013 brings us to the following line of code:<\/p>\n<pre class=\"codeSample\">objExcel.Workbooks.OpenText \"C:\\Scripts\\Test.txt\",,3,xlFixedWidth,,,,,,,,, _\n    Array(Array(0, xlTextFormat),Array(51, xlTextFormat),Array(102, xlTextFormat))\n<\/pre>\n<p>So is that the craziest line of code that the Scripting Guy who writes this column has ever written? No, but it\u2019s probably in the top 10. As you can see, we\u2019re using the <b>OpenText<\/b> method (a method that belongs to the <b>Workbooks<\/b> collection) to open the file C:\\Scripts\\Test.txt. As for all the other craziness crammed into that one line of code, well, any time you see a blank parameter (that is, a comma followed by absolutely nothing) that means that this is a parameter we don\u2019t need in order to open our text file. For example, you might note that one of those blank parameters comes immediately after the file path:<\/p>\n<pre class=\"codeSample\">\"C:\\Scripts\\Test.txt\",,\n<\/pre>\n<p>As it turns out, this second, optional parameter lets you specify the \u201corigin\u201d of the text file; that is, whether this file came from a Windows computer, an MS-DOS computer, or a Macintosh. (Oh, OK: a Macintosh <i>computer<\/i>.) Because the default is to assume that the file came from a Windows machine, we can simply leave this parameter blank. Just remember, though, that the parameter must be included even though it has no value. You might think that you could take a shortcut by eliminating all the blank parameters:<\/p>\n<pre class=\"codeSample\">objExcel.Workbooks.OpenText \"C:\\Scripts\\Test.txt\",3,xlFixedWidth, _\n    Array(Array(0, xlTextFormat),Array(51, xlTextFormat),Array(102, xlTextFormat))\n<\/pre>\n<p>That\u2019s actually a pretty handy little shortcut \u2026 unless you want your script to work. If you want the script to work then leave in all the blank parameters. When you call the OpenText method the numerical position of the parameters is of paramount importance. The constant xlFixedWidth must be the fourth parameter in the method call. If xlFixedWidth appears anywhere else your script is likely to blow up.<\/p>\n<p>And yes, we mean that literally: last year, in the state of Washington alone, nearly 300 people were horribly disfigured when their Excel scripts blew up on them.<\/p>\n<table class=\"dataTable\" id=\"EZF\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. Well, OK, so maybe that <i>is<\/i> a complete and total fabrication. We just thought people might listen to us if we scared them a little.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>As for the other parameters, the <b>3<\/b> tells the script that we want to start with line 3 in the text file; that enables us to skip over the two header lines. If you don\u2019t <i>want<\/i> to skip over the two headers lines then leave this parameter blank.<\/p>\n<table class=\"dataTable\" id=\"EPG\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. Can you tell the script to import line 1 \u2013 the actual header line \u2013 skip line 2, and then start importing again on line 3? No. That\u2019d be cool, but you can\u2019t do it. If you want to get rid of all those dashes you\u2019ll need to open the script and replace line 2 with nothing. We won\u2019t bother explaining how that script might work, at least not today. However, <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/oct04\/hey1018.mspx\"><b>this column<\/b><\/a> should give you a hint or two.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>You already know what the constant xlFixedWidth is for; that tells the script that we\u2019re working with a fixed-width file. That leaves us with this:<\/p>\n<pre class=\"codeSample\">Array(Array(0, xlTextFormat),Array(51, xlTextFormat),Array(102, xlTextFormat))\n<\/pre>\n<p>Believe it or not, this is where we define each field in the text file. To begin with, because we can (and usually will) have multiple fields in a text file all this information must be passed as an array; that\u2019s why we end up with a bunch of little arrays embedded inside one big, giant array. As for those little arrays, let\u2019s take a look at the code that defines the first field in the text file:<\/p>\n<pre class=\"codeSample\">Array(0, xlTextFormat)\n<\/pre>\n<p>As you can see, we\u2019re creating an array that contains two elements. The first item, the 0, represents the starting character position for this field. (The first character position in a file is position 0; that\u2019s why we have a 0 here rather than a 1.) The second item is the constant xlTextFormat, which defines the data type for this field.<\/p>\n<p>Now, what about field 2? Well, the second field begins at character 51; hence we use 51 for the first item in the array, and the constant xlTextFormat as the second item in the array:<\/p>\n<pre class=\"codeSample\">Array(51, xlTextFormat)\n<\/pre>\n<p>Etc., etc.<\/p>\n<p>So will that actually import a fixed-width text file into Excel? Well, it did for us; give it a try and see what happens for you.<\/p>\n<p>In the meantime, we\u2019re sure that many of you are thinking, \u201cYou know, the Scripting Guy who writes that column is always complaining about how bad the weather is. I wonder when the last time was that they had a nice day in Seattle?\u201d Funny you should ask that: we looked it up, and the last time we actually had a nice day in Seattle was on June 19, 1937. Which was nice; that meant the Scripting Editor had nice weather for her 16<sup>th<\/sup> birthday party. <i>(<\/i><i>Ed<\/i><i>itor\u2019s Note: Well, that\u2019s pretty close, with the exception of the month, day, and year.<\/i><i> It is true, however, that I had a 16<\/i><sup>th<\/sup><i> birthday.<\/i><i>)<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I import a fixed-width data file into Microsoft Excel?&#8212; RS Hey, RS. This column is being written on a Friday morning, and so far it\u2019s been a very weird morning at that. For one thing, it\u2019s not raining and \u2013 if you look closely \u2013 you can even see the [&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":[18,711,48,49,3,4,14,5],"class_list":["post-55783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-arrays-hash-tables-and-dictionary-objects","tag-excel-application","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I import a fixed-width data file into Microsoft Excel?&#8212; RS Hey, RS. This column is being written on a Friday morning, and so far it\u2019s been a very weird morning at that. For one thing, it\u2019s not raining and \u2013 if you look closely \u2013 you can even see the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55783","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=55783"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55783\/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=55783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}