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

capson

Board Regular
Joined
Jul 9, 2010
Messages
107
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
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,202
Office Version
  1. 365
Platform
  1. 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
11,723
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
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
73,202
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,903
Messages
5,834,312
Members
430,276
Latest member
legalcriminal015

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top