Export ListBox contents to new workbook via CommandButton

HArce

New Member
Joined
Apr 5, 2013
Messages
15
Hello,

I'm having difficulty coming up with vba to copy the filtered contents in a multi-column listbox "ListBox1" to a new workbook. I would like to achieve this via CommandButton "Export" located in UserForm frmAAGReport". Can anyone offer some insight?

P.S. - I would attach a sample workbook but can not find where I can include an attachment. Not sure if it's because I'm a new member.

Thank you.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Welcome to Mr.Excel!

Some more information is necessary:

I'm having difficulty coming up with vba to copy the filtered contents in a multi-column listbox "ListBox1" to a new workbook. I would like to achieve this via CommandButton "Export" located in UserForm frmAAGReport". Can anyone offer some insight?

1. Do you want all the contents of the ListBox, or just the selected items?
2. Is the Listbox on the form, or a worksheet?
3. Did you mean new workbook or new worksheet and do you want to name the new workbook (or worksheet)?
4. Where on the target worksheet do you want the items to go?
5. https://www.box.com/ is a file sharing location you can use.
 
Upvote 0
Welcome to Mr.Excel!

Some more information is necessary:



1. Do you want all the contents of the ListBox, or just the selected items?
2. Is the Listbox on the form, or a worksheet?
3. Did you mean new workbook or new worksheet and do you want to name the new workbook (or worksheet)?
4. Where on the target worksheet do you want the items to go?
5. https://www.box.com/ is a file sharing location you can use.


Thanks for your response, tlowry. I'll try to address your questions in the same order below.
1. I've already setup a filter via another command button to filter specific information which displays in the listbox. Hence, filtered information displayed in the listbox is ideal.
2. The listbox is located in a userform titled "frmAAGReport"
3. I meant in a brand new workbook, though a new worksheet would also work.
4. The items can be listed starting on Cell A1 in the same format it is displayed in the source.
5. Thanks for this information. I'll try to upload it and provide a link here.

Thank you.
 
Upvote 0
This is what I think you want...

Put this code in the form's module for the CommandButton1

Code:
Private Sub CommandButton1_Click()
   With Workbooks.Add
        .Sheets(1).Range(Cells(1, 1), Cells(Me.ListBox1.ListCount, Me.ListBox1.ColumnCount)) = _
            Me.ListBox1.List
        Sheets(1).Name = Format(Now(), "yyyy-mm-dd") & " Transfer"
    End With
End Sub
 
Upvote 0
Thank you, tlowry! The code works very well. A couple of questions though: (1) would it be possible to maintain the same format as the source and (2) can the header row transfer to the new workbook? Thanks!
 
Upvote 0
Having a header row is an important bit of information. Here's the new code:
Code:
Private Sub CommandButton1_Click()
    Dim rng: Set rng = Range(Me.ListBox1.RowSource)
    Select Case Me.ListBox1.ColumnHeads
        Case True
            Range(Cells(rng.Row - 1, rng.Column).Address & ":" & _
                            rng.Cells.SpecialCells(xlCellTypeLastCell).Address).Copy
        Case Else
            rng.Copy
        End Select
    With Workbooks.Add
        Cells(1, 1).PasteSpecial xlAll
        Sheets(1).Name = Format(Now(), "yyyy-mm-dd") & " Transfer"
    End With
End Sub
 
Upvote 0
Having a header row is an important bit of information. Here's the new code:
Code:
Private Sub CommandButton1_Click()
    Dim rng: Set rng = Range(Me.ListBox1.RowSource)
    Select Case Me.ListBox1.ColumnHeads
        Case True
            Range(Cells(rng.Row - 1, rng.Column).Address & ":" & _
                            rng.Cells.SpecialCells(xlCellTypeLastCell).Address).Copy
        Case Else
            rng.Copy
        End Select
    With Workbooks.Add
        Cells(1, 1).PasteSpecial xlAll
        Sheets(1).Name = Format(Now(), "yyyy-mm-dd") & " Transfer"
    End With
End Sub

Thanks, tlowry. I'm having a problem with the code this time around. When the data transfers over to the new workbook, the cells where the data should be are highlighted in black and the data does not transfer. Any idea what might be causing this to happen? Thanks again!
 
Upvote 0
Well, this is getting a bit more complicated.

I think the problem is that the code (that would be me) made an assumption that the data (without headers, or formatting) were in the active sheet. Here's an update:


Code:
Private Sub CommandButton1_Click()
    Dim aSheet, aWind
    Set aWind = ThisWorkbook
    Set aSheet = ActiveSheet
    Sheets(Range(Me.ListBox1.RowSource).Parent.Name).Activate
    Dim rng: Set rng = Range(Me.ListBox1.RowSource)
    Select Case Me.ListBox1.ColumnHeads
        Case True
            Range(Cells(rng.Row - 1, rng.Column).Address & ":" & _
                            rng.Cells.SpecialCells(xlCellTypeLastCell).Address).Copy
        Case Else
            rng.Copy
        End Select
    Dim oW
    Set oW = Workbooks.Add
    Cells(1, 1).PasteSpecial xlAll
    Sheets(1).Name = Format(Now(), "yyyy-mm-dd") & " Transfer"
    Cells(1, 1).Select
    aWind.Activate
    Application.CutCopyMode = False
    aSheet.Activate
    oW.Activate
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,510
Members
448,967
Latest member
screechyboy79

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top