+ Reply to Thread
Results 1 to 17 of 17

Using vba to populate ComboBox list using named ranges

  1. #1
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Using vba to populate ComboBox list using named ranges

    ...
    I've got some code that works when I explicitly define the range. I'm wondering why it won't using named ranges. It specifically throws "Could not List Property" error on "Me.ComboBox1.List(Me.ComboBox1.ListIndex, 1)" The form activation seems to accept using named ranges for ComboBox1. Is there a way to do this using named ranges?
    Please Login or Register  to view this content.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Using vba to populate ComboBox list using named ranges

    The array variable is populated from one contiguous range. loop through each named range something like this...

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 04-04-2018 at 10:58 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Using vba to populate ComboBox list using named ranges

    Thanks, AlphaFrog. I'm still getting list property error which is due to the way I load the ComboBox1 in the userform. ComboBox1 data shows but as soon as a selection is made, the error is thrown. How would I load a combobox using named ranges? It's what I read off the MSDN, but to their credit, there was nothing related to using named ranges (EDIT: using named ranges in userform controls).
    Please Login or Register  to view this content.
    This is what I currently use to initialize Comboboxes, but it is somewhat nightmarish since it references cells instead of names.
    Please Login or Register  to view this content.
    Last edited by terriertrip; 04-05-2018 at 12:54 AM. Reason: to keep references consistent

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Using vba to populate ComboBox list using named ranges

    I don't follow.

    Did my previous code work?
    Is this a different problem?
    It seems like its a different combobox (combobox3) but with the same problem as before. If yes, then the previous code construct should work for this combobox.

  5. #5
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Using vba to populate ComboBox list using named ranges

    No. The code did not work. Produces same error on the same line.
    I thought it may be due to the way ComboBox1 was initially populated.

    Please disregard the combobox3 statement; it was simply a sidenote of another example of populating a combobox I'm using in another routine.

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Using vba to populate ComboBox list using named ranges

    Quote Originally Posted by terriertrip View Post
    No. The code did not work. Produces same error on the same line.
    The line of code that errored in the original code no longer exists in the new code. Does the new code error on this line?
    v = ws.Range(nm).Value

    Are all the named ranges on this sheet?
    ThisWorkbook.Sheets(msSHEET_LST)

    Double check that all these named ranges actually exist and the Names are exact.
    "Name1", "Name2", "Name3", "Name4"

  7. #7
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Using vba to populate ComboBox list using named ranges

    The error (v(i, 2) =subscript out of range) is on the IfThen line
    If v(i, 2) = Me.ComboBox1.List(Me.ComboBox1.ListIndex, 1) Then

    Yes all named ranges exist on this sheet (AY:BD)

    All Named ranges exist and are exact.

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Using vba to populate ComboBox list using named ranges

    Quote Originally Posted by terriertrip View Post
    The error (v(i, 2) =subscript out of range) is on the IfThen line
    If v(i, 2) = Me.ComboBox1.List(Me.ComboBox1.ListIndex, 1) Then
    Do the named ranges have four columns each?

  9. #9
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Using vba to populate ComboBox list using named ranges


  10. #10
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Using vba to populate ComboBox list using named ranges

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  11. #11
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Using vba to populate ComboBox list using named ranges

    ...
    How can I use strictly named ranges and get the same result?
    Attached Files Attached Files

  12. #12
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Using vba to populate ComboBox list using named ranges

    Try this. It assumes each named range is based on the OD letter using this naming convention:
    OD_A
    OD_B
    OD_C
    ...etc

    It gets the OD letter of the selected item from the second column of cboOD

    Please Login or Register  to view this content.

  13. #13
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Using vba to populate ComboBox list using named ranges

    I admire your late night fortitude but afraid that didn't work. I copied directly into the sample wb and it throws an error (v=Nothing). The variant range needs to be A:D since that is what will be the list within cboWt (combobox2).

  14. #14
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Using vba to populate ComboBox list using named ranges

    Quote Originally Posted by terriertrip View Post
    I admire your late night fortitude but afraid that didn't work. I copied directly into the sample wb and it throws an error (v=Nothing). The variant range needs to be A:D since that is what will be the list within cboWt (combobox2).
    Did you create named ranges using the naming convention i discussed?

    each named range is based on the OD letter using this naming convention:
    OD_A
    OD_B
    OD_C
    ...etc

  15. #15
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Using vba to populate ComboBox list using named ranges

    Oh yes. I forgot about doing that. After renaming the ranges, the subscript error occurs on
    .AddItem v(i, 2)

  16. #16
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Using vba to populate ComboBox list using named ranges

    It worked for me.
    Attached Files Attached Files

  17. #17
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Using vba to populate ComboBox list using named ranges

    Ok. I see what you were talking about, but that's only creating more issues.

    This range of data is part of a large lookuplist with over a hundred columns. The lookuplist imports a master lookuplist database, so every time a user opens a new workbook, a master lookuplist is transferred to the workbook and ranges are named - everything done by vba.

    I would then need additional code to select those ranges you are suggesting, and name them strictly using vba. So I'm not sure if this is the best direction.

+ 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. [SOLVED] Combobox combining 2 named ranges
    By gsandy in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-12-2017, 03:33 PM
  2. Using Named range to populate a Combobox in userform
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-04-2015, 09:57 PM
  3. Using named range to populate ComboBox
    By tanktata in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-29-2015, 04:39 PM
  4. [SOLVED] Unable to populate ComboBox with a Named Range
    By RJK in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-22-2012, 06:22 PM
  5. [SOLVED] Populate combobox named range
    By zardof in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-14-2012, 04:00 AM
  6. [SOLVED] Named ranges and combobox
    By bigfoot007 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-17-2012, 03:06 PM
  7. Populate a list box with named ranges.....
    By Chris Salcedo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-23-2005, 02:05 AM

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