Results 1 to 14 of 14

Thread: Extracting a range to a listbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    14

    Extracting a range to a listbox

    I have a range in an excel document which I need to extract into a listbox. I can do this through .txt file, but really would prefer to source the information from excel. Any ideas?

    Many tks for yr help in advance.

    Best Rds.

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    You'll need to...
    • Start a new vb project
    • Add 1 listbox and 1 command button to the form
    • Goto the project menu > references option & select the Microsoft Excel Library line
    • Create an excel file with the first 10 cells A1 to A10 with data in, then save this file as "C:\file.xls"


    Code:
    Private Sub Command1_Click()
        Dim objXLApp As Excel.Application
        Dim intLoopCounter As Integer
        
        Set objXLApp = New Excel.Application
        
        With objXLApp
            .Workbooks.Open "C:\File.xls"
            .Workbooks(1).Worksheets(1).Select
        
            For intLoopCounter = 1 To 10
                List1.AddItem .Range("A" & intLoopCounter)
            Next intLoopCounter
            
            .Workbooks(1).Close False
            .Quit
        End With
        
        Set objXLApp = Nothing
    End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    14
    Tks for the help..... I understand this code. And it works fine assuming the list in excel doesn't grow.

    Is there a way to display a range which has been "named"

    Example:

    Range("Test")

    Where Range("test") = A1 to A20

    Tks for yr help!

  4. #4
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    You could loop through until you come to a cell that is blank.
    Code:
    With objXLApp
            .Workbooks.Open "C:\File.xls"
            .Workbooks(1).Worksheets(1).Select
    
        count = 1
        while strComp(.cells(count,1),"")<>0
            List1.AddItem .Range("A" & count)
    
            count = count + 1
        wend
    
    end with
    i haven't check this so the syntax may be wrong but the idea will work!!!!
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    14
    I tried yr code I get teh following error msg:

    Comile error:

    Function or interface marked as restricited, or the function uses an Automation type not supported in Visual Basics.

    Any ideas?

    Tks for yr help.

  6. #6
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    put

    dim count as integer

    above the count = 1 line
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  7. #7
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Sorry, only just noticed you replied to this - if the suggestions above don't work, you should be able to use this one:
    Code:
    Private Sub Command1_Click()
        Dim objXLApp As Excel.Application
        Dim intLoopCounter As Integer
        
        Set objXLApp = New Excel.Application
        
        With objXLApp
            .Workbooks.Open "C:\File.xls"
            .Workbooks(1).Worksheets(1).Select
        
            For intLoopCounter = 1 To CInt(.ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row)
                List1.AddItem .Range("A" & intLoopCounter)
            Next intLoopCounter
            
            .Workbooks(1).Close False
            .Quit
        End With
        
        Set objXLApp = Nothing
    End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  8. #8
    New Member
    Join Date
    Mar 2003
    Posts
    1
    Have been following this thread as I'm doing something similar; however, I'm having a problem listing a range. Please see "not working" comment in code below.
    Thanks for any help offered.

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim objXLApp As Excel.Application
    3.     Dim intRowCounter As Integer
    4.     Set objXLApp = New Excel.Application
    5.     With objXLApp
    6.         .Workbooks.Open "C:\File.xls"
    7.         .Workbooks(1).Worksheets(1).Select
    8.         For intRowCounter = 1 To 25
    9.             'Test for empty cell in column 2
    10.             If StrComp(.Cells(intRowCounter, 2), "") <> 0 Then
    11.                 List1.AddItem .Range("A" & intRowCounter)
    12.                 'Not working: List1.AddItem .Range("A" & intRowCounter, "C" & intRowCounter)
    13.             End If:
    14.         Next intRowCounter
    15.         .Workbooks(1).Close False
    16.         .Quit
    17.     End With
    18.     Set objXLApp = Nothing
    19. End Sub
    Last edited by dnivi; Mar 26th, 2003 at 11:04 PM.

  9. #9
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    To specify a range, you have to use this format - A1:C1 rather than the comma, try altering the commented line to this:
    VB Code:
    1. List1.AddItem .Range("A" & intRowCounter & ":C" & intRowCounter)

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  10. #10
    Lively Member
    Join Date
    Mar 2003
    Location
    singapore
    Posts
    86
    try using this ... your list box should be named "list"
    Private Sub UserForm_Initialize()
    Dim lngRow As Long
    Dim strTxt As String
    lngRow = 1
    Do Until IsEmpty(Cells(lngRow, 1))
    strTxt = strTxt & Cells(lngRow, 1) & vbLf
    lngRow = lngRow + 1
    Loop
    list.Text = Left(strTxt, Len(strTxt) - 1)
    End Sub

  11. #11
    New Member
    Join Date
    May 2007
    Posts
    1

    Question Re: Extracting a range to a listbox

    Quote Originally Posted by Dandlv
    I have a range in an excel document which I need to extract into a listbox. I can do this through .txt file, but really would prefer to source the information from excel. Any ideas?

    Many tks for yr help in advance.

    Best Rds.
    Dim objXLApp As Excel.Application

    I have paste the entire code and also created a button and a list box on the form as well as created the desired tables in the excel file.

    on the buttons click event the application gives me this error

    " User define type not defined " for Dim objXLApp As Excel.Application

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Extracting a range to a listbox

    Welcome to VBForums

    You haven't added the reference, as specified at the top of post #2.

    You may find my Excel Tutorial (link in my signature) useful.

  13. #13
    New Member
    Join Date
    May 2009
    Posts
    4

    Re: Extracting a range to a listbox

    Hey Alex,
    I am unable to find the "Microsoft Excel Library" in the Project emnu. Any other suggestions.

    Thanks and Regards,
    Gideon.

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

    Re: Extracting a range to a listbox

    I am unable to find the "Microsoft Excel Library" in the Project emnu. Any other suggestions.
    to add a reference in VBA, menu > tools > references
    vb6 is different, there it would be in the project menu
    but if you are doing this in excel you do not need to do
    if you are using late binding you do not need to do

    if you have a question start a new thread, instead of reserrecting one that was started in 2003, and ask a full question with information about what program or application you are writing your code in
    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

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