+ Reply to Thread
Results 1 to 7 of 7

Export contents of listbox to new excel workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    03-04-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    48

    Question Export contents of listbox to new excel workbook

    I've got a userform that searches a database for certain criteria and displays the results in a list box with 7 columns. I'm now trying to figure out the code to export the contents of that listbox into a new workbook executing with a command button, appreciate any help!
    Last edited by jstn; 09-20-2014 at 02:42 PM.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Export contents of listbox to new excel workbook

    Try something like this...

    Private Sub CommandButton1_Click()
        With Me.ListBox1
            Workbooks.Add xlWBATWorksheet
            Sheets(1).Range("A1").Resize(.ListCount, .ColumnCount).Value = .List
        End With
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Export contents of listbox to new excel workbook

    Hi

    This is all you need to fill and store entries from a listbox.
    I have included a few redundant lines to give you some hints.

    Private Sub CommandButton1_Click()
    MyName = ActiveWorkbook.Name
    Workbooks.Add
    NewName = ActiveWorkbook.Name
    Entries = ListBox1.ListCount
    For Count = 0 To Entries - 1
    For count2 = 0 To 6
    
    Cells(Count + 1, count2 + 1).Value = ListBox1.List(Count, count2)
    Next
    Next
    
    End Sub
    
    Private Sub UserForm_Activate()
    ListBox1.List = Range("A1:G18").Value
    End Sub
    This code stores the whole listbox. How often will you need to do that?

    
    Private Sub CommandButton1_Click()
    
    Workbooks.Add
    Range("A1:A" & ListBox1.ListCount).Value = ListBox1.List
    End Sub
    Attached Files Attached Files
    Last edited by mehmetcik; 09-20-2014 at 12:59 PM.

  4. #4
    Registered User
    Join Date
    03-04-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    48

    Re: Export contents of listbox to new excel workbook

    That does the trick, thank you! seems so simple, I suspected it would be but couldn't nail it down.
    The whole list box contents is what I needed, the filtering is done on the userform.

    A small adjustment if possible, the list box doesn't have headers, but the database it pulls from does, could I have the headers added into the exported file? Could we also size the columns in the workbook to fit the data?

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Export contents of listbox to new excel workbook

    Quote Originally Posted by jstn View Post
    A small adjustment if possible, the list box doesn't have headers, but the database it pulls from does, could I have the headers added into the exported file? Could we also size the columns in the workbook to fit the data?
    Private Sub CommandButton1_Click()
        With ActiveSheet
            Workbooks.Add xlWBATWorksheet
            .Rows(1).Copy Destination:=ActiveSheet.Rows(1)
        End With
        With Me.ListBox1
            ActiveSheet.Range("A2").Resize(.ListCount, .ColumnCount).Value = .List
        End With
        ActiveSheet.Columns.AutoFit
    End Sub

  6. #6
    Registered User
    Join Date
    03-04-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    48

    Re: Export contents of listbox to new excel workbook

    Thank you again, that's perfect! Is there a way to declare the column headers?

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Export contents of listbox to new excel workbook

    You're welcome.

    Private Sub CommandButton1_Click()
        Workbooks.Add xlWBATWorksheet
        Range("A1:G1").Value = Array("Header1", "Header2", "Header3", "Header4", "Header5", "Header6", "Header7")
        With Me.ListBox1
            ActiveSheet.Range("A2").Resize(.ListCount, .ColumnCount).Value = .List
        End With
        ActiveSheet.Columns.AutoFit
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Activate the first workbook from the listbox, copy all the contents & close it
    By kit370 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-22-2014, 07:38 PM
  2. I would need a macro to export data from base example workbook to export worbook
    By slato8 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-01-2012, 11:21 AM
  3. Replies: 1
    Last Post: 07-08-2010, 06:10 AM
  4. Replies: 1
    Last Post: 08-07-2008, 02:11 PM
  5. [SOLVED] Export TextBox contents to new workbook.
    By Andy Tallent in forum Excel General
    Replies: 0
    Last Post: 02-22-2005, 10:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1