VBA/Excel dynamic list control

252 views Asked by At

I want to create pick list depend on what was provided on previous list but there is a problem. I was using list validation, but it was working until I didn't close file. After reopen this file, when the list was clear, it gives me an error and didn't want to load data into it.

So I decided to use control combo list, but there is another problem with creating an array...

    Sub RozwiƄ298_Zmienianie(Target)
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim m As Integer
    Dim lista() As Variant
    i = 1
    j = 1
    m = 0

    Do While Sheets("Dane").Cells(i, 14) <> Target.Text
        i = i + 1
    Loop
    j = i
    Do While Sheets("Dane").Cells(j, 14) = Target.Text
        j = j + 1
    Loop
    j = j - 1
    k = j - i + 1
    ReDim lista(k)
    For m = 0 To k
        lista(m) = Sheets("Dane").Cells(i, 15).Text
        i = i + 1
    Next m

    Sheets("Formularz").Shapes.Range(Array("Drop Down 298")).Select
    With Selection
        .List = lista
    End With
End Sub

Now 2 questions... Somehow it's working, but it give quite odd error after picking something from list( Argument not optional) - what I'm done wrong?

Second: How to set in Worksheet_Change(ByVal Target As Range) condition for picking something from this list and use picked data to create another pick list?

1

There are 1 answers

3
R.Katnaan On BEST ANSWER

Try by adding this piece of code. I already tested it. It work well.

Dim validationRange As Range

Sheets("Formularz").Cells(6, 2).Select

Set validationRange = Sheets("Dane").Range("P" & i & ":P" & j)

With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="='Dane'!" & validationRange.Address
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
End With