Drop-down filled dynamically from range how to clear drop-down if range empty

capson

Board Regular
Joined
Jul 9, 2010
Messages
104
I have a dynamic Drop-down being populated by the Headers from Range("G1:AZ1") when a button is clicked, there could be any number of headers in this range

If there are No headers in this range i.e. all cells are empty then I need to clear the drop-down of the values it has.

But I am having troubles doing this

Thank you for any help on this

Code:
Sub setupDV()
    Dim rSource As Range, rDV As Range, r  As Range, csString As String, v As String
    Dim c As Collection


    Set rSource = Sheets("Upload").Range("G1:AZ1")
    Set rDV = Sheets("List").Range("A2")
    Set c = New Collection
    csString = ""
    On Error Resume Next
    For Each r In rSource
        v = r.value
        If v <> "" Then
            c.Add v, CStr(v)
            If Err.Number = 0 Then
                If csString = "" Then
                    csString = v
                Else
                    csString = csString & "," & v
                End If
            Else
                Err.Number = 0
            End If
        End If
    Next r
    On Error GoTo 0
   
    With rDV.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:=csString
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = False
    End With
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,332
Office Version
365
Platform
Windows
How about
Code:
Sub setupDV()
   Dim cl As Range, rDV As Range
   Dim St As String
   
   Set rDV = Sheets("List").Range("A2")
   With CreateObject("scripting.dictionary")
      For Each cl In Sheets("Upload").Range("G1:AZ1")
         If cl.Value <> "" Then .Item(cl.Value) = Empty
      Next cl
      St = join(.Keys, ",")
   End With
   With rDV.Validation
      .Delete
      If St <> "" Then
         .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:=csString
         .IgnoreBlank = True
         .InCellDropdown = True
         .InputTitle = ""
         .ErrorTitle = ""
         .InputMessage = ""
         .ErrorMessage = ""
         .ShowInput = True
         .ShowError = False
      End If
   End With
End Sub
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,536
Try:
Code:
Sub test()
    If WorksheetFunction.CountA(Range("G1:AZ1")) = 0 Then
        Range("A2").Validation.Delete
    Else
        With Range("A2")
            With .Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=$G$1:$AZ$1"
            End With
        End With
    End If
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,332
Office Version
365
Platform
Windows
Glad we could help & thanks for the feedback
 

Forum statistics

Threads
1,082,373
Messages
5,365,070
Members
400,821
Latest member
kezza123

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top