I am working on a rainfall regression analysis for 30 Years in an excel file. I have been able to link to the excel file from vb6.0 and now my problem is how do I use the data in the excel file to plot a graph in another sheet inside the same excel file. Below is the code. I have a combobox named cmdLocation which is used to select the state among 3 states of data in the excel file and a command button to be able to read the file named cmdRead
- Private Sub cmdRead_Click()
- Dim excel_app As Excel.Application
- Dim workbook As Excel.workbook
- Dim sheet As Excel.Worksheet
- Dim sheet2 As Excel.Chart
- Dim StartCell As Integer
- Dim XValues(30), YValues(30) As Double
- Dim d As Integer
- Dim Reg As New RegressionObject
- Const R = 100
- Dim P(1 To 200) As PointType
- Dim X As Double
- Dim Y As Double
- Dim LocationValue As String
- LocationValue = cmbLocation.Text
- If LocationValue = "" Then
- MsgBox "Please select location"
- cmbLocation.SetFocus
- Else
- ' Get the Excel application object.
- Set excel_app = New Excel.Application
- ' Make Excel visible (optional).
- excel_app.Visible = True
- ' Open the workbook read-only.
- Set workbook = excel_app.Workbooks.Open( _
- FileName:=App.Path & "\RegressionData.xls", ReadOnly:=True)
- ' Get the first worksheet.
- Set sheet = workbook.Sheets("Sheet2")
- Set sheet2 = workbook.Sheets("Chart1")
- If LocationValue = "Lagos" Then
- StartCell = 2
- ElseIf LocationValue = "PHarcourt" Then
- StartCell = 33
- ElseIf LocationValue = "Ilorin" Then
- StartCell = 76
- End If
- For d = 1 To 30
- XValues(d) = Val(sheet.Cells(d + StartCell, 2).Value)
- YValues(d) = Val(sheet.Cells(d + StartCell, 14).Value) * 100
- Next d
- For d = 1 To 30
- sheet.Cells(d + 2, 100).Value = XValues(d)
- sheet.Cells(d + 2, 101).Value = YValues(d)
- Next d
- 'MsgBox LocationValue
- sheet.Cells(3, 102).Value = LocationValue
- sheet2.Select
- ' Save the changes and close the workbook.
- 'workbook.Close SaveChanges:=True
- ' Close the Excel server.
- ' excel_app.Quit
- MsgBox "Done"
- End If
- 143 views