Ciao Draleo,
Chiedo scusa per la mia svista!
In inglese si dice: More haste, less speed (più fretta, meno velocità...):
Qui segue il mio adattamento del tuo codice:
'====================
Dim myVar As Variant <<=== vedi al di sotto
Sub ListBox1_Click()
myVar = vbEmpty
Lrow = Cells(Rows.Count, "G").End(xlUp).Row
On Error GoTo ErrHandler
UserForm2.ListBox2.List = Unique_With_Pivot(Range("G12:G" & Lrow))
'creo le item della listbox2 ordinate
UserForm2.Show
Exit Sub
ErrHandler:
If Not (IsEmpty(myVar)) Then
UserForm2.ListBox2.AddItem myVar
UserForm2.Show
End If
End Sub
Function Unique_With_Pivot(rng As Excel.Range, Optional bValid As Boolean)
Dim r As Excel.Range, sPFName As String
Dim PC As PivotCache, PT As PivotTable
Set r = rng.Parent.Parent.Worksheets.Add.[a3]
sPFName = rng(1).Value
Set PC = rng.Parent.Parent.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=rng.Address(, , , True))
Set PT = r.Parent.PivotTables.Add(PC, r, "tabella_pivot_x")
With PT.PivotFields(sPFName)
.Orientation = xlRowField
.Position = 1
Unique_With_Pivot = .DataRange.Value
End With
Application.DisplayAlerts = False
r.Parent.Delete
Application.DisplayAlerts = True
bValid = (IsArray(Unique_With_Pivot))
If Not bValid Then myVar = Unique_With_Pivot
End Function
'====================
Npta bene: La variabile myVar deve essere dichiarata nella parte
superiore del modulo, prima di qualsiasi altro codice.
===
Regards,
Norman