Save listbox into Excel

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Save listbox into Excel

  1. #1
    Join Date
    Aug 2004

    Save listbox into Excel

    [Originally posted by soda]

    I have been searching the forums on how to Save a listbox into a certain Column in Excel and i just cant seem to come up with any solutions. If you could please help me out, that would be great. I just need to make a listbox save as an excel file formatted into different columns from each different listbox. Thanks

  2. #2
    Join Date
    Aug 2004

    Re:Save listbox into Excel

    [Originally posted by Gabriel Medina]

    This is an example of what you need.

    Do this:

    Create a new project, add a Command Button (Command1) and a List Box (List1) and add the following code:

    Private Sub Command1_Click()
    Dim xl As Excel.Application
    Dim wb As Excel.Workbook
    Dim ws As Excel.Worksheet

    'Fill the list box with simple values
    Me.List1.AddItem "one"
    Me.List1.AddItem "two"
    Me.List1.AddItem "three"
    Me.List1.AddItem "four"
    Me.List1.AddItem "five"

    'Automate excel
    Set xl = New Excel.Application
    Set wb = xl.Workbooks.Add
    Set ws = wb.Worksheets.Item(1)

    'Hide the Excel window from users,
    ' (unless, of course, you want them to see it).
    xl.Visible = False

    'Save the values!
    For n = 1 To Me.List1.ListCount
    ws.Range("A" & Trim(Str(n))).Value = Me.List1.List(n - 1)
    Next n

    'Save the Work Book
    wb.SaveAs "Choose Your File Name.xls"

    'Clean up our mess.
    Set ws = Nothing
    Set wb = Nothing
    Set xl = Nothing

    'Done, now you have your Excel book with the listbox data in it.

    End Sub

  3. #3
    Join Date
    Aug 2004

    Re:Re:Save listbox into Excel

    [Originally posted by soda]

    Thankyou sooo much, i will give it a try, looks great to me though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center