Results 1 to 14 of 14

Thread: [RESOLVED] Export ListBox data to Excel...

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    41

    Resolved [RESOLVED] Export ListBox data to Excel...

    Hi!

    Office 2000 Access

    I have a listbox on my form with 6 columns in it and some rows.
    My purpose is to export data to Excel from that listbox and i can't get it done.

    I have tried this:

    Code:
    Private Sub ReportToExcel_Click()
    'EXPORT to EXCEL
    Dim xlApp As Excel.Application
    Dim xlSh As Excel.Worksheet
    Dim i As Long
    Set xlApp = New Excel.Application
    
    xlApp.Visible = True
    xlApp.Workbooks.Add
    Set xlSh = xlApp.Workbooks(1).Worksheets(1)
    For i = 1 To ReportRecordList.ListCount
        xlSh.Cells(i, 1).Value = ReportRecordList.ItemData(i - 1)
    Next
    
    
    Set xlSh = Nothing
    Set xlApp = Nothing
    End Sub
    But this code returns only 1 column, Header including.
    I can't understand what should i do with "i" to start pulling data from column 2 and so on...


    Thanks in advance!
    Ruslan.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Export ListBox data to Excel...

    you need to loop through the columns as well, something like

    vb Code:
    1. For i = 1 To ReportRecordList.ListCount
    2.   for j = ReportRecordList.columncount
    3.     xlSh.Cells(i, j).Value = ReportRecordList.list(i - 1, j)
    4.   next
    5. Next
    i did this in excel listbox so might not be quite the same for access listbox
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    41

    Re: Export ListBox data to Excel...

    Thanks for the reply!

    You are right, the ListBox in Excel and Listbox in Access is not the same.
    In Access there is no such property like ".list", only ListCount or ListIndex DataItem.

    My code would be useful if i'd want to export first column, but i need to get all the data from list box.
    At the moment i do not know how to show the programm that i want value from ListBox.Column(1) or ListBox.Column(2) ListBox.Column(3) and so on...

    Any ideas?

    Thanks,
    Ruslan.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Export ListBox data to Excel...

    try the help for access vba
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    41

    Resolved Re: Export ListBox data to Excel...

    Hi!

    I have managed to get all the data from ListBox without using ".List" property.
    After hours of studing ListBox and figuering out why my code pulls only first column from the listbox i came to conclusion that it is the BoundColumn that i must use.

    So i loop the BoundColumn value. But at the end of the loop i must manually set BoundColumn value to 1, couse when the export is finished the BoundColumn has value 7(this is in my case, couse i have 6 columns). If i wont set value to 1 then the next time i will get a blank Excel Worksheet, that is bacause BoundColumn still holds value 7 and there is no column 7 in my ListBox.

    Code follows:
    Code:
    Private Sub ReportToExcel_Click()
    'EXPORT to EXCEL
    Dim myExApp As Excel.Application    'variable for Excel App
    Dim myExSheet As Excel.Worksheet    'variable for Excel Sheet
    Dim i As Long                       'variable for ColumnCount
    Dim j As Long                       'variable for ListCount
    Set myExApp = New Excel.Application
    
    myExApp.Visible = True              'Sets Excel visible
    myExApp.Workbooks.Add               'Add a new Workbook
    Set myExSheet = myExApp.Workbooks(1).Worksheets(1)
    
    For i = 1 To ReportRecordList.ColumnCount   'Counter for ColumnCount
        ReportRecordList.BoundColumn = ReportRecordList.BoundColumn + 1 'Setting counter for BoundColumn
        For j = 1 To ReportRecordList.ListCount 'Counter for ListCount
            myExSheet.Cells(j , i) = ReportRecordList.ItemData(j - 1)    'Insert ItemData into Excel Worksheet
        Next j  'Iterating through ListCount
    Next i  'Iterating through ColumnCount
    ReportRecordList.BoundColumn = 1    'Setting BoundColumn to original 1
    
    Set myExSheet = Nothing 'Release Worksheet
    Set myExApp = Nothing   'Release Excel Application
    
    End Sub

    Thanks for trying to help, i hope that this Thread will help others to solve same problems.

    Best Regards,
    Ruslan.

  6. #6
    New Member
    Join Date
    Jun 2012
    Posts
    3

    Re: Export ListBox data to Excel...

    Quote Originally Posted by ruslannurijev View Post
    Hi!

    I have managed to get all the data from ListBox without using ".List" property.
    After hours of studing ListBox and figuering out why my code pulls only first column from the listbox i came to conclusion that it is the BoundColumn that i must use.

    So i loop the BoundColumn value. But at the end of the loop i must manually set BoundColumn value to 1, couse when the export is finished the BoundColumn has value 7(this is in my case, couse i have 6 columns). If i wont set value to 1 then the next time i will get a blank Excel Worksheet, that is bacause BoundColumn still holds value 7 and there is no column 7 in my ListBox.

    Code follows:
    Code:
    Private Sub ReportToExcel_Click()
    'EXPORT to EXCEL
    Dim myExApp As Excel.Application    'variable for Excel App
    Dim myExSheet As Excel.Worksheet    'variable for Excel Sheet
    Dim i As Long                       'variable for ColumnCount
    Dim j As Long                       'variable for ListCount
    Set myExApp = New Excel.Application
    
    myExApp.Visible = True              'Sets Excel visible
    myExApp.Workbooks.Add               'Add a new Workbook
    Set myExSheet = myExApp.Workbooks(1).Worksheets(1)
    
    For i = 1 To ReportRecordList.ColumnCount   'Counter for ColumnCount
        ReportRecordList.BoundColumn = ReportRecordList.BoundColumn + 1 'Setting counter for BoundColumn
        For j = 1 To ReportRecordList.ListCount 'Counter for ListCount
            myExSheet.Cells(j , i) = ReportRecordList.ItemData(j - 1)    'Insert ItemData into Excel Worksheet
        Next j  'Iterating through ListCount
    Next i  'Iterating through ColumnCount
    ReportRecordList.BoundColumn = 1    'Setting BoundColumn to original 1
    
    Set myExSheet = Nothing 'Release Worksheet
    Set myExApp = Nothing   'Release Excel Application
    
    End Sub

    Thanks for trying to help, i hope that this Thread will help others to solve same problems.

    Best Regards,
    Ruslan.
    This code is fine but how to I can export with name field ???

    Many Thanks

  7. #7
    New Member
    Join Date
    Jun 2012
    Posts
    3

    Re: Export ListBox data to Excel...

    and how to I can import from excel to Table ??

  8. #8
    New Member
    Join Date
    Jun 2012
    Posts
    3

    Re: Export ListBox data to Excel...

    Quote Originally Posted by uronmapu View Post
    This code is fine but how to I can export with name field ???

    Many Thanks
    Done: In Listbox, Column Heads select yes

  9. #9
    Addicted Member
    Join Date
    Jul 2012
    Location
    Wiltshire, England
    Posts
    211

    Re: [RESOLVED] Export ListBox data to Excel...

    Hi,

    An update to this thread for Access 2007 and no need to change bound column:

    Code:
    Dim appExcel As Excel.Application
    Dim wksData As Excel.Worksheet
    Dim intColumn_Index As Integer
    Dim intRow_Index As Integer
    
    Set appExcel = New Excel.Application
    appExcel.Workbooks.Add
    
    Set wksData = appExcel.Workbooks(1).Worksheets(1)
    
    For intRow_Index = 0 To lstMissing_User_Fields.ListCount
    
        For intColumn_Index = 1 To lstMissing_User_Fields.ColumnCount
            wksData.Cells(intRow_Index + 1, intColumn_Index).Value = lstMissing_User_Fields.Column(intColumn_Index, intRow_Index)
        Next intColumn_Index
    
    Next intRow_Index
    
    appExcel.Visible = True
    
    Set wksData = Nothing
    Set appExcel = Nothing
    Note that intRow_Index starts at zero to get column headers which have been turned on.

    Kristian

  10. #10
    New Member
    Join Date
    Apr 2014
    Posts
    4

    Question Re: [RESOLVED] Export ListBox data to Excel...

    Quote Originally Posted by kriswork View Post
    Hi,

    An update to this thread for Access 2007 and no need to change bound column:

    Code:
    Dim appExcel As Excel.Application
    Dim wksData As Excel.Worksheet
    Dim intColumn_Index As Integer
    Dim intRow_Index As Integer
    
    Set appExcel = New Excel.Application
    appExcel.Workbooks.Add
    
    Set wksData = appExcel.Workbooks(1).Worksheets(1)
    
    For intRow_Index = 0 To lstMissing_User_Fields.ListCount
    
        For intColumn_Index = 1 To lstMissing_User_Fields.ColumnCount
            wksData.Cells(intRow_Index + 1, intColumn_Index).Value = lstMissing_User_Fields.Column(intColumn_Index, intRow_Index)
        Next intColumn_Index
    
    Next intRow_Index
    
    appExcel.Visible = True
    
    Set wksData = Nothing
    Set appExcel = Nothing
    Note that intRow_Index starts at zero to get column headers which have been turned on.

    Kristian
    I know some time has passed, but I'm trying to use this. The only problem I am having is that it is skipping outputting the first column of data. I'm using Access 2010. Ideas?

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Export ListBox data to Excel...

    For intColumn_Index = 1 To lstMissing_User_Fields.ColumnCount
    index of first column should be 0 in listbox, but 1 in excel worksheet

    try starting from columnindex = 0 but add 1 to the cell column
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12
    New Member
    Join Date
    Apr 2014
    Posts
    4

    Re: [RESOLVED] Export ListBox data to Excel...

    Yep, that is exactly what I did and it worked. Thanks.

  13. #13
    New Member
    Join Date
    Apr 2014
    Posts
    4

    Re: [RESOLVED] Export ListBox data to Excel...

    I have a followup question to this thread. The export from the listbox is working great - THANKS. However, I would like to add a row in Excel before the data starts that shows the parameters that are in a label in Access. Any idea where I would insert this? I'm really just a Google programmer. I tried to add something before the For loop, but...

  14. #14
    New Member
    Join Date
    Apr 2014
    Posts
    4

    Re: [RESOLVED] Export ListBox data to Excel...

    Actually, I solved my own problem. I just put the label values before the For Loop and changed the intRow_Index to start lower in the spreadsheet!

Posting Permissions

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



Click Here to Expand Forum to Full Width