VBA excel perform action from dropdown selection

Asked

Viewed 154 times

0

Talk people, good afternoon, all right?

I came to ask for help from the university because I’ve been racking my brain for days and I can’t seem to solve my problem.

I basically have this table:

inserir a descrição da imagem aqui

By clicking the re-register button, automatically the first list (B2) opens.

inserir a descrição da imagem aqui

What I wanted is that the moment I select some option of this dropdown that opens automatically, the next (C2) also opens automatically, at the time of selecting B2.

I’m using this in VBA to perform the opening of the first dropdown:

Sub AdicionaConta()
  Range("a2").EntireRow.Insert
  Range("B2").Select
  SendKeys "%{down}"    
End Sub

Thanks in advance, a hug!

1 answer

0


Hail!

The event that detects changes in a given field is the Change of the object Worksheet.

If the routine AdicionaConta is already in object code Worksheet of this specific worksheet, just add the event, for example:

Private Sub Worksheet_Change(ByVal Target As Range) ' O evento nos traz o objeto target, do tipo Range, que é a Range que foi alterada
    If Target.Cells.Count = 1 And Target.Column = 2 Then ' Se foi alterada uma célula só E isso ocorreu na coluna 2 (que é a B), então faça o seguinte:
        Range("C2").Select ' Coloquei o número 2 porque foi a linha que você utilizou como exemplo, mas imagino que você vá usar ali uma variável com o número da linha que você tem que ajustar.
        SendKeys "%{down}"
    End If
End Sub
  • Thank you very much, Caesar, that’s exactly what I was trying to do. Now next step is to do for column D, but not all options in column C have options, so I believe you need to do another if.

  • 1

    Rectifying, I’ve done it!!! Thank you very much for your help Caesar!!

  • Mark my answer as the correct one, then.

  • 1

    Oops, I thought I couldn’t get by being a new user. It’s already been checked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.