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\nThe 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\nThis is what our code looks like in the module:<\/p>\n\n\n\n <\/figure>\n\n\n\nFor 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\nThe 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\nFrom these options in the picture above, we will choose msoFileDialogFilePicker<\/strong>.<\/p>\n\n\n\nApplication.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\nThen 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\nWe 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\nFor 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\nIf Len(file_name) > 0 Then\n Workbooks.Open file_name\n Set wb = ActiveWorkbook\nEnd If<\/code><\/pre>\n\n\n\nTo 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\nWe 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\nIn 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\nNow, when we click on this button, we will have a file picker:<\/strong><\/p>\n\n\n\n <\/figure>\n\n\n\nWe 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":"\n
How to Open CSV File in VBA<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n