+ Reply to Thread
Results 1 to 9 of 9

dynamic named range not populating combo box list if range = single cell

Hybrid View

HeyInKy dynamic named range not... 10-23-2014, 05:06 PM
TMS Re: dynamic named range not... 10-23-2014, 05:55 PM
HeyInKy Re: dynamic named range not... 10-23-2014, 06:09 PM
HeyInKy Re: dynamic named range not... 10-23-2014, 06:13 PM
Norie Re: dynamic named range not... 10-23-2014, 06:19 PM
HeyInKy Re: dynamic named range not... 10-24-2014, 01:27 PM
Norie Re: dynamic named range not... 10-24-2014, 02:09 PM
HeyInKy Re: dynamic named range not... 10-24-2014, 04:28 PM
Norie Re: dynamic named range not... 10-24-2014, 05:27 PM
  1. #1
    Forum Contributor
    Join Date
    04-14-2013
    Location
    Ky
    MS-Off Ver
    Excel 2013
    Posts
    344

    dynamic named range not populating combo box list if range = single cell

    I think my error is more related to my Excel formula, but it's showing up when I run my VBA (and the solution may involve VBA) so I'm posting in this forum.

    I have 2 userforms that I use to enter names, Employee names and Supervisor Names. These get listed on the next available row of my "Admin Sheet", using something like:

    Private Sub btnSupAdd_Click()
      Dim ws2 As Worksheet: Set ws2 = Sheets("Admin Menu")
      Dim emptyRow As Long
      With ws2
        'Determine EmptyRow
        emptyRow = WorksheetFunction.CountA(Range("$E:E")) + 1
        Cells(emptyRow, 5).Value = SupNameTextBox.Value
      End With
      Unload Me
    End Sub
    My add employee name routine is similar, only it goes in column B. Because I have a header row, my named range for my supervisor list is:

    Formula: copy to clipboard
    =OFFSET('Admin Menu'!$E$2,0,0,COUNTA('Administrative Menu'!$E:$E)-1,1)


    The "-1" is to account for row 1, the header row, otherwise a blank value shows up in my named range (it would count the header plus all the values beneath in that column, which because my offset formula starts in $E$2 and not $E$1 and the COUNTA formula counts the entire column, $E:$E). Now, this formula works, and when I open my name manager and click into the "Refers to:" box when the named range ("SupList") is selected, I get the moving selection box around cell E2 thru however names are listed below in adjacent rows in column E. Even if there's just 1 name (in E2) and E3 is blank, the name manger recognizes that one cell as my range. If there are 2 names, one in E2 and another in E3, then it recognizes those 2 but not E4. Perfect.

    Here's the problem: When I try to populate a combo box with this named range, if there is only 1 cell in the range (E2), it does not read the value and does not populate with that single name! However, if there are 2 or more values (E2 and E3, plus how ever many more), it works fine and the entire named range is available. My code for populating is:

    Private Sub UserForm_Initialize()
      Dim ws As Worksheet
      On Error Resume Next
      Set ws = Worksheets("Admin Menu")
      Me.cbo1.List = ws.Range("SupList").Value
      'Etc...
    Similar problem with the employee list. If I change the named range formula to
    Formula: copy to clipboard
    =OFFSET('Admin Menu'!$E$2,0,0,COUNTA('Administrative Menu'!$E:$E),1)
    , the range then counts the blank cell after the last name, and includes that blank when the combo box is populated.

    Sorry I cannot provide a sample (too much sanitizing to do) - but is should be fairly simple to understand, hopefully...

    -HeyInKy
    Last edited by HeyInKy; 10-23-2014 at 05:17 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,167

    Re: dynamic named range not populating combo box list if range = single cell

    I prefer to use INDEX for Dynamic Named Ranges.

    This:
    Formula: copy to clipboard
    =Sheet4!$E$2:INDEX(Sheet4!$E:$E,MAX(COUNTA(Sheet4!$E:$E),2))


    will give you a single (empty) cell in the DNR even if there are no entries. One or more should give you the correct Named Range. I guess.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    04-14-2013
    Location
    Ky
    MS-Off Ver
    Excel 2013
    Posts
    344

    Re: dynamic named range not populating combo box list if range = single cell

    Apparently, from my research, the problem is the ".list" property... it will not recognize a single value. So the code "Me.cbo1.List = ws.Range("SupList").Value" will not return a list when that list is less than 2.

    There's a work around, but it creates another problem in some other areas of my code.

    Before each combobox that populates with a named dynamic range that may be a single value (some of my comboboxes are not dynamic and have more than 1 value), I can use code to count the rows in the range, and if = to 1, use the .value property instead, but if not, use the .list property to populate to combobox. Something along the lines of:

    Private Sub UserForm_Initialize()
      Dim Nrow As Long
    On Error Resume Next
      Nrow = Range("SupList").Rows.Count
      If Nrow = 1 Then
        Me.cbo5.Value = Range("SupList").Value
      Else
        Me.cbo5.List = Range("SupList").Value
      End If
    End Sub
    Now my problem is when I use this code to write or do something else with whatever is in my combobox, if it's a single value, it will not recognize it as a list. For example, elsewhere, I may use or refer to "cbo5.ListIndex" in a line of code, and there is not a list index if there is not a list!

    But at least I think I'm on the right track now...

  4. #4
    Forum Contributor
    Join Date
    04-14-2013
    Location
    Ky
    MS-Off Ver
    Excel 2013
    Posts
    344

    Re: dynamic named range not populating combo box list if range = single cell

    @TMS

    Thanks for the suggestion. I am out of time today but will look at this tomorrow. But if it returns a single cell (with or without value), I don't think the .list property will like it...

    -HeyInKy

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: dynamic named range not populating combo box list if range = single cell

    Do you use the named range anywhere else in the workbook?

    If you don't why not use code for it?
    Dim rngSupList As Range
    
        With  Sheets("Admin Menu")
            Set rngSupList = .Range("E2", .Range("E" & Rows.Count).End(xlUp))
        End If
    
        If rngSupList.Rows.Count = 1 Then
            Me.cbo5.AddItem rngSupList
        Else
            Me.cbo5.List = rngSupList
        End If
    If posting code please use code tags, see here.

  6. #6
    Forum Contributor
    Join Date
    04-14-2013
    Location
    Ky
    MS-Off Ver
    Excel 2013
    Posts
    344

    Re: dynamic named range not populating combo box list if range = single cell

    @Norie - I like that... how would I use that when I need to use the listindex property? For example, if my combobox is populated from the VBA range using "Me.cbo5.List", it's be fine, but if there is just a single cell value in the range and it populates using the .addItem property, then I'm not sure how to work with that (it's probably very simple, but I am still a newbie...)

    If use it in a routine to delete a selected name from the range, my specific code is something along the lines of (places where listindex) used are bolded in blue:

      If cbo5.ListIndex = -1 Then
        MsgBox "No supervisor selected."
      Else
        answer = MsgBox("Are you sure you want to delete " & cbo5.Value & "?", vbYesNo + vbExclamation, "Delete Supervisor")
        If answer = vbYes Then
          Range("SupList").Cells(cbo5.ListIndex + 1, 1).Delete xlShiftUp
          Range("A" & Cells.Rows.Count).End(xlUp).Select
          Selection.ClearContents
          MsgBox cbo5.Value & " has been deleted."
          Unload Me
        End If
      End If

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: dynamic named range not populating combo box list if range = single cell

    You would just use it in the same way, the only difference is that the listbox is only populated with one item.

    Setting the value of a listbox doesn't populate it with anything, try checking out the ListCount when SupList only has one row.
    Private Sub UserForm_Initialize()
    Dim Nrow As Long
        On Error Resume Next
        Nrow = Range("SupList").Rows.Count
        If Nrow = 1 Then
            Me.cbo5.Value = Range("SupList").Value
    
            MsgBox "Did the previous line populate the listbox with the single value from SupList?" & vbCrLf & vbCrLf & IIf(Me.cbo5.ListCount = 0, "No", "Yes")
    
        Else
            Me.cbo5.List = Range("SupList").Value
        End If
    End Sub

  8. #8
    Forum Contributor
    Join Date
    04-14-2013
    Location
    Ky
    MS-Off Ver
    Excel 2013
    Posts
    344

    Re: dynamic named range not populating combo box list if range = single cell

    @Norie, your the best!

    And my actual code was:

        With Sheets("Admin Menu")
            Set rngSupList = .Range("E2", .Range("E" & Rows.Count).End(xlUp))
        
        If rngSupList.Rows.Count = 1 Then
            Me.cbo7.AddItem rngSupList
        Else
            Me.cbo7.List = rngSupList.Value
        End If
        End With
    Norie, inadvertently I'm sure, had "End If" instead of "End With" - and to populate the list, I had to add ".Value" to the end of "rngSupList" But I got it working
    Last edited by HeyInKy; 10-24-2014 at 05:01 PM.

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: dynamic named range not populating combo box list if range = single cell

    HeyInKy

    Those were deliberate mistakes to promote learning, nothing to do with post-pub ramblings.

+ 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] Dynamic List From Single Column of Named Range Table (Permit Open/Close Bracket)
    By BoardGuy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-18-2014, 09:41 AM
  2. [SOLVED] Populating ComboBox using dynamic named range - Error
    By TC1980 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-16-2014, 05:42 AM
  3. Replies: 7
    Last Post: 01-24-2013, 06:55 PM
  4. Replies: 5
    Last Post: 07-27-2010, 10:58 AM
  5. Replies: 1
    Last Post: 06-16-2009, 09:42 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