{"id":9412,"date":"2022-04-25T09:37:10","date_gmt":"2022-04-25T09:37:10","guid":{"rendered":"https:\/\/officetuts.net\/excel\/?p=9412"},"modified":"2024-03-29T23:56:43","modified_gmt":"2024-03-29T23:56:43","slug":"open-csv-file-in-vba","status":"publish","type":"post","link":"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/","title":{"rendered":"How to Open CSV File in VBA"},"content":{"rendered":"\n

Although it is not strictly and directly defined, Excel relates to all other Office programs and file types. This is best visible and achievable through VBA<\/strong>.<\/p>\n\n\n\n

In the example below, we will show how to open the CSV (comma separated values)<\/strong> file with VBA, and how to use the FileDialog option<\/strong> to choose a CSV file<\/strong> among all the files in the folder.<\/p>\n\n\n\n

Open CSV File with VBA<\/h2>\n\n\n\n

To simply open the CSV<\/strong><\/a> file through VBA<\/strong>, we first need to input the code in your module. To get there, we need to click ALT + F11<\/strong> and then right-click anywhere on the left side of the window that appears, go to Insert >> Module<\/strong>:<\/p>\n\n\n\n

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

When we insert a module, the following code needs to be inserted:<\/p>\n\n\n\n

Sub Open_CSV_File()\n    Workbooks.OpenText Filename:= _\n                       \"C:\\Users\\Harun\\Documents\\CSV file\\Book2.csv\", Local:=True\nEnd Sub<\/code><\/pre>\n\n\n\n

This Is how our code looks in the module:<\/p>\n\n\n\n

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

This is a simple code. To comprehend it, we need to realize that CSV files are text files whose values are separated by a comma, tab, space, or semicolon. We treat them as such in our code as well, so we start it with:<\/p>\n\n\n\n

Workbooks.OpenText Filename:=<\/code><\/pre>\n\n\n\n

Then we need to insert the location of the file itself (we can go to the file, right-click on it, go to Properties, and then copy the Location<\/strong>):<\/p>\n\n\n\n

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

Of course, on the location, we need to add the name of the file and its type (in our case .csv)<\/strong>. We add the option Local<\/strong> to save files against the language of Excel.<\/p>\n\n\n\n

If we go on and execute our code from the Module (by clicking F5 or the green button in this picture)<\/strong>:<\/p>\n\n\n\n

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

When we do this, our CSV will be opened with an Excel<\/strong><\/a> file:<\/p>\n\n\n\n

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

Open CSV File with File Dialog<\/h2>\n\n\n\n

There is also a great built-in function in VBA<\/strong> that can allow you to open the file picker and choose what file you want to open.<\/p>\n\n\n\n

The code to do this is as follows:<\/p>\n\n\n\n

Sub csv_FileDialog()\n    Dim wb          As Workbook\n    Dim file_name   As String\n    Dim FilterIndex As Integer\n    Dim filter      As String\n    Dim fd2         As FileDialog\n    Set fd2 = Application.FileDialog(msoFileDialogFilePicker)\n    With fd2\n        .AllowMultiSelect = FALSE\n        .Filters.Add \"Text Files\", \"*.csv\", 1\n        .FilterIndex = 1\n        .Filters.Add \"text files| \", \"*.csv\"\n        If .Show = -1 Then file_name = .SelectedItems(1)\n    End With\n    If Len(file_name) > 0 Then\n        Workbooks.Open file_name\n        Set wb = ActiveWorkbook\n    End If\nEnd Sub<\/code><\/pre>\n\n\n\n

This is what our code looks like in the module:<\/p>\n\n\n\n

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

For the first part of the code, we are simply declaring variables:<\/p>\n\n\n\n

Dim wb As Workbook\nDim file_name As String\nDim FilterIndex As Integer\nDim filter As String\nDim fd2 As FileDialog<\/code><\/pre>\n\n\n\n

The particularly important one is fd2<\/strong>, which is declared as a file dialog<\/strong>. When you start writing<\/p>\n\n\n\n

\u201eSet fd2 = Application.FileDialog(\u201c<\/strong> and then press CTRL + SPACE<\/strong> (which is a shortcut for IntelliSense- code editing features<\/strong>), you will see multiple options to choose from:<\/p>\n\n\n\n

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

From these options in the picture above, we will choose msoFileDialogFilePicker<\/strong>.<\/p>\n\n\n\n

Application.FileDialog <\/strong>gets us to the picker of files, and folders, or to open or save files. The same options that we have when we go to the File tab.<\/strong><\/p>\n\n\n\n

Then we declare the following things to do with the file that will be open:<\/p>\n\n\n\n

With fd2\n    .AllowMultiSelect = False\n    .Filters.Add \"Text Files\", \"*.csv\", 1\n    .FilterIndex = 1\n    .Filters.Add \"text files| \", \"*.csv\"\n    If .Show = -1 Then file_name = .SelectedItems(1)\nEnd With<\/code><\/pre>\n\n\n\n

We declare that we will not allow multiple files to be selected. Then we declare that the files that can be opened are the text file, i.e. CSV.<\/p>\n\n\n\n

For the last thing, we declare that if we find the file and select it in the folder, then it will be equal to the variable file_name<\/strong>.<\/p>\n\n\n\n

For the last part of the code, we use the IF function<\/strong> to define that, if the user has chosen the CSV file, for it to be open:<\/p>\n\n\n\n

If Len(file_name) > 0 Then\n    Workbooks.Open file_name\n    Set wb = ActiveWorkbook\nEnd If<\/code><\/pre>\n\n\n\n

To save our code in the worksheet for the user, we will get back to the Excel file, go to the Developer tab >> Controls >> Insert >> Button (which is the first option in Form Controls):<\/strong><\/p>\n\n\n\n

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

We will then drag and drop the button, and will be presented with the following window when we are done:<\/p>\n\n\n\n

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

In this window, we will choose the macro that will be connected to the button. We will choose csv_FileDialog<\/strong>, as this is the name of the macro we created for opening the CSV files. When we click OK<\/strong>, our button will be created. We will right-click on it to change its name to \u201cOpen CSV file\u201d:<\/strong><\/p>\n\n\n\n

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

Now, when we click on this button, we will have a file picker:<\/strong><\/p>\n\n\n\n

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

We can see that this file picker<\/strong> searches for Text Files (*.csv)<\/strong> just as we defined in our code. When we select the file and click Open<\/strong>, our CSV file will be opened.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"

Although it is not strictly and directly defined, Excel relates to all other Office programs and file types. This is best visible and…<\/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":"\nHow to Open CSV File in VBA<\/title>\n<meta name=\"description\" content=\"Learn how to open a CSV file with VBA in Excel and use the FileDialog option to choose CSV files among all other files in the folder. Follow our step-by-step guide now.\" \/>\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\/open-csv-file-in-vba\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Open CSV File in VBA\" \/>\n<meta property=\"og:description\" content=\"Learn how to open a CSV file with VBA in Excel and use the FileDialog option to choose CSV files among all other files in the folder. Follow our step-by-step guide now.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-04-25T09:37:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-29T23:56:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/04\/inserting-module.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=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/\"},\"author\":{\"name\":\"Harun Spahic\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2\"},\"headline\":\"How to Open CSV File in VBA\",\"datePublished\":\"2022-04-25T09:37:10+00:00\",\"dateModified\":\"2024-03-29T23:56:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/\"},\"wordCount\":691,\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/04\/inserting-module.png\",\"keywords\":[\"pinterest\"],\"articleSection\":[\"vba\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/\",\"url\":\"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/\",\"name\":\"How to Open CSV File in VBA\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/04\/inserting-module.png\",\"datePublished\":\"2022-04-25T09:37:10+00:00\",\"dateModified\":\"2024-03-29T23:56:43+00:00\",\"description\":\"Learn how to open a CSV file with VBA in Excel and use the FileDialog option to choose CSV files among all other files in the folder. Follow our step-by-step guide now.\",\"breadcrumb\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/#primaryimage\",\"url\":\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/04\/inserting-module.png\",\"contentUrl\":\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/04\/inserting-module.png\",\"width\":444,\"height\":469},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/officetuts.net\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Open CSV File in 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":"How to Open CSV File in VBA","description":"Learn how to open a CSV file with VBA in Excel and use the FileDialog option to choose CSV files among all other files in the folder. Follow our step-by-step guide now.","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\/open-csv-file-in-vba\/","og_locale":"en_US","og_type":"article","og_title":"How to Open CSV File in VBA","og_description":"Learn how to open a CSV file with VBA in Excel and use the FileDialog option to choose CSV files among all other files in the folder. Follow our step-by-step guide now.","og_url":"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/","article_published_time":"2022-04-25T09:37:10+00:00","article_modified_time":"2024-03-29T23:56:43+00:00","og_image":[{"url":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/04\/inserting-module.png"}],"author":"Harun Spahic","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Harun Spahic","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/#article","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/"},"author":{"name":"Harun Spahic","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2"},"headline":"How to Open CSV File in VBA","datePublished":"2022-04-25T09:37:10+00:00","dateModified":"2024-03-29T23:56:43+00:00","mainEntityOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/"},"wordCount":691,"publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/#primaryimage"},"thumbnailUrl":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/04\/inserting-module.png","keywords":["pinterest"],"articleSection":["vba"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/","url":"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/","name":"How to Open CSV File in VBA","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/#website"},"primaryImageOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/#primaryimage"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/#primaryimage"},"thumbnailUrl":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/04\/inserting-module.png","datePublished":"2022-04-25T09:37:10+00:00","dateModified":"2024-03-29T23:56:43+00:00","description":"Learn how to open a CSV file with VBA in Excel and use the FileDialog option to choose CSV files among all other files in the folder. Follow our step-by-step guide now.","breadcrumb":{"@id":"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/#primaryimage","url":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/04\/inserting-module.png","contentUrl":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/04\/inserting-module.png","width":444,"height":469},{"@type":"BreadcrumbList","@id":"https:\/\/officetuts.net\/excel\/vba\/open-csv-file-in-vba\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/officetuts.net\/excel\/"},{"@type":"ListItem","position":2,"name":"How to Open CSV File in 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\/9412"}],"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=9412"}],"version-history":[{"count":7,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/9412\/revisions"}],"predecessor-version":[{"id":13244,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/9412\/revisions\/13244"}],"wp:attachment":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/media?parent=9412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/categories?post=9412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/tags?post=9412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}