Asked in
Microsoft Excel
Visual Basic Programming

How do you import data from Excel into a chart on Visual Basic 2017?

Answer

User Avatar
Wiki User
June 23, 2017 6:35AM

First of all, Include Excel object library in your project via Proect-->Add Reference

Then you need to import the excel object like this

Imports Microsoft.Office.Interop

And Then you need to code like this

Dim xlApp As New Excel.Application Dim xlWB As Excel.Workbook Dim xlWS As Excel.Worksheet 'xlApp.Visible = True xlWB = xlApp.Workbooks.Open("Your_Excel_File_Path_And_Name.xlsx") xlWS = xlWB.Worksheets("Sheet2") ' To get data from excel Dim Sample(1) As String Sample(0) = xlWS.Cells(4, 1).Value ' 4th row 1st col Sample(1) = xlWS.Cells(4, 2).Value ' 4th row 2nd col MsgBox(Sample(0) & "--" & Sample(1)) ' To write data to excel Dim Sample(1) As String Sample(0) = "Apple" Sample(1) = "Orange" xlWS.Cells(25, 1).Value = Sample(0) '25th row 1st col xlWS.Cells(25, 2).Value = Sample(1)