Ir ao conteúdo
  • Cadastre-se

Excel unificar códigos vba (worksheet_change)


Posts recomendados

Poderiam me ajudar com unificar esse dois Worksheet_Change(ByVal Target As Range) para mesma planilha:

 

PRIMEIRO CÓDIGO

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Tratar

Dim campoAlt As String
Dim valorDig As String
Dim linha As Long

linha = Sheets("Registro").Range("A" & Rows.Count).End(xlUp).Row + 1

valorDig = Target.Value
campoAlt = Target.Address

With Sheets("Registro")
    .Cells(linha, 1) = Now()
    .Cells(linha, 2) = Application.UserName
    .Cells(linha, 3) = valorDig
    .Cells(linha, 4) = ActiveSheet.Name
    .Cells(linha, 5) = campoAlt
End With

Tratar:
Exit Sub

 

SEGUNDO CÓDIGO:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Cells(Target.Row + Target.Rows.Count, Target.Item(1, 1).Column).ID = Target.Address
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
Dim nlin As Integer
Dim wsRegistro As Worksheet
Set wsRegistro = ThisWorkbook.Sheets("Registro")
Dim LinhaRegistro As Long
LinhaRegistro = wsRegistro.Cells(wsRegistro.Rows.Count, "A").End(xlUp).Row + 1
nlin = Target.Row
If Target.Item(1, 1).ID <> "" Then
If Target.Rows.Count > 1 Then
wsRegistro.Cells(LinhaRegistro, 1).Value = Now()
wsRegistro.Cells(LinhaRegistro, 2).Value = "Linhas " & nlin & " à " & nlin + Target.Rows.Count & " excluída"
Else
wsRegistro.Cells(LinhaRegistro, 1).Value = Now()
wsRegistro.Cells(LinhaRegistro, 2).Value = "Linha " & nlin & " excluída"
End If
Else
If Target.Rows.Count > 1 Then
wsRegistro.Cells(LinhaRegistro, 1).Value = Now()
wsRegistro.Cells(LinhaRegistro, 2).Value = "Linhas " & nlin & " à " & nlin + Target.Rows.Count & " inserídas"
Else
wsRegistro.Cells(LinhaRegistro, 1).Value = Now()
wsRegistro.Cells(LinhaRegistro, 2).Value = "Linha " & nlin & " inserídas"
End If
End If
Target.Item(1, 1).ID = ""
Cells(Target.Row + Target.Rows.Count, Target.Item(1, 1).Column).ID = Target.Address
End Sub

 

Link para o comentário
Compartilhar em outros sites

Basicamente é só copiar um dos procedimentos e deixar tudo no mesmo.

 

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Tratar

Dim campoAlt As String
Dim valorDig As String
Dim linha As Long
Dim nlin As Integer
Dim wsRegistro As Worksheet
Dim LinhaRegistro As Long

linha = Sheets("Registro").Range("A" & Rows.Count).End(xlUp).Row + 1

valorDig = Target.Value
campoAlt = Target.Address

With Sheets("Registro")
    .Cells(linha, 1) = Now()
    .Cells(linha, 2) = Application.UserName
    .Cells(linha, 3) = valorDig
    .Cells(linha, 4) = ActiveSheet.Name
    .Cells(linha, 5) = campoAlt
End With

Set wsRegistro = ThisWorkbook.Sheets("Registro")
LinhaRegistro = wsRegistro.Cells(wsRegistro.Rows.Count, "A").End(xlUp).Row + 1
nlin = Target.Row
If Target.Item(1, 1).ID <> "" Then
If Target.Rows.Count > 1 Then
wsRegistro.Cells(LinhaRegistro, 1).Value = Now()
wsRegistro.Cells(LinhaRegistro, 2).Value = "Linhas " & nlin & " à " & nlin + Target.Rows.Count & " excluída"
Else
wsRegistro.Cells(LinhaRegistro, 1).Value = Now()
wsRegistro.Cells(LinhaRegistro, 2).Value = "Linha " & nlin & " excluída"
End If
Else
If Target.Rows.Count > 1 Then
wsRegistro.Cells(LinhaRegistro, 1).Value = Now()
wsRegistro.Cells(LinhaRegistro, 2).Value = "Linhas " & nlin & " à " & nlin + Target.Rows.Count & " inserídas"
Else
wsRegistro.Cells(LinhaRegistro, 1).Value = Now()
wsRegistro.Cells(LinhaRegistro, 2).Value = "Linha " & nlin & " inserídas"
End If
End If
Target.Item(1, 1).ID = ""
Cells(Target.Row + Target.Rows.Count, Target.Item(1, 1).Column).ID = Target.Address

Tratar:
End Sub

 

Link para o comentário
Compartilhar em outros sites

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

 

GRÁTIS: ebook Redes Wi-Fi – 2ª Edição

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!