+ Reply to Thread
Results 1 to 23 of 23

Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

  1. #1
    Registered User
    Join Date
    07-14-2021
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    31

    Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    Hello Excel gurus,


    I am trying to populate a ListBox on a UserForm with an inventory from one of 50 different trucks.

    The user will open their inventory on the spreadsheet, it gets processed there after some formulas work their magic.

    Then it needs to be displayed in a ListBox.

    The data will always have 4 columns, but the rows can vary.

    The standard way we all know how to use the ListBox is:
    Please Login or Register  to view this content.
    The "truck" sheet isn't known until the user opens it.

    So let's say truck D50 (which is referenced through a variable and not "hard coded") is created and has its inventory listed on a new sheet with that name within an already existing workbook.

    How can I reference that variable as the sheet name?

    Thank you for any help and a great website!

  2. #2
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    Hi.

    PHP Code: 
    'First assign a variable the data range (without the titles):

    With Sheets(1)
      Set Rng = .Range("A2:E" & .Cells(Rows.Count, "A").End(xlUp).Row)
    End With

    '
    And then assign the range to the listbox:

    ListBox1.RowSource Rng.Address(External:=True
    You are always very welcome if you add reputation by clicking the * (bottom left) of each message that has helped you.

  3. #3
    Registered User
    Join Date
    07-14-2021
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    31

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    Outstanding! Worked perfectly! Thank you!

  4. #4
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    It was a great pleasure to help you, thanks for the +rep and until the next help.

  5. #5
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,517

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    Or if you want to keep it simple:
    Please Login or Register  to view this content.
    Change references as required.
    You don't need to give me a star as all I am trying to do is help people without requesting recognition.
    Experience trumps academics every day of the week and twice on Sunday.

  6. #6
    Registered User
    Join Date
    07-14-2021
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    31

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    Thanks for the help everyone.

    The only thing I think I forgot to mention was the fact that it did work, but I had to add a slight modification - the only way to reference "Sheet1" was to move it to the beginning of the sheet tabs. then I renamed it. Perfect!

  7. #7
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,377

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    Take a look at the .List property as well. Instead of .rowsource

  8. #8
    Registered User
    Join Date
    07-14-2021
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    31

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    Thanks for that, I had actually tried that before RowSource because I thought it would be easier to use .Clear.

    Then I realized I really didn't need to clear it.

    But thanks!

  9. #9
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,302

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    @jolivanes & @JEC - sound advice.
    Torachan,

    Mission statement; Promote the use of Tables, Outlaw the use of 'merged cells' and 'RowSource'.

  10. #10
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    Quote Originally Posted by Spielberg View Post
    Thanks for that, I had actually tried that before RowSource because I thought it would be easier to use .Clear.

    Then I realized I really didn't need to clear it.

    But thanks!
    It is much smarter to use RowSource than List because it allows you to see the same format in the listBox as in the cells.

    And the "formal" equivalent to:
    ListBox1.Clear

    is:
    ListBox1.RowSource = ""

  11. #11
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,517

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    This site has a good explanation on these issues, some confirming, others contradicting.

    https://excelmacromastery.com/vba-listbox/

  12. #12
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,302

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    And another site that has more information that will save many an headache.

    Golden rule, never refresh RowSource if you have been swopping or activating other sheets (it gets lost and some surprise will haunt you).

  13. #13
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,377

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    Always go for .list in my opinion.
    Only. rowsource has better headers

  14. #14
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,208

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    Personally, I am not in favor of using RowSource. I do well enough using List.
    However, regardless of my preference, as a warning, I send the workbook with a RowSource associated with a dynamic data range. In good faith, we are linking RowSource to dynamic data hoping that if the data in the source scope changes, it will also change in the ListBox. And in the meantime...

    WARNING: When opening this file, make it the only open workbook in Excel. Otherwise you may lose data from other workbooks.
    You have been warned.

    I know the answer to the resulting problem. This is more of a task for those interested.

    Artik
    Attached Files Attached Files

  15. #15
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    Hello Artik.
    The technique you use to add rows to a listObject invariably leads to a well-known bug.

    But the solution is really simple:
    PHP Code: 
    ListBox1.RowSource "" 
    So I'm going to show you how to add or delete rows.
    Attached Files Attached Files

  16. #16
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable


    Quote Originally Posted by JEC. View Post
    Only. rowsource has better headers
    In this case better is to use a ListView rather than a ListBox …

  17. #17
    Registered User
    Join Date
    07-14-2021
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    31

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    Excellent! Thanks!

  18. #18
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    Quote Originally Posted by Marc L View Post

    In this case better is to use a ListView rather than a ListBox …
    I agree, Marc, but with one caveat: The number of cells with data! as this last detail is what makes the ListView inefficient.

  19. #19
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable


    As a ListView can work with ADO SQL I can't see why it could be less efficient than a ListBox …

    As using an UserForm ListBox is most of the time useless 'cause less features than directly under Excel
    so the same with a ListView, a DataGrid, whatever …

    So the use of any ListBox or any alternative must be devoluted for small ancillary tasks but not for main Excel purposes.
    From hundreds & hundreds UserForm I've seen I found very few (far less than ten) really necessary …
    The classic no sense : « I wan't to filter my data within an UserForm » so why not directly use the Excel filter or advanced filter ?!
    Last edited by Marc L; 02-06-2023 at 10:34 AM.

  20. #20
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,517

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    Re: "is most of the time useless"

    I think that the vast majority of people seeking help on these sites are not professionals like you, Marc L and beyond Excel, are. Some, or maybe most, of your suggestions are fantastic.
    However, in my opinion, the vast majority again I think, are learning with baby steps and will not be able to fix or change these contributions to suit their needs. Of course, some people don't care as long as it does what they asked for.
    To me a perfect example is looping and non looping suggestions. Most of us are aware of the fact that looping generally is slower but with todays equipment for most users not noticeably so, but it is generally easier to follow what is happening with the code as it goes along doing it's thing and thus should be simpler to fix or adjust.

    Don't see this as a knock on your contributions because I think they are really good.

    Keep up the good work.

    Above observation is just my opinion.

  21. #21
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable


    Was just my answer to beyond Excel about many UserForm threads like those asking for a Dictionary when it's not the best way to go …

  22. #22
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    Quote Originally Posted by jolivanes View Post
    However, in my opinion, the vast majority again I think, are learning with baby steps and will not be able to fix or change these contributions to suit their needs. Of course, some people don't care as long as it does what they asked for.
    For those who want (and have the time) to study a bit more, here's a basic listView management model:

    - Differentiated format of the columns,
    - Sorting by column (with a click on the header of each column),
    - Filter by one of the fields, and
    - Removal of records.

    An interesting exercise is to copy the UserForm and change the listView to a listBox and compare times and performance.

    By the way: the "magic" of the model that I attached is not only provided by the listView but is provided by a recordset (Rst): it is it that performs three of the four points mentioned above.
    Attached Files Attached Files
    Last edited by beyond Excel; 02-07-2023 at 04:12 PM.

  23. #23
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Trying to use RowSource in a UserForm ListBox, referencing a sheet name by a variable

    .
    (Removed)
    .
    Last edited by beyond Excel; 02-07-2023 at 04:11 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Filter UserForm Multicolumn ListBox. populated by RowSource. with TextBox/ComboBox
    By Alex.riccio in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-07-2016, 10:19 AM
  2. [SOLVED] Listbox Rowsource set when userform opened
    By PFDave in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-23-2016, 05:58 AM
  3. [SOLVED] Userform listbox adding rowsource runtime error 380
    By anrichards22 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-11-2016, 02:10 PM
  4. Update Listbox Rowsource when combobox_change in userform
    By hifliers in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-30-2014, 06:50 AM
  5. [SOLVED] VBA: UserForm ListBox Column Headings RowSource = Named Range
    By Hudas in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-22-2012, 02:21 AM
  6. [SOLVED] ???Help??? Userform.Listbox.rowsource = ???
    By Steve Sparti in forum Excel General
    Replies: 0
    Last Post: 03-01-2006, 05:50 PM
  7. [SOLVED] Initialize Userform, Populate Listbox, Dynamic RowSource?
    By RShow in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-21-2005, 03:05 PM

Tags for this Thread

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