Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import csv file into MSChart

Status
Not open for further replies.

norason

Programmer
Jan 28, 2009
139
US
I am trying to import an Excel csv file into MSChart. The file is a single column (A) comprised of general cell data, which are all numbers, like this for 1250 points:
A
1 2
2 4
3 17
4 86
5 102
6 125
etc.

This code keeps saying iData is subscript out of range. What am I doing wrong?

Private Sub CmdOpenFile_Click()
With CommonDialog1
On Error Resume Next
.Flags = cdlOFNOverwritePrompt
.DialogTitle = "Open file..."
.Filter = "CSV Files (*.csv) | *.csv"
.ShowOpen
OpenName = .FileName
End With
Set tempfile = fso2.OpenTextFile(Drive1.Drive & "\OpenName", ForReading)
Debug.Print tempfile
Debug.Print OpenName
Dim iData() As String
Dim inum As Integer
inum = 0
Open OpenName For Input As #1
Debug.Print OpenName
Do While Not EOF(1)
ReDim Preserve iData(inum)
Input #1, iData(inum)
inum = inum + 1
Debug.Print iData(inum)
Loop
Close #1
With MSChart1
.chartType = VtChChartType2dLine
.RowCount = 1250
.ColumnCount = 2
.ChartData = Data
.TitleText = "GRAPH"
.ChartData = iData
With .DataGrid
.SetSize 1, 1, 1250, 1 '(rowLabelCount, columnLabelCount, dataRowCount, columnLabelCount)
Dim intIndex As Integer '*******************
MSChart1.RowCount = 1250
End With
With .Plot
With .Axis(VtChAxisIdY).ValueScale
.Auto = False
.Minimum = 0
.Maximum = 625
End With
With .Axis(VtChAxisIdX).AxisGrid
.MajorPen.VtColor.Set 204, 204, 204
.MajorPen.Width = 2
.MinorPen.VtColor.Set 204, 204, 204
.MinorPen.Width = 2
End With
With .Axis(VtChAxisIdY).AxisGrid
.MajorPen.VtColor.Set 204, 204, 204
.MajorPen.Width = 2
.MinorPen.VtColor.Set 204, 204, 204
.MinorPen.Width = 2
End With
With .SeriesCollection
.Item(1).LegendText = "Data"
With .Item(1).DataPoints(-1) 'controls text for flow line - grid too small - can't see the text
.Brush.FillColor.Set 255, 0, 0 '# red line
End With
End With '#seriescoll
End With '# plot
End With
End Sub


Thanks,

norason
 
Never mind, I finally found a reference that worked just fine. FYI:

Dim file_name As String
Dim fnum As Integer
Dim whole_file As String
Dim lines As Variant
Dim one_line As Variant
Dim num_rows As Long
Dim num_cols As Long
Dim the_array() As String
Dim R As Long
Dim C As Long

file_name = App.Path
If Right$(file_name, 1) <> "\" Then file_name = _
file_name & "\"
'OpenName
' file_name = file_name & "test.csv"
file_name = OpenName
' Load the file.
fnum = FreeFile
Open file_name For Input As fnum
whole_file = Input$(LOF(fnum), #fnum)
Close fnum

' Break the file into lines.
lines = Split(whole_file, vbCrLf)

' Dimension the array.
num_rows = UBound(lines)
one_line = Split(lines(0), ",")
num_cols = UBound(one_line)
ReDim the_array(num_rows, num_cols)

' Copy the data into the array.
For R = 0 To num_rows
one_line = Split(lines(R), ",")
For C = 0 To num_cols
the_array(R, C) = one_line(C)
Next C
Next R

' Prove we have the data loaded.
For R = 0 To num_rows
For C = 0 To num_cols
Debug.Print the_array(R, C) & "|";
Next C
Debug.Print
Next R
Debug.Print "======="
'End Sub

Sorry if I bothered anyone.

Norason
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top