Página 1 de 1

Atualizar tamanho da tabela de acordo com uma primeira tabel

Enviado: 18 Dez 2015 às 13:44
por rafak1ngz
Olá,

Estava precisando de algumas ajudas e criei um tópico (link abaixo), onde o Parkeless conseguiu resolver meu problema.
http://gurudoexcel.com/forum/viewtopic.php?f=7&t=1061

Como não conheço sobre VBA, ele também me explicou e consegui entender um pouco do que ele desenvolveu.

A planilha que estava (que ele mechou para mim) está assim:

Imagem
A coluna "EMPRESA", quando adiciona ou altera alguém, ele atualiza a tabela que está na aba "SITUAÇÃO FINANCEIRA"
Imagem
Em ambos os casos, as colunas "EMPRESA" está na célula B2.

O código macro que ele me mandou foi esse:
Código: Selecionar todos
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
On Error GoTo Erro
If Target.Column = 2 Then
    Application.ScreenUpdating = False
    If Target = "" Or Target.Row <> Sheets(2).Range("B1000000").End(xlUp).Row Then
        Sheets(2).ListObjects("Tabela5").Resize Range("$B$2:$N$" & Sheets(1).Range("B1000000").End(xlUp).Row & "")
    End If
    Cells(Target.Offset(1, 0).Row, 2).Select
        Application.ScreenUpdating = True

End If
Exit Sub


Erro:
MsgBox "Aviso: as fórmulas da aba " & Sheets(2).Name & " não puderam ser atualizada automaticamente!", vbExclamation

End Sub
Até ai está funcionando tudo direito! O problema começou quando fiz o seguinte:
Fui personalizar a planilha, colocando cabeçalho e etc.

Após adicionar cabeçalho, a tabela desceu algumas linhas, ficando agora em B7:

Imagem
Imagem

Após fazer isso, tentei editar o VBA em base ao que o Parkeless explicou, ficou assim:
Código: Selecionar todos
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
On Error GoTo Erro
If Target.Column = 2 Then
    Application.ScreenUpdating = False
    If Target = "" Or Target.Row <> Sheets(3).Range("B1000000").End(xlUp).Row Then
        Sheets(3).ListObjects("TabelaSITUACAO").Resize Range("$B$7:$N$" & Sheets(1).Range("B1000000").End(xlUp).Row & "")
    End If
    If Target = "" Or Target.Row <> Sheets(4).Range("B1000000").End(xlUp).Row Then
        Sheets(4).ListObjects("TabelaJan").Resize Range("$B$7:$N$" & Sheets(1).Range("B1000000").End(xlUp).Row & "")
    End If
    Cells(Target.Offset(1, 0).Row, 7).Select
        Application.ScreenUpdating = True

End If
Exit Sub



Erro:
MsgBox "Aviso: as fórmulas da aba " & Sheets(3).Name & " não puderam ser atualizada automaticamente!", vbExclamation

End Sub
Não sei se se está certo, mas mesmo assim, se eu adicionar um novo cliente, ele não expande a tabela na outra tabela.

Se tiver alguma dúvida, vou encaminhar a tabela em anexo.

Abraços

Atualizar tamanho da tabela de acordo com uma primeira tabel

Enviado: 18 Dez 2015 às 14:34
por Parkeless
Olá novamente Rafa!

Tenta isso:
Código: Selecionar todos
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
'On Error GoTo Erro
On Error GoTo 0
If Target.Column = 2 Then
    Application.ScreenUpdating = False
    If Target = "" Or Target.Row <> Sheets(3).Range("B1000000").End(xlUp).Row Then
        Sheets("SITUAÇÃO FINANCEIRA").ListObjects("TabelaSITUACAO").Resize Range("$B$7:$N$" & Sheets("CLIENTE").Range("B1000000").End(xlUp).Row & "")
    End If
    If Target = "" Or Target.Row <> Sheets(4).Range("B1000000").End(xlUp).Row Then
        Sheets("JAN").ListObjects("TabelaJan").Resize Range("$B$7:$N$" & Sheets("CLIENTE").Range("B1000000").End(xlUp).Row & "")
    End If
    Cells(Target.Offset(1, 0).Row, 7).Select
        Application.ScreenUpdating = True

End If
Exit Sub



Erro:
MsgBox "Aviso: as fórmulas da aba SITUAÇÃO FINANCEIRA não puderam ser atualizada automaticamente!", vbExclamation

End Sub
No código anterior, eu tinha referenciado as abas como números ( sheets(1) ); como você criou novas abas, essa ordem mudou, então o Excel estava se perdendo. Alterei o código indicando o nome das abas, ao invés do seu número.

Atualizar tamanho da tabela de acordo com uma primeira tabel

Enviado: 18 Dez 2015 às 20:12
por rafak1ngz
Deu para entender o que fez! Mais uma vez me salvando!
Desculpa ficar toda hora enchendo o saco, estou lendo um pouco sobre o assunto, tentar aprender um pouco sobre essa linguagem.
Muito obrigado pela ajuda! :D

Abraços!