excel - Error while reading CSV with VBA -


i'm trying read csv vba. when following this tutorial, following code:

sub opentextfile()  dim filepath string filepath = "c:\path\to\file\mycsv.csv" open filepath input #1 row_number = 0  until eof(1)     line input #1, linefromfile     lineitems = split(linefromline, ",")      activecell.offset(row_number, 0).value = lineitems(2)     activecell.offset(row_number, 1).value = lineitems(1)     activecell.offset(row_number, 2).value = lineitems(0)      row_number = row_number + 1 loop  close #1  end sub 

this csv:

peter,paris,23 mary,london,34 steve,rome,56 lily,madrid,65 

when executing code, error:

index out of range

and line marked yellow:

activecell.offset(row_number, 0).value = lineitems(2) 

you have typo:

lineitems = split(linefromline, ",") 

should

lineitems = split(linefromfile, ",") 

this not have happened if used option explicit @ beginning of module ;)


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -