Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88030

How to paste selected items from a list box in VB.net to excel sheet?

$
0
0

I have a list box populated from SQL database, I would like to export the selected items from the list box to an excel sheet. What I can't work out is how to past each selected item from listbox in different cells. I have the code to drop down a row each time but cant figure out how to put it together with the selected items block of code.

   Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim NewExcel As New Excel.Application
    NewExcel.Workbooks.Add()
    Dim counter As Integer = 0
    Dim SelectedItems = (From i In ListBox1.SelectedItems).ToList

    For Each selectedItem In SelectedItems
        NewExcel.Range("A1").Value = ListBox1.SelectedItem
    Next

    'Do
    '    NewExcel.ActiveCell.Value = counter
    '    NewExcel.ActiveCell.Offset(1, 0).Activate()
    '    counter += 1
    'Loop Until counter = 10

    NewExcel.Visible = True
End Sub

Viewing all articles
Browse latest Browse all 88030

Trending Articles