Este fórum está sendo desativado

Depois de 9 anos, este fórum será desativado. Mas calma.... estamos migrando para uma comunidade no DISCORD. Junte-se a nós.

ENTRAR PARA DISCORD

Tópicos relacionados a códigos VBA, gravação de macros, etc.
  • Avatar do usuário
Por AlissonHenrique89
#39236
Boa noite, pessoal.

Preciso criar uma tabela dinâmica via VBA, pois tenho que comparar o relatório de vendas no nosso sistema com o relatório do sistema do cliente. A tabela dinâmica é necessária porque nosso sistema segmenta alguns dados e o relatório do cliente não. Por isso, uso a tabela dinâmica para agrupar os dados do nosso sistema.

Como sou noob em VBA ainda, gravei uma macro da montagem da tabela dinâmica. O problema é que como são empresas diferentes, a quantidade de colunas é fixo mas a quantidade de linhas mudam.

Então quando executo o código em uma tabela que contenha mais linhas, algumas linhas são omitidas na tabela dinâmica e quando a tabela tem menos linhas fica um campo escrito "(vazio)" na tabela dinâmica.

Eu preciso de uma correção no código para que a tabela dinâmica utilize somente a área com dados da tabela original.

Agradeço quem puder me ajudar.

Segue um exemplo do código que estou utilizando e planilha.
Código: Selecionar todos
Sub Macro3()
'
' Macro3 Macro
'

'
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Planilha1!R1C1:R6C9", Version:=6).CreatePivotTable TableDestination:= _
        "Planilha1!R1C12", TableName:="Tabela dinâmica3", DefaultVersion:=6
    Sheets("Planilha1").Select
    Cells(1, 12).Select
       With ActiveSheet.PivotTables("Tabela dinâmica3").PivotCache
        .RefreshOnFileOpen = False
        .MissingItemsLimit = xlMissingItemsDefault
    End With
    ActiveSheet.PivotTables("Tabela dinâmica3").RepeatAllLabels xlRepeatLabels
    With ActiveSheet.PivotTables("Tabela dinâmica3").PivotFields("Loja")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("Tabela dinâmica3").PivotFields("Filial")
        .Orientation = xlRowField
        .Position = 2
    End With
    With ActiveSheet.PivotTables("Tabela dinâmica3").PivotFields("Data")
        .Orientation = xlRowField
        .Position = 3
    End With
    With ActiveSheet.PivotTables("Tabela dinâmica3").PivotFields("Nota")
        .Orientation = xlRowField
        .Position = 4
    End With
    With ActiveSheet.PivotTables("Tabela dinâmica3").PivotFields("Serie")
        .Orientation = xlRowField
        .Position = 5
    End With
    ActiveSheet.PivotTables("Tabela dinâmica3").AddDataField ActiveSheet. _
        PivotTables("Tabela dinâmica3").PivotFields("Valor"), "Soma de Valor", xlSum
    With ActiveSheet.PivotTables("Tabela dinâmica3").PivotFields("Chave")
        .Orientation = xlRowField
        .Position = 6
    End With
    Range("L2").Select
    ActiveSheet.PivotTables("Tabela dinâmica3").PivotFields("Loja").Subtotals = _
        Array(False, False, False, False, False, False, False, False, False, False, False, False)
    ActiveSheet.PivotTables("Tabela dinâmica3").PivotFields("Loja").LayoutForm = _
        xlTabular
   
    Range("M2").Select
    ActiveSheet.PivotTables("Tabela dinâmica3").PivotFields("Filial").Subtotals = _
        Array(False, False, False, False, False, False, False, False, False, False, False, False)
    ActiveSheet.PivotTables("Tabela dinâmica3").PivotFields("Filial").LayoutForm = _
        xlTabular
    Range("N2").Select
    ActiveSheet.PivotTables("Tabela dinâmica3").PivotFields("Data").Subtotals = _
        Array(False, False, False, False, False, False, False, False, False, False, False, False)
    ActiveSheet.PivotTables("Tabela dinâmica3").PivotFields("Data").LayoutForm = _
        xlTabular
    Range("O2").Select
    ActiveSheet.PivotTables("Tabela dinâmica3").PivotFields("Nota").Subtotals = _
        Array(False, False, False, False, False, False, False, False, False, False, False, False)
    ActiveSheet.PivotTables("Tabela dinâmica3").PivotFields("Nota").LayoutForm = _
        xlTabular
    Range("P2").Select
    ActiveSheet.PivotTables("Tabela dinâmica3").PivotFields("Serie").Subtotals = _
        Array(False, False, False, False, False, False, False, False, False, False, False, False)
    ActiveSheet.PivotTables("Tabela dinâmica3").PivotFields("Serie").LayoutForm = _
        xlTabular
End Sub
Você não está autorizado a ver ou baixar esse anexo.
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]

Estamos migrando para uma comunidade no Discord