+ Reply to Thread
Results 1 to 7 of 7

Problem in Returning Values from a Multi-column Listbox (ActiveX)

Hybrid View

  1. #1
    Registered User
    Join Date
    06-06-2006
    MS-Off Ver
    2010, 365
    Posts
    39

    Problem in Returning Values from a Multi-column Listbox (ActiveX)

    Hi,

    I need to achieve the following:

    1. insert an ActiveX listbox (single select, 4 columns) in the worksheet (Sheet1)
    2. populate the listbox with a range in another sheet (sheet2)
    3. after populating the list, whenever the user clicks on a row, a userform should pop-up with all the values in the 4 columns of the listbox in 4 different textboxes
    4. user may want to edit the data, which may then be committed to the data sheet (sheet2) and refreshed in the listbox as well.

    I am stuck at the 3rd step. Whenever I try to access the listbox range, the following error is generated:

    Run-time error '1004':

    Method 'Range' of object '_Worksheet' failed
    The code that I am using is:

    Private Sub myList_Click()
    Dim v1, v2, v3 As String
    Dim rng As Range
    
    Set rng = Range(myList.ListFillRange)
    
    v1 = myList.Value
    v2 = rng.Offset(myList.ListIndex, 1).Resize(1, 1).Value
    v3 = rng.Offset(myList.ListIndex, 2).Resize(1, 1).Value
    
    myForm.myTxt1.Value = v1 
    myForm.myTxt2.Value = v2 
    myForm.myTxt3.Value = v3 
    
    myForm.Show
    End Sub
    Is my approach wrong? Or some problem with my coding? Please help.

    Peace,

    Shivboy

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Shivboy,

    I tested your code without the UserForm and it works fine. Did you set the ListFillRange property manually or by code? If it isn't set, you will get this error.

    My Test Code
    Private Sub ListBox1_Click()
    
      Dim v1, v2, v3 As String
      Dim rng As Range
    
        Set rng = Range(ListBox1.ListFillRange)
    
        With ListBox1
          v1 = .Value
          v2 = rng.Offset(.ListIndex, 1).Resize(1, 1).Value
          v3 = rng.Offset(.ListIndex, 2).Resize(1, 1).Value
        End With
        
    End Sub
    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    06-06-2006
    MS-Off Ver
    2010, 365
    Posts
    39
    Hi Leith,

    I have set the ListFillRange property manually in the Properties of the listbox. But it still isn't working!

    Shivboy

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Shivboy,

    Can you post your workbook?

    Thanks,
    Leith Ross

  5. #5
    Registered User
    Join Date
    06-06-2006
    MS-Off Ver
    2010, 365
    Posts
    39
    Hi Leith,

    Here is the file attached. I forgot to mention one thing, I am using Excel 2007, if that makes a difference.

    Shivboy
    Attached Files Attached Files

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    The workbook in the zip is not a 2007 format.

    I can't see why you are working like this, why not have a Listbox on the UserForm? You can't define a cell like that, try

    Private Sub myList_Click()
        Dim v1, v2, v3 As String
        Dim rng    As Range
        Set rng = Sheet2.Cells(myList.ListIndex + 1, 1)
    
        v1 = myList.Value
        v2 = rng.Offset(0,1).Value
        v3 = rng.Offset(0,2).Value
    
        myForm.txt1.Value = v1
        myForm.txt2.Value = v2
        myForm.txt3.Value = v3
        myForm.Show
    End Sub
    Last edited by royUK; 09-16-2007 at 02:18 PM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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