If Left(locLinesList(i)(j), 1) = parExcludeCharacter Then If IsArray(parArray) = False Then isArrayEmpty = True 'Returns False if not an array or a dynamic array Public Function isArrayEmpty(parArray As Variant) As Boolean Cells(1, 1).Resize(UBound(Data, 1), UBound(Data, 2)) = Data 'and the array values are inserted in one operation. 'A range gets the same dimensions as the array 'If you want to operate directly on the array, 'If the array isn't empty it is inserted into 'Function call - the file is read into the arrayĭata = getDataFromFile(parFileName, parDelimiter) ParDelimiter As String, parSheetName As String)ĭim Data As Variant 'Array for the file values Private Sub copyDataFromCsvFileToSheet(parFileName As String, _ 'and sheet name from the worksheet or an inputĬopyDataFromCsvFileToSheet sPath, " ", "Sheet2" 'Of course you could also read the separator SPath = ThisWorkbook.Path & "\csvtest.csv"
Import data from excel to excel vba how to#
'I show how to do that - just replace the 'file open dialogue that returns the name 'use Application.GetOpenFilename to get a 'you want something more flexible, you can 'Below we assume that the file, csvtest.csv, The file is imported correctly, and the recorded macro looks something like this (if you use a Danish Excel version): If you record a macro when opening the file, everything works fine. If you save a spreadsheet as a csv file and open the file with Notepad, you will see that the values are separated with semicolons (depending on your local settings).
Import data from excel to excel vba code#
If you use comma as decimal separator, using comma for separating values would make a mess.īelow are some examples on how to import csv files using VBA, and there is also an example on how to parse ("read") the file using code insted of using Excel's built-in import functions. The reason for this is fairly simple: "csv" stands for "comma separated values", and VBA "thinks the American way" and doesn't use the local settings (here: semicolon). It is easy to open a csv file in Excel, you just double-click the icon, and doing it with an Excel VBA macro is also straigthforward, unless it is a semicolon-delimited file. It could be from your internet bank or maybe some Google service like Analytics.Ĭsv-files are just text files, where the values are separated with a comma, semicolon, tab or space. It is now quite common that you can download data as csv files.