Ir ao conteúdo

Excel código de VBA em minha pasta de trabalho dando erro


Ir à solução Resolvido por AfonsoMira,

Posts recomendados

Postado

Boa noite

tenho estes dois códigos em esta pasta de trabalho e eles funcionam perfeitamente, mas precisei inserir o terceiro código mas ele dá erro por ter uma linha em ambos os códigos que estão com o mesmo nome.

os dois abaixo são os que já estavam na planilha e funcionam perfeito

 

Option Explicit

Private Sub Workbook_Open()
ActiveWindow.DisplayWorkbookTabs = False ' Oculta o Diiplay c/ os nomes das abas, ao abrir
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
 Dim LR As Long, X As String

  If Target.Count > 1 Then Exit Sub

  If Target.Column = 10 And Target.Value = "A" Then X = "Itau - Alimenta"
  If Target.Column = 10 And Target.Value = "G" Then X = "BRADESCO - Golden"
  
  If X <> vbNullString Then
   With Sheets(X)
    LR = .Cells(4501, 1).End(3).Row
    On Error GoTo res
    Application.EnableEvents = False
    .Cells(LR + 1, 1).Resize(, 5).Value = Cells(Target.Row, 2).Resize(, 5).Value
    .Cells(LR + 1, 😎 = Cells(Target.Row, 9)
    .Cells(LR + 1, 10).Resize(, 2).Value = Cells(Target.Row, 11).Resize(, 2).Value
res:
    Application.EnableEvents = True
   End With
  End If
  
End Sub
 

 

MAS QUANDO COLOCO ESTE ABAIXO, DÁ ERRO, PORQUE AMBOS POSSUEM O MESMO NOME

ENTEI COLOCAR um ponto  EM UM DELES MAS NÃO RESOLVEU.

 

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
 Dim UL As Long
  If Sh.Name = "Itau - Alimenta" Or Target.Count > 1 Then Exit Sub
  If Target.Column <> 10 Or Target.Value <> "C" Then Exit Sub
  Application.ScreenUpdating = False
  With Sheets("Itau - Alimenta")
   UL = .Cells(4500, 1).End(xlUp).Row
   Cells(Target.Row, 2).Resize(, 5).Copy .Cells(UL + 1, 1)
   Cells(Target.Row, 8).Resize(, 5).Copy .Cells(UL + 1, 7)
  End With
End Sub

Alguém poderia me ajudar a corrigir este erro?

 

 

  • Solução
Postado

Boas @Osmarbg ,
Experimente assim:


 

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'Primeira Macro 
Dim LR As Long, X As String

  If Target.Count > 1 Then Exit Sub

  If Target.Column = 10 And Target.Value = "A" Then X = "Itau - Alimenta"
  If Target.Column = 10 And Target.Value = "G" Then X = "BRADESCO - Golden"
  
  If X <> vbNullString Then
   With Sheets(X)
    LR = .Cells(4501, 1).End(3).Row
    On Error GoTo res
    Application.EnableEvents = False
    .Cells(LR + 1, 1).Resize(, 5).Value = Cells(Target.Row, 2).Resize(, 5).Value
    .Cells(LR + 1, 😎 = Cells(Target.Row, 9)
    .Cells(LR + 1, 10).Resize(, 2).Value = Cells(Target.Row, 11).Resize(, 2).Value
res:
    Application.EnableEvents = True
   End With
  End If

'Segunda Macro
Dim UL As Long
  If Sh.Name = "Itau - Alimenta" Or Target.Count > 1 Then Exit Sub
  If Target.Column <> 10 Or Target.Value <> "C" Then Exit Sub
  Application.ScreenUpdating = False
  With Sheets("Itau - Alimenta")
   UL = .Cells(4500, 1).End(xlUp).Row
   Cells(Target.Row, 2).Resize(, 5).Copy .Cells(UL + 1, 1)
   Cells(Target.Row, 8).Resize(, 5).Copy .Cells(UL + 1, 7)
  End With
  
End Sub



Ps. Não testei aqui 🙂

  • Curtir 1

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...

LANÇAMENTO!

eletronica2025-popup.jpg


CLIQUE AQUI E BAIXE AGORA MESMO!