+ Reply to Thread
Results 1 to 6 of 6

Thread: How to insert a dynamic range in a UserForm into a ListBox

  1. #1
    Valued Forum Contributor
    Join Date
    05-18-2009
    Location
    Slo
    MS-Off Ver
    Excel 2007
    Posts
    210

    How to insert a dynamic range in a UserForm into a ListBox

    Hi

    I made a UserForm that collects the data from the active sheet and it copys it in to the UserForm.

    In here I have a ListBox where in the options menu I set the ColumnCount: 6 becouse I have 6 rows I want to be shown in the UserForm. The Columns are A:F how many rows is actualy different. Sometime the data goes from A3:F40 and sometimes it goes from A3:F5 depends on the material order. Under options for the ListBox I tryed to enter the RowSource but I can only insert it like this A3:F40 then I have a lot of empty cells.

    So is it possible somehow to insert here a dynamic range that would take cells from A3 (always the beggining cell) to F - How many rowes are filled?
    Last edited by stojko89; 11-18-2011 at 02:27 AM.

  2. #2
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: How to insert a dynamic range in a UserForm into a ListBox

    Create a proper Dynamic Named Range to use as the RowSource

    Alternatively, you can set the range when the form loads

    Option Explicit
    
    
    Private Sub UserForm_Initialize()
        Dim rData As Range
        With Sheet1 '<- sheet with data on
            Set rData = .Range(.Cells(3, 1), .Cells(.Rows.Count, 6).End(xlUp))
        End With
    
        Me.ListBox1.List = rData.Value
    End Sub
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  3. #3
    Valued Forum Contributor
    Join Date
    05-18-2009
    Location
    Slo
    MS-Off Ver
    Excel 2007
    Posts
    210

    Re: How to insert a dynamic range in a UserForm into a ListBox

    royUK
    I've inserted the code like this:
    Option Explicit
    
    Private Sub UserForm_Initialize()
        Dim rData As Range
        With Sheets("00057-2011")
            Set rData = .Range(.Cells(3, 1), .Cells(.Rows.Count, 6).End(xlUp))
        End With
    
        Me.lstMaterial.Value = rData.Value
    End Sub
    And I get an error and it says that it couldn't set the Value property.

  4. #4
    Valued Forum Contributor
    Join Date
    05-18-2009
    Location
    Slo
    MS-Off Ver
    Excel 2007
    Posts
    210

    Re: How to insert a dynamic range in a UserForm into a ListBox

    Sory!! Sory!!

    I just saw I've entered the last part of the code wrong.

    It works great! :D

  5. #5
    Valued Forum Contributor
    Join Date
    05-18-2009
    Location
    Slo
    MS-Off Ver
    Excel 2007
    Posts
    210

    Re: How to insert a dynamic range in a UserForm into a ListBox

    Thanks a lot royUK!

    Works for every sheet

    I won't even bother to try and make the dynamic range the other way.

    So thank you.

  6. #6
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: How to insert a dynamic range in a UserForm into a ListBox

    or

    Private Sub UserForm_Initialize()
      lstMaterial.List = Sheets("00057-2011").cells(1).currentregion.offset(2).resize(,6).Value
    End Sub
    If you treat these sheets as data'warehouses' you can start in row 1.
    Last edited by snb; 11-18-2011 at 02:30 AM.



+ 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.2.0