{"id":11766,"date":"2022-09-20T09:46:21","date_gmt":"2022-09-20T09:46:21","guid":{"rendered":"https:\/\/officetuts.net\/excel\/?p=11766"},"modified":"2024-03-30T00:19:18","modified_gmt":"2024-03-30T00:19:18","slug":"save-excel-worksheet-as-csv-with-vba","status":"publish","type":"post","link":"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/","title":{"rendered":"Save Excel Worksheet as CSV with VBA"},"content":{"rendered":"\n

Visual Basic for Applications (VBA)<\/strong> is a great tool for the automatization of our work. It can also be used to manipulate any Office application (mostly Excel)<\/strong> on any level (sheet, workbook, or application as a whole).<\/p>\n\n\n\n

In the example below, we will show how can we manipulate the worksheet from the VBA by saving the worksheet in a specific file type – a CSV file<\/strong>.<\/p>\n\n\n\n

Save Excel Worksheet as CSV with VBA<\/h2>\n\n\n\n

For our example, we will use the table with NBA players and their respective teams:<\/p>\n\n\n\n

\"Graphical<\/figure>\n\n\n\n

Purposefully, we have started with row number 3<\/strong>. To open the VBA, we will click ALT + F11<\/strong>, then right-click in the left module and choose Insert >> Module<\/strong>:<\/p>\n\n\n\n

\"Graphical<\/figure>\n\n\n\n

In the window that appears on the right side, we will insert the following code:<\/p>\n\n\n\n

Sub SaveSheetAsCSV()\n    Dim ws          As Worksheet\n    Dim csvFile     As String\n    Set ws = ActiveSheet\n    ws.Copy\n    csvFile = CurDir & \"\\\" & ws.Name & \".csv\"\n    Application.ActiveWorkbook.SaveAs Filename:=csvFile, _\n                                      FileFormat:=xlCSV, CreateBackup:=False\n    Application.ActiveWorkbook.Saved = TRUE\n    Application.ActiveWorkbook.Close\nEnd Sub<\/code><\/pre>\n\n\n\n

This is what a code looks like in the VBA:<\/p>\n\n\n\n

\"Graphical<\/figure>\n\n\n\n

This code first declares two variables: ws as Worksheet and csvFile as String<\/strong>.<\/p>\n\n\n\n

Then we set ws to be equal to the active sheet (the one we are currently on). Of course, we can set this sheet to be anything we like.<\/p>\n\n\n\n

The next step:<\/p>\n\n\n\n

csvFile = CurDir & \"\\\" & ws.Name & \".csv\"\nApplication.ActiveWorkbook.SaveAs Filename:=csvFile, _\nFileFormat:=xlCSV, CreateBackup:=False<\/code><\/pre>\n\n\n\n

Saves our file (our worksheet) in the current directory<\/strong> (it is set to be in MyDocuments<\/strong> for all users), and then it saves it under the name that will be equal to the name of our sheet (\u201eNBA players\u201c<\/strong> in this case), and it saves is in CSV file format<\/strong>.<\/p>\n\n\n\n

The code finally saves the workbook (copied one), and then it closes it. The important step here is to copy our Worksheet<\/strong> into the new workbook and to have the original file opened as well.<\/p>\n\n\n\n

When we execute the code by pressing F5<\/strong> on our keyboard, we will only see something happening in the background.<\/p>\n\n\n\n

Now we need to go to our CurDir folder<\/strong>– it is set to be C:\\Users\\[username]\\Documents folder<\/strong>.<\/p>\n\n\n\n

In there, we will be able to see the CSV file<\/strong> created:<\/p>\n\n\n\n

\"Application\n\nDescription<\/figure>\n\n\n\n

When we open the file, you will notice that the list of NBA players in the CSV file<\/strong> starts from the first row:<\/p>\n\n\n\n

\"Table\n\nDescription<\/figure>\n","protected":false},"excerpt":{"rendered":"

Visual Basic for Applications (VBA) is a great tool for the automatization of our work. It can also be used to manipulate any…<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[190],"yoast_head":"\nSave Excel Worksheet as CSV with VBA<\/title>\n<meta name=\"description\" content=\"Learn how to use Visual Basic for Applications (VBA) to automate your work and manipulate any Office application (mostly Excel) with this tutorial on saving an Excel worksheet as a CSV file.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Save Excel Worksheet as CSV with VBA\" \/>\n<meta property=\"og:description\" content=\"Learn how to use Visual Basic for Applications (VBA) to automate your work and manipulate any Office application (mostly Excel) with this tutorial on saving an Excel worksheet as a CSV file.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-20T09:46:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-30T00:19:18+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/09\/graphical-user-interface-table-description-autom.png\" \/>\n<meta name=\"author\" content=\"Harun Spahic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Harun Spahic\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/\"},\"author\":{\"name\":\"Harun Spahic\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2\"},\"headline\":\"Save Excel Worksheet as CSV with VBA\",\"datePublished\":\"2022-09-20T09:46:21+00:00\",\"dateModified\":\"2024-03-30T00:19:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/\"},\"wordCount\":352,\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/09\/graphical-user-interface-table-description-autom.png\",\"keywords\":[\"pinterest\"],\"articleSection\":[\"vba\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/\",\"url\":\"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/\",\"name\":\"Save Excel Worksheet as CSV with VBA\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/09\/graphical-user-interface-table-description-autom.png\",\"datePublished\":\"2022-09-20T09:46:21+00:00\",\"dateModified\":\"2024-03-30T00:19:18+00:00\",\"description\":\"Learn how to use Visual Basic for Applications (VBA) to automate your work and manipulate any Office application (mostly Excel) with this tutorial on saving an Excel worksheet as a CSV file.\",\"breadcrumb\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/#primaryimage\",\"url\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/09\/graphical-user-interface-table-description-autom.png\",\"contentUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/09\/graphical-user-interface-table-description-autom.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/officetuts.net\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Save Excel Worksheet as CSV with VBA\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/officetuts.net\/excel\/#website\",\"url\":\"https:\/\/officetuts.net\/excel\/\",\"name\":\"\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/officetuts.net\/excel\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\",\"name\":\"Tomasz Decker\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"caption\":\"Tomasz Decker\"},\"logo\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\"},\"description\":\"Spreadsheet and Python enthusiast.\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2\",\"name\":\"Harun Spahic\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g\",\"caption\":\"Harun Spahic\"},\"sameAs\":[\"http:\/\/human.bsru.ac.th\/\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Save Excel Worksheet as CSV with VBA","description":"Learn how to use Visual Basic for Applications (VBA) to automate your work and manipulate any Office application (mostly Excel) with this tutorial on saving an Excel worksheet as a CSV file.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/","og_locale":"en_US","og_type":"article","og_title":"Save Excel Worksheet as CSV with VBA","og_description":"Learn how to use Visual Basic for Applications (VBA) to automate your work and manipulate any Office application (mostly Excel) with this tutorial on saving an Excel worksheet as a CSV file.","og_url":"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/","article_published_time":"2022-09-20T09:46:21+00:00","article_modified_time":"2024-03-30T00:19:18+00:00","og_image":[{"url":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/09\/graphical-user-interface-table-description-autom.png"}],"author":"Harun Spahic","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Harun Spahic","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/#article","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/"},"author":{"name":"Harun Spahic","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2"},"headline":"Save Excel Worksheet as CSV with VBA","datePublished":"2022-09-20T09:46:21+00:00","dateModified":"2024-03-30T00:19:18+00:00","mainEntityOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/"},"wordCount":352,"publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/#primaryimage"},"thumbnailUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/09\/graphical-user-interface-table-description-autom.png","keywords":["pinterest"],"articleSection":["vba"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/","url":"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/","name":"Save Excel Worksheet as CSV with VBA","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/#website"},"primaryImageOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/#primaryimage"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/#primaryimage"},"thumbnailUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/09\/graphical-user-interface-table-description-autom.png","datePublished":"2022-09-20T09:46:21+00:00","dateModified":"2024-03-30T00:19:18+00:00","description":"Learn how to use Visual Basic for Applications (VBA) to automate your work and manipulate any Office application (mostly Excel) with this tutorial on saving an Excel worksheet as a CSV file.","breadcrumb":{"@id":"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/#primaryimage","url":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/09\/graphical-user-interface-table-description-autom.png","contentUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/09\/graphical-user-interface-table-description-autom.png"},{"@type":"BreadcrumbList","@id":"https:\/\/officetuts.net\/excel\/vba\/save-excel-worksheet-as-csv-with-vba\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/officetuts.net\/excel\/"},{"@type":"ListItem","position":2,"name":"Save Excel Worksheet as CSV with VBA"}]},{"@type":"WebSite","@id":"https:\/\/officetuts.net\/excel\/#website","url":"https:\/\/officetuts.net\/excel\/","name":"","description":"","publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/officetuts.net\/excel\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42","name":"Tomasz Decker","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","caption":"Tomasz Decker"},"logo":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/"},"description":"Spreadsheet and Python enthusiast."},{"@type":"Person","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2","name":"Harun Spahic","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g","caption":"Harun Spahic"},"sameAs":["http:\/\/human.bsru.ac.th\/"]}]}},"_links":{"self":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/11766"}],"collection":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/comments?post=11766"}],"version-history":[{"count":3,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/11766\/revisions"}],"predecessor-version":[{"id":12716,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/11766\/revisions\/12716"}],"wp:attachment":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/media?parent=11766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/categories?post=11766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/tags?post=11766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}