Adaptação Macro

salvador

20-03-2016 12:55:41

Bom Dia Amigos;

Preciso de uma ajuda simples de vocês.
O código abaixo, quando clicado com o botão direito do mouse sobre uma célula, ele copia os valores dessa linha para uma outra planilha que alimenta um gráfico.

Só que eu queria que o botão direito funcionasse apenas na coluna 7 dessa planilha, para essa macro. Como posso adapatar?

Vide código:

Código: Selecionar todos

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.EnableCancelKey = xlDisabled



Dim I As Long

   For I = 8 To 19
       Plan13.Cells(13, I).Value = Plan2.Cells(ActiveCell.Row, I).Value
       If Plan13.Cells(13, I).Value = Empty Then Plan13.Cells(13, I).Value = 0
   Next I
   
   Plan13.Range("G5").Value = ActiveCell.Value

   Plan13.Select
      Cancel = True


Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub


End Sub

Eu tente fazer da seguinte maneia, mas nao deu certo. Vide:

Código: Selecionar todos

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.EnableCancelKey = xlDisabled



    With ActiveCell
        If .Row < 4 And .Row > 98 Then Validate = False
        If .Column < 7 And .Column > 7 Then Validate = False
    End With


    If Not Validate Then
        Exit Sub
    End If


Dim I As Long

   For I = 8 To 19
       Plan13.Cells(13, I).Value = Plan2.Cells(ActiveCell.Row, I).Value
       If Plan13.Cells(13, I).Value = Empty Then Plan13.Cells(13, I).Value = 0
   Next I
   
   Plan13.Range("G5").Value = ActiveCell.Value

   Plan13.Select
      Cancel = True


Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub


End Sub

Obrigado pela ajuda, mais uma vez!

webmaster

21-03-2016 19:51:04

Nao li o codigo todo, mas acho que se substituir isso:

Código: Selecionar todos

With ActiveCell
        If .Row < 4 And .Row > 98 Then Validate = False
        If .Column < 7 And .Column > 7 Then Validate = False
    End With


    If Not Validate Then
        Exit Sub
    End If
Por isso:

Código: Selecionar todos

If ActiveCell.Column <> 7 Then Exit Sub
Deve funcionar

salvador

25-03-2016 12:10:49

Amigo, bom dia!

Achei que havia dado certo, mas não deu não. O que eu quero fazer é trabalhar com duas macros no evento do botão direito do mouse:
1 - Quando eu clicar com o botão direito em qualquer célula da coluna 7, ele vai executar a primeira macro, que vai me gerar um gráfico numa outra planilha.
2 - Quando eu clicar com o botão direito sobre o range H7:S98 vai aparecer um botão Gerar Analitico, que vai filtrar os dados numa outra planilha!

Na verdade as duas macros funcionam a contento... só não consigo juntar as duas. Veja o que estou fazendo:

Código: Selecionar todos

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, cancel As Boolean)


Application.ScreenUpdating = False
Application.EnableEvents = False
Application.EnableCancelKey = xlDisabled


If ActiveCell.Column = 7 Then


' ESSSA MACRO COPIA OS VALORES DA LINHA PARA UMA OUTRA PLANILHA E GERAR UM GRÁFICO

Dim I As Long

   For I = 8 To 19
       Plan13.Cells(13, I).Value = Plan2.Cells(ActiveCell.Row, I).Value
       If Plan13.Cells(13, I).Value = Empty Then Plan13.Cells(13, I).Value = 0
   Next I

   Plan13.Range("G5").Value = ActiveCell.Value

   Plan13.Select
      cancel = True


Else

' ESSA MACRO GERA UM NOVO ITEM NO MENU DO BOTÃO DIREITO E EXECUTA UMA OUTRA MACRO PARA FILTRAR OS DADOS

For Each vSaber In Application.CommandBars("cell").Controls
    If vSaber.Tag = "sbx" Then vSaber.Delete
Next vSaber

If Not Application.Intersect(Target, Range("H7:S98")) Is Nothing Then
   With Application.CommandBars("cell").Controls _
       .Add(Type:=msoControlButton, before:=21, _
          temporary:=True)
       .Caption = "--->>>>> GERAR CONTA ANALITICA <<<<<---"
       .OnAction = "Filtra_Valores"
       .Tag = "sbx"
       .FaceId = 901
 End With
End If



End If



Application.EnableEvents = True
Application.ScreenUpdating = True



End Sub
Se puder me ajudar, ficarei imensamente grato!