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
 

Some videos you may like

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,755
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
10,097
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
52,755
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,122,233
Messages
5,594,964
Members
413,955
Latest member
FalcoDaz

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