Hi There,
I have the following code to import a csv file into Excel:
This works fine, however I only want to import certain columns based on heading names which are always the same (Product, Product Code, etc) - the CSV could have these headings or not (should bomb out if not)
Any suggestions on how to import by column name? I've search this post, couldn't really find an answer
I have the following code to import a csv file into Excel:
Code:
Sub ImportCSV()
Dim vPath As Variant
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Set wb = Excel.ActiveWorkbook
Set ws = Excel.ActiveSheet
vPath = Application.GetOpenFilename("CSV (Comma Delimited) (*.csv),*.csv" _
, 1, "Select a file", , False)
''//Show the file open dialog to allow user to select a CSV file
If vPath = False Then Exit Sub
''//Exit macro if no file selected
Workbooks.OpenText Filename:=vPath, Origin:=xlMSDOS, StartRow:=1 _
, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, Comma:=True _
, FieldInfo:=Array(Array(1, xlTextFormat), Array(2, xlTextFormat), _
Array(3, xlTextFormat))
''//The fieldinfo array needs to be extended to match your number of columns
Columns.EntireColumn.AutoFit
''//Resize the columns
Sheets(1).Move After:=wb.Sheets(1)
''//Move the data into the Workbook
End Sub
This works fine, however I only want to import certain columns based on heading names which are always the same (Product, Product Code, etc) - the CSV could have these headings or not (should bomb out if not)
Any suggestions on how to import by column name? I've search this post, couldn't really find an answer