Condividi:        

Tabella Pivot creazione e layout

Vuoi potenziare i tuoi documenti Word? Non sai come si fa una macro in Excel? Devi creare una presentazione in PowerPoint?
Oppure sei passato a OpenOffice e non sei sicuro di come lavorare al meglio?

Moderatori: Anthony47, Flash30005

Tabella Pivot creazione e layout

Postdi EnricoBanco » 17/01/18 23:29

Ciao Anthony, ciao a tutti

sono quattro giorni che cerco fare una pivot valida sempre e di aggiornare il layout. Provo e riprovo alla fine vado al topic
'http://www.pc-facile.com/forum/viewtopic.php?p=636355

Parola magica di Anthony: Selection.PivotTable

Ovviamente in questo caso, e' opportuno che il codice faccia riferimento non a una tabella specifica (come si faceva col codice ActiveSheet.Range("A5").PivotTable.etc etc) ma alla tabella selezionata; quindi usando
Selection.PivotTable.etc etc
Se la macro la registri allora il codice include normalmente qualcosa come ActiveSheet.PivotTables("Tabella_pivotN").Etc Etc
Per renderlo universale dovrai modificare la parte ActiveSheet.PivotTables("Tabella_pivotN") come da suggerimento precedente.


La soluzione era ad un passo. Posto il codice con un esempio assemblato con quanto ho trovato, testato e...Con la parola magica di Anthony Selection.PivotTable

Al posto di CAMPO, il nome campo che si vuole.

Grazie Anthony per la 1000ma volta!!!


Codice: Seleziona tutto
Sub Pivot_creazione_e_layout()


    'On Error Resume Next
    Sheets("Fogliodati ").Select
   
    Dim objTable As PivotTable, objField As PivotField
       
    Range("A1").Select
   
    Set objTable = Foglio1.PivotTableWizard
 
    ' Specify row and column fields.
    Set objField = objTable.PivotFields("CAMPO")
    objField.Orientation = xlRowField
    objField.Position = 1
   
    Set objField = objTable.PivotFields("CAMPO ")
    objField.Orientation = xlRowField
    objField.Position = 2
   
    Set objField = objTable.PivotFields("CAMPO ")
    objField.Orientation = xlRowField
    objField.Position = 3
   
    Set objField = objTable.PivotFields("CAMPO")
    objField.Orientation = xlColumnField
    objField.Position = 1
         
   
   
    ' Specify a data field with its summary
    ' function and format.
    Set objField = objTable.PivotFields("CAMPO ")
    objField.Orientation = xlDataField
    objField.Function = xlSum
    objField.NumberFormat = "$ #,##0"
   
   
    Set objField = objTable.PivotFields("CAMPO ")
    objField.Orientation = xlDataField
    objField.Function = xlSum
    objField.NumberFormat = "$ #,##0"
   
   
    Set objField = objTable.PivotFields("CAMPO ")
    objField.Orientation = xlDataField
    objField.Function = xlSum
    objField.NumberFormat = "$ #,##0"
   
       
    Set objField = objTable.PivotFields("CAMPO")
    objField.Orientation = xlDataField
    objField.Function = xlSum
    objField.NumberFormat = "$ #,##0"
   
    Cells.Select
    Selection.Style = "Comma"
   
    ' Specify a page field.
    'Set objField = objTable.PivotFields("CAMPO ")
    'objField.Orientation = xlPageField
   
    ' Preview the new PivotTable report.
    ' ActiveSheet.PrintPreview
   
    ' Prompt the user whether to delete the PivotTable.
    ' Application.DisplayAlerts = False
    ' If MsgBox("Delete the PivotTable?", vbYesNo) = vbYes Then
    ' ActiveSheet.Delete
    ' End If
    ' Application.DisplayAlerts = True
 
 
  With Selection.PivotTable.DataPivotField
        .Orientation = xlColumnField
        .Position = 2
    End With
   
   
   Selection.PivotTable.PivotFields("CAMPO "). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO " _
        ).Subtotals = Array(False, False, False, False, False, False, False, False, False, False _
        , False, False)
    Selection.PivotTable.PivotFields("CAMPO "). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO "). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO "). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO "). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO "). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO ").Subtotals = _
        Array(False, False, False, False, False, False, False, False, False, False, False, False)
    Selection.PivotTable.PivotFields("CAMPO "). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO "). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO ").Subtotals = _
        Array(False, False, False, False, False, False, False, False, False, False, False, False)
    Selection.PivotTable.PivotFields("CAMPO ").Subtotals _
        = Array(False, False, False, False, False, False, False, False, False, False, False, False _
        )
    Selection.PivotTable.PivotFields( _
        " CAMPO ").Subtotals = Array(False, False, False, _
        False, False, False, False, False, False, False, False, False)
    Selection.PivotTable.PivotFields( _
        " CAMPO ").Subtotals = Array(False, False, False, False _
        , False, False, False, False, False, False, False, False)
    Selection.PivotTable.PivotFields( _
        " CAMPO ").Subtotals = Array(False, False, False, _
        False, False, False, False, False, False, False, False, False)
    Selection.PivotTable.PivotFields("CAMPO " _
        ).Subtotals = Array(False, False, False, False, False, False, False, False, False, False _
        , False, False)
    Selection.PivotTable.PivotFields("CAMPO "). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO "). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO "). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO "). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.RepeatAllLabels xlRepeatLabels
 
    Cells.Select
    Selection.Style = "Comma"
 
End Sub
EnricoBanco
Utente Junior
 
Post: 77
Iscritto il: 18/07/17 06:29

Sponsor
 

Re: Tabella Pivot creazione e layout

Postdi Anthony47 » 17/01/18 23:52

Hummm.... Voglio illudermi che hai postato una soluzione che hai potuto finalizzare anche grazie al suggerimento trovato nella discussione che hai linkato.

Quindi "grazie per aver condiviso la tua esperienza"

Se invece c'e' anche una richiesta di aiuto che non ho voluto vedere, beh ci pensero' domani in tarda mattinata :D

Ciao
Avatar utente
Anthony47
Moderatore
 
Post: 19181
Iscritto il: 21/03/06 16:03
Località: Ivrea

Re: Tabella Pivot creazione e layout

Postdi EnricoBanco » 19/01/18 19:02

Si, si, funziona grazie alla tua parola magica :-)
EnricoBanco
Utente Junior
 
Post: 77
Iscritto il: 18/07/17 06:29

Re: Tabella Pivot creazione e layout

Postdi EnricoBanco » 24/01/18 12:22

Ho fatto un pò di aggiunte scartabellando per il web e testando il tutto: gestione tabella se già esiste oppure no e nome pivot

Codice: Seleziona tutto
Sub CreatePivot()

'Funziona
'http://www.pc-facile.com/forum/viewtopic.php?p=636355
    'On Error Resume Next
    'ActiveWorkbook.Sheets("FoglioDati").Select
   
    Application.DisplayAlerts = False
    If MsgBox("Crea tabella Pivot", vbYesNo) = vbYes Then
    GoTo CreaPivot
    If vbNo Then
    GoTo Fine
    End If
    Application.DisplayAlerts = True
     
   
CreaPivot:

Dim foglio As Worksheet
For Each foglio In Worksheets
If foglio.Name = "Pivot" Then
Sheets("Pivot").Delete
End If
Next foglio

Sheets("FoglioDati").Select
   
   
    ' Creates a PivotTable report from the table on Sheet1
    ' by using the PivotTableWizard method with the PivotFields
    ' method to specify the fields in the PivotTable.
    Dim objTable As PivotTable, objField As PivotField
   
    ' Select the sheet and first cell of the table that contains the data.
   
    Range("A1").Select
   
    ' Create the PivotTable object based on the Employee data on Sheet1.
    Set objTable = Foglio1.PivotTableWizard
   
   
    ActiveSheet.Name = "Pivot"    'Rinomina foglio della tabella pivot

   
    ' Specify row and column fields.
    Set objField = objTable.PivotFields("CAMPO")
    objField.Orientation = xlRowField
    objField.Position = 1
   
    Set objField = objTable.PivotFields("CAMPO ")
    objField.Orientation = xlRowField
    objField.Position = 2
   
    Set objField = objTable.PivotFields("CAMPO ")
    objField.Orientation = xlRowField
    objField.Position = 3
   
    Set objField = objTable.PivotFields("CAMPO ")
    objField.Orientation = xlColumnField
    objField.Position = 1
         
   
   
    ' Specify a data field with its summary
    ' function and format.
    Set objField = objTable.PivotFields("CAMPO ")
    objField.Orientation = xlDataField
    objField.Function = xlSum
    objField.NumberFormat = "$ #,##0"
   
   
    Set objField = objTable.PivotFields("CAMPO ")
    objField.Orientation = xlDataField
    objField.Function = xlSum
    objField.NumberFormat = "$ #,##0"
   
   
    Set objField = objTable.PivotFields("CAMPO ")
    objField.Orientation = xlDataField
    objField.Function = xlSum
    objField.NumberFormat = "$ #,##0"
   
       
    Set objField = objTable.PivotFields("CAMPO ")
    objField.Orientation = xlDataField
    objField.Function = xlSum
    objField.NumberFormat = "$ #,##0"
   
    Cells.Select
    Selection.Style = "Comma"
   
    ' Specify a page field.
    'Set objField = objTable.PivotFields("CAMPO ")
    'objField.Orientation = xlPageField
   
    ' Preview the new PivotTable report.
    ' ActiveSheet.PrintPreview
   
    ' Prompt the user whether to delete the PivotTable.
    ' Application.DisplayAlerts = False
    ' If MsgBox("Delete the PivotTable?", vbYesNo) = vbYes Then
    ' ActiveSheet.Delete
    ' End If
    ' Application.DisplayAlerts = True
 
 
  With Selection.PivotTable.DataPivotField
        .Orientation = xlColumnField
        .Position = 2
    End With
   
   
   Selection.PivotTable.PivotFields("CAMPO"). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO" _
        ).Subtotals = Array(False, False, False, False, False, False, False, False, False, False _
        , False, False)
    Selection.PivotTable.PivotFields("CAMPO"). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO"). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO"). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO"). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO"). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO").Subtotals = _
        Array(False, False, False, False, False, False, False, False, False, False, False, False)
    Selection.PivotTable.PivotFields("CAMPO"). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO"). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO").Subtotals = _
        Array(False, False, False, False, False, False, False, False, False, False, False, False)
    Selection.PivotTable.PivotFields("CAMPO").Subtotals _
        = Array(False, False, False, False, False, False, False, False, False, False, False, False _
        )
    Selection.PivotTable.PivotFields( _
        " CAMPO").Subtotals = Array(False, False, False, _
        False, False, False, False, False, False, False, False, False)
    Selection.PivotTable.PivotFields( _
        " CAMPO ").Subtotals = Array(False, False, False, False _
        , False, False, False, False, False, False, False, False)
    Selection.PivotTable.PivotFields( _
        " CAMPO").Subtotals = Array(False, False, False, _
        False, False, False, False, False, False, False, False, False)
    Selection.PivotTable.PivotFields("CAMPO" _
        ).Subtotals = Array(False, False, False, False, False, False, False, False, False, False _
        , False, False)
    Selection.PivotTable.PivotFields("CAMPO"). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO"). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO"). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.PivotFields("CAMPO"). _
        Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
        False, False)
    Selection.PivotTable.RepeatAllLabels xlRepeatLabels
 
    Cells.Select
    Selection.Style = "Comma"
 
Fine:
  Exit Sub
 
  End If

End Sub
 
 



EnricoBanco
Utente Junior
 
Post: 77
Iscritto il: 18/07/17 06:29


Torna a Applicazioni Office Windows


Topic correlati a "Tabella Pivot creazione e layout":


Chi c’è in linea

Visitano il forum: Nessuno e 38 ospiti