Ricardo Caicedo
New Member
- Joined
- Aug 21, 2014
- Messages
- 43
Hello
I got a CSV file to be imported in my current sheet, but I need to import the data from the fifth row (5) and also some files I have different quatity of columns in the CSV file.</SPAN></SPAN>
The next code works perfectly, but it is importing from row one(1)(until the row “totals”) and sometimes I got the run-time error ‘9’ as the subscript is out of range, because the files can have variable columns.</SPAN></SPAN>
So I need to add in this code, an instruction to start to import from the row 5 and also to check the number of columns and extract all the info</SPAN></SPAN>
Anyone can help me </SPAN></SPAN>
Next is how the CSV File looks(if I check it in excel, the bold letters -i did it- are the columns):</SPAN>
-------------------------------------------------------------------------------------------------------------------------</SPAN>
" REPORT","Mkt Monthly 1 LML Grp","Reporting Period: February 2014","Mar 12, 2014 08:54:59","Revised","InfoRev5"</SPAN></SPAN>
,"Measured","Scheduled",,,"Calculated",</SPAN></SPAN>
"Day","TotalUsed","Schedule Daily","Schedule Monthly","Schedule Firm","Balance","UOR (1st 5%)"</SPAN></SPAN>
,,,,,,</SPAN></SPAN>
1,5653.8,4877.0,13.0,807.0,,</SPAN></SPAN>
2,6037.9,4877.0,13.0,807.0,,</SPAN></SPAN>
3,7884.7,4877.0,13.0,807.0,,</SPAN></SPAN>
4,8513.7,8000.0,13.0,807.0,,</SPAN></SPAN>
5,8929.0,8000.0,13.0,807.0,,109.0</SPAN></SPAN>
6,8522.6,8000.0,13.0,807.0,,</SPAN></SPAN>
7,7828.9,8000.0,13.0,807.0,,</SPAN></SPAN>
8,6535.8,8000.0,13.0,807.0,,</SPAN></SPAN>
24,7960.1,3180.0,13.0,807.0,,</SPAN></SPAN>
25,7327.0,3180.0,13.0,807.0,,</SPAN></SPAN>
26,6707.8,3180.0,13.0,807.0,,</SPAN></SPAN>
27,6333.3,3180.0,13.0,807.0,,</SPAN></SPAN>
28,5631.9,3180.0,13.0,807.0,12929.7,</SPAN></SPAN>
Totals,187544.7,150379.0,364.0,22596.0,12929.7,109.0</SPAN></SPAN>
And the code is
Sub ImportData()</SPAN>
Dim NextRow As Long</SPAN>
Dim Data As String, EndFile As String</SPAN>
Dim FileName, x</SPAN>
FileName = Application.GetOpenFilename( _</SPAN>
FileFilter:="Comma Separated Files (*.csv), *.csv", _</SPAN>
FilterIndex:=1, _</SPAN>
Title:="Select a File")</SPAN>
If FileName = False Then Exit Sub</SPAN>
Open FileName For Input As #1</SPAN>
NextRow = 1</SPAN>
EndFile = ""</SPAN>
Do Until EOF(1) Or EndFile = "Totals"</SPAN>
Line Input #1, Data</SPAN>
x = Split(Data, ",")</SPAN>
EndFile = x(0)</SPAN>
If EndFile = "Totals" Then</SPAN>
NextRow = NextRow + 1</SPAN>
Else</SPAN>
Cells(NextRow, "A").Value = x(2) 'Extract Daily Schedule</SPAN>
Cells(NextRow, "B").Value = x(3) 'Extract Monthly schedule</SPAN>
Cells(NextRow, "C").Value = x(4) 'Extract Firm</SPAN>
Cells(NextRow, "D").Value = x(5) 'Extract Balance</SPAN>
Cells(NextRow, "E").Value = x(6) 'Extract UOR 1st 5%</SPAN>
Cells(NextRow, "F").Value = x(7) 'Extract UOR 1st 5%</SPAN>
NextRow = NextRow + 1</SPAN>
End If</SPAN>
Loop</SPAN>
Close #1</SPAN>
End Sub</SPAN>
I got a CSV file to be imported in my current sheet, but I need to import the data from the fifth row (5) and also some files I have different quatity of columns in the CSV file.</SPAN></SPAN>
The next code works perfectly, but it is importing from row one(1)(until the row “totals”) and sometimes I got the run-time error ‘9’ as the subscript is out of range, because the files can have variable columns.</SPAN></SPAN>
So I need to add in this code, an instruction to start to import from the row 5 and also to check the number of columns and extract all the info</SPAN></SPAN>
Anyone can help me </SPAN></SPAN>
Next is how the CSV File looks(if I check it in excel, the bold letters -i did it- are the columns):</SPAN>
-------------------------------------------------------------------------------------------------------------------------</SPAN>
" REPORT","Mkt Monthly 1 LML Grp","Reporting Period: February 2014","Mar 12, 2014 08:54:59","Revised","InfoRev5"</SPAN></SPAN>
,"Measured","Scheduled",,,"Calculated",</SPAN></SPAN>
"Day","TotalUsed","Schedule Daily","Schedule Monthly","Schedule Firm","Balance","UOR (1st 5%)"</SPAN></SPAN>
,,,,,,</SPAN></SPAN>
1,5653.8,4877.0,13.0,807.0,,</SPAN></SPAN>
2,6037.9,4877.0,13.0,807.0,,</SPAN></SPAN>
3,7884.7,4877.0,13.0,807.0,,</SPAN></SPAN>
4,8513.7,8000.0,13.0,807.0,,</SPAN></SPAN>
5,8929.0,8000.0,13.0,807.0,,109.0</SPAN></SPAN>
6,8522.6,8000.0,13.0,807.0,,</SPAN></SPAN>
7,7828.9,8000.0,13.0,807.0,,</SPAN></SPAN>
8,6535.8,8000.0,13.0,807.0,,</SPAN></SPAN>
24,7960.1,3180.0,13.0,807.0,,</SPAN></SPAN>
25,7327.0,3180.0,13.0,807.0,,</SPAN></SPAN>
26,6707.8,3180.0,13.0,807.0,,</SPAN></SPAN>
27,6333.3,3180.0,13.0,807.0,,</SPAN></SPAN>
28,5631.9,3180.0,13.0,807.0,12929.7,</SPAN></SPAN>
Totals,187544.7,150379.0,364.0,22596.0,12929.7,109.0</SPAN></SPAN>
And the code is
Sub ImportData()</SPAN>
Dim NextRow As Long</SPAN>
Dim Data As String, EndFile As String</SPAN>
Dim FileName, x</SPAN>
FileName = Application.GetOpenFilename( _</SPAN>
FileFilter:="Comma Separated Files (*.csv), *.csv", _</SPAN>
FilterIndex:=1, _</SPAN>
Title:="Select a File")</SPAN>
If FileName = False Then Exit Sub</SPAN>
Open FileName For Input As #1</SPAN>
NextRow = 1</SPAN>
EndFile = ""</SPAN>
Do Until EOF(1) Or EndFile = "Totals"</SPAN>
Line Input #1, Data</SPAN>
x = Split(Data, ",")</SPAN>
EndFile = x(0)</SPAN>
If EndFile = "Totals" Then</SPAN>
NextRow = NextRow + 1</SPAN>
Else</SPAN>
Cells(NextRow, "A").Value = x(2) 'Extract Daily Schedule</SPAN>
Cells(NextRow, "B").Value = x(3) 'Extract Monthly schedule</SPAN>
Cells(NextRow, "C").Value = x(4) 'Extract Firm</SPAN>
Cells(NextRow, "D").Value = x(5) 'Extract Balance</SPAN>
Cells(NextRow, "E").Value = x(6) 'Extract UOR 1st 5%</SPAN>
Cells(NextRow, "F").Value = x(7) 'Extract UOR 1st 5%</SPAN>
NextRow = NextRow + 1</SPAN>
End If</SPAN>
Loop</SPAN>
Close #1</SPAN>
End Sub</SPAN>