{"id":70183,"date":"2005-03-22T17:06:00","date_gmt":"2005-03-22T17:06:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/03\/22\/how-can-i-save-a-single-excel-worksheet-to-a-csv-file\/"},"modified":"2005-03-22T17:06:00","modified_gmt":"2005-03-22T17:06:00","slug":"how-can-i-save-a-single-excel-worksheet-to-a-csv-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-save-a-single-excel-worksheet-to-a-csv-file\/","title":{"rendered":"How Can I Save a Single Excel Worksheet to a CSV File?"},"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&rsquo;d like to be able to open an Excel spreadsheet, get all the information off one of the worksheets, and then save that worksheet data to a comma-separated values file. How can I do that?<\/p>\n<p>&#8212; SS<\/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, SS. You know, you just have to love Excel. Yes, we know, to be &ldquo;cool&rdquo; these days you&rsquo;re supposed to say that you don&rsquo;t use Excel, that you use one of those Office wannabes instead. But we&rsquo;d like to see one of those Office wannabes carry out a task like this, and in just a few lines of code to boot. <\/p>\n<p>Now, to be honest, when we first read the question we weren&rsquo;t sure how easy this was going to be. Turns out it was this easy:<\/p>\n<pre class=\"codeSample\">Const xlCSV = 6\n\nSet objExcel = CreateObject(\"Excel.Application\")\nSet objWorkbook = objExcel.Workbooks.Open(\"C:\\Scripts\\Testsheet.xls\")\nobjExcel.DisplayAlerts = FALSE\nobjExcel.Visible = TRUE\n\nSet objWorksheet = objWorkbook.Worksheets(\"Sheet1\")\nobjWorksheet.SaveAs \"c:\\scripts\\test.csv\", xlCSV\n\nobjExcel.Quit\n<\/pre>\n<p>That&rsquo;s it; that&rsquo;s the whole script right there. We begin by creating a constant named xlCSV and setting the value to 6; we&rsquo;ll use this constant later on to specify the file format for our new file. We then create an instance of the <b>Excel.Application<\/b> object and use the <b>Workbooks.Open<\/b> method to open the spreadsheet C:\\Scripts\\Testsheet.xls. (We also set the <b>Visible<\/b> property to TRUE, but that&rsquo;s just so you can see what&rsquo;s going on. If you&rsquo;d prefer to have Excel run in an invisible window, then just leave this line out.)<\/p>\n<p>Oh, and we have one other line of code that might be of interest:<\/p>\n<pre class=\"codeSample\">objExcel.DisplayAlerts = FALSE\n<\/pre>\n<p>This line of code tells Excel not to display any message boxes, but to instead go with the default value any time it has a question. Why do we bother with this? Well, we&rsquo;re going to save worksheet information to a CSV (comma-separated values) file. If that file already exists Excel will pop up a message box asking if we want to overwrite the existing file. For this script we&rsquo;re assuming that we <i>always<\/i> want to overwrite the existing file. By setting the <b>DisplayAlerts<\/b> property to FALSE we suppress the display of that message box and tell Excel to go ahead and carry out the default operation. In this case, the default operation just happens to be, &ldquo;Go ahead and overwrite any existing file.&rdquo;<\/p>\n<p>After we have Excel up and running it takes just two lines of code to save worksheet data to a CSV file. First, we bind to the desired worksheet (in this case, the worksheet named &ldquo;Sheet1&rdquo;):<\/p>\n<pre class=\"codeSample\">Set objWorksheet = objWorkbook.Worksheets(\"Sheet1\")\n<\/pre>\n<p>After that we simply call the <b>SaveAs<\/b> method, passing two parameters: the path to CSV file (C:\\Scripts\\Test.csv) and the format for the new file. Because we want this saved as a CSV file, we use the xlCSV constant we defined way back when. Thus:<\/p>\n<pre class=\"codeSample\">objWorksheet.SaveAs \"c:\\scripts\\test.csv\", xlCSV\n<\/pre>\n<p>All we have to do now is call the <b>Quit<\/b> method to terminate our Excel instance and we&rsquo;re done. <i>Now<\/i> try and tell us that one of those Excel wannabes is as good as the real thing!<\/p>\n<p style=\"text-align: left\"><b>Shameless Self-Promotion:<\/b> If you&rsquo;re interested in scripting Microsoft Office applications, be sure and check out the&nbsp;<a href=\"http:\/\/technet.microsoft.com\/en-us\/scriptcenter\/dd742371\">Learn about Scripting for Microsoft Office<\/a>. &nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I&rsquo;d like to be able to open an Excel spreadsheet, get all the information off one of the worksheets, and then save that worksheet data to a comma-separated values file. How can I do that? &#8212; SS Hey, SS. You know, you just have to love Excel. Yes, we know, to be [&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":[48,49,3,5],"class_list":["post-70183","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I&rsquo;d like to be able to open an Excel spreadsheet, get all the information off one of the worksheets, and then save that worksheet data to a comma-separated values file. How can I do that? &#8212; SS Hey, SS. You know, you just have to love Excel. Yes, we know, to be [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70183","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=70183"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70183\/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=70183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}