Visual Basic - Import selected rows and columns from Excel

0

Is there a way to add more than one range of data from Excel to Visual Basic? I have managed to import the cells A6 to B260 into a datagrid, but I also want the columns E6-E260 and G6-G260. (selectedNOD is the selected spreadsheet)

    Dim selectedNOD As String
    selectedNOD = ListNOD.SelectedItem
    Try
        Dim MyConnection As System.Data.OleDb.OleDbConnection
        Dim dataSet As System.Data.DataSet
        Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
        Dim path As String = "NOD.xlsx"

        MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;")
        MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [" + selectedNOD + "$A6:B260]", MyConnection)

        dataSet = New System.Data.DataSet
        MyCommand.Fill(dataSet)
        DataGridView1.DataSource = dataSet.Tables(0)

        MyConnection.Close()
    Catch ex As Exception
        MsgBox(ex.Message.ToString)
    End Try

MJK

Posted 2016-10-01T17:18:15.720

Reputation: 31

Answers

0

This code solved my issue:

Me.dataGridView1.Columns("CustomerID").Visible = False

MJK

Posted 2016-10-01T17:18:15.720

Reputation: 31