Variable columns or fields in a CSV file to be imported

Ricardo Caicedo

New Member
Joined
Aug 21, 2014
Messages
43
Hello Guys</SPAN>

I need to import several CSV files to specific fields in my sheet.</SPAN>

So far with the code is a working well, but just if the imported files have the same number of fields by rows (or columns).However the CSV files can change as sometimes has 6 columns sometimes just 3.</SPAN>

I feed the data in a variable name x, that contains the import fields.</SPAN>

If the files are different I got the error subscript 9 as the x variable got short and creates the fail.</SPAN>

See so far</SPAN>
Dim fName, x</SPAN></SPAN>
Open fName For Input As #1</SPAN></SPAN>
Do Until EOF(1)</SPAN></SPAN>
Line Input #1, Data</SPAN></SPAN>
x = Split(Data, ",")</SPAN></SPAN>
Cells(NextRow, "N").Value = x(2) 'Extract Daily </SPAN></SPAN>
Cells(NextRow, "O").Value = x(3) 'Extract Monthly </SPAN></SPAN>
Cells(NextRow, "J").Value = x(4) 'Extract Fi</SPAN></SPAN>
Cells(NextRow, "P").Value = x(5) 'Extract Bal </SPAN></SPAN>
Cells(NextRow, "Q").Value = x(6) 'Extract 5%</SPAN></SPAN>
Cells(NextRow, "R").Value = x(7) 'Extract 5%</SPAN></SPAN>
NextRow = NextRow + 1</SPAN></SPAN>

Loop</SPAN></SPAN>
Close #1</SPAN></SPAN>

How I can control x or knew the number of columns so I can fix the code?</SPAN></SPAN>

Thank you Guys very appreciate</SPAN></SPAN>
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Forum statistics

Threads
1,214,826
Messages
6,121,792
Members
449,048
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top