Macro funcionar somente com botão "ToggleButton" pressionado

fabianolordelo

28-08-2014 20:20:22

Boa tarde,

Tenho o seguinte código que seleciona toda linha da planilha:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error Resume Next
y = Selection.Column
x = Selection.Row

If Selection.Rows.Count + Selection.Columns.Count > 2 Then
Exit Sub
Else
Selection.EntireRow.Select
Cells(x, y).Activate
End If

End Sub

Agora gostaria que esse código funcionasse somente enquanto eu estiver com um botão "ToggleButton" pressionado. E voltasse ao normal quando ele não estiver pressionado. Algum pode me ajudar ?

Rafael Monteiro

29-08-2014 11:21:30

Tem que adicionar uma condição. Seria assim:

Código: Selecionar todos

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Plan1.ToggleButton1.Value = True Then
On Error Resume Next
y = Selection.Column
x = Selection.Row
If Selection.Rows.Count + Selection.Columns.Count > 2 Then
Exit Sub
Else
Selection.EntireRow.Select
Cells(x, y).Activate
End If
Else
MsgBox "Função desativada!"
End If
End Sub
Certifique-se de que o botão esteja na plan1. Caso esteja em outra plan altere no código.

fabianolordelo

29-08-2014 11:44:24

Perfeito Rafael.

Muito obrigado.