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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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
 
Upvote 0
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
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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
Back
Top