Sub ProvaLuca()
Dim MioRangeL
Dim MiaCellaW1
Dim DaAggiungere()
Dim Vettore()
Dim Indice As Integer
Dim Index2 As Integer
Dim MiaCella As Range
Dim Trovato As Boolean
Dim UltimaCella As Range
Dim PrimaCella As Range
Set MiaCellaW1 = Range("A2")
Set MioRangeL = Range(MiaCellaW1.Offset(1, 0), MiaCellaW1.Offset(1, 0).End(xlToRight).End(xlDown))
MioRangeL.Select
ActiveWorkbook.Worksheets("Foglio2").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Foglio2").Sort.SortFields.Add Key:=Intersect(MioRangeL, MioRangeL.Columns(1)) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
ActiveWorkbook.Worksheets("Foglio2").Sort.SortFields.Add Key:=Intersect(MioRangeL, MioRangeL.Columns(2)) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Foglio2").Sort.SortFields.Add Key:=Intersect(MioRangeL, MioRangeL.Columns(3)) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Foglio2").Sort
.SetRange MioRangeL
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Set UltimaCella = MioRangeL.Cells(1, 1)
ReDim DaAggiungere(1 To 3, 0)
Set PrimaCella = MioRangeL.Cells(1, 1)
Do
ReDim Vettore(1 To 2, 0)
Do
Set MiaCella = UltimaCella
MiaCella.Select
If UBound(Vettore, 2) = 0 Then
ReDim Vettore(1 To 2, 1)
Vettore(1, 1) = MiaCella.Offset(0, 2)
Else
Trovato = False
For Indice = 1 To UBound(Vettore, 2)
If Vettore(1, Indice) = MiaCella.Offset(0, 2) Then
Trovato = True
Exit For
End If
Next
If Not Trovato Then
ReDim Preserve Vettore(1 To 2, UBound(Vettore, 2) + 1)
Vettore(1, UBound(Vettore, 2)) = MiaCella.Offset(0, 2)
End If
End If
Set MiaCella = MiaCella.Offset(1, 0)
Set UltimaCella = MiaCella
Loop While MiaCella = MiaCella.Offset(-1, 0)
Set MiaCella = PrimaCella
Do
For Indice = 1 To UBound(Vettore, 2)
Vettore(2, Indice) = False
Next Indice
Do
MiaCella.Select
For Indice = 1 To UBound(Vettore, 2)
If Vettore(1, Indice) = MiaCella.Offset(0, 2) Then
Vettore(2, Indice) = True
End If
Next Indice
Set MiaCella = MiaCella.Offset(1, 0)
Loop While MiaCella.Offset(-1, 1) = MiaCella.Offset(0, 1)
For Indice = 1 To UBound(Vettore, 2)
If Vettore(2, Indice) = False Then
ReDim Preserve DaAggiungere(1 To 3, UBound(DaAggiungere, 2) + 1)
DaAggiungere(1, UBound(DaAggiungere, 2)) = MiaCella.Offset(-1, 0)
DaAggiungere(2, UBound(DaAggiungere, 2)) = MiaCella.Offset(-1, 1)
DaAggiungere(3, UBound(DaAggiungere, 2)) = Vettore(1, Indice)
End If
Next Indice
'Set MiaCella = MiaCella.Offset(1, 0)
Set PrimaCella = UltimaCella.Offset(1, 0)
Set UltimaCella = MiaCella
Loop While MiaCella.Offset(-1, 0) = MiaCella
Loop While MiaCella.Offset(1, 0) <> ""
' MsgBox "pippo"
UltimaCella.Select
For Indice = 1 To UBound(DaAggiungere, 2)
For Index2 = 1 To 3
UltimaCella.Offset(Indice - 1, Index2 - 1) = DaAggiungere(Index2, Indice)
Next
Next
Range(UltimaCella.Offset(-1, 0), UltimaCella.Offset(-1, 2)).Copy
With Range(UltimaCella, UltimaCella.Offset(UBound(DaAggiungere, 2) - 1, 3 - 1))
.Select
.PasteSpecial _
Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
With .Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
Set MioRangeL = Range("A2")
Set MioRangeL = Range(MioRangeL.Offset(1, 0), MioRangeL.Offset(1, 0).End(xlToRight).End(xlDown))
MioRangeL.Select
ActiveWorkbook.Worksheets("Foglio2").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Foglio2").Sort.SortFields.Add Key:=Intersect(MioRangeL, MioRangeL.Columns(1)) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
ActiveWorkbook.Worksheets("Foglio2").Sort.SortFields.Add Key:=Intersect(MioRangeL, MioRangeL.Columns(2)) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Foglio2").Sort.SortFields.Add Key:=Intersect(MioRangeL, MioRangeL.Columns(3)) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Foglio2").Sort
.SetRange MioRangeL
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Set MioRangeL = Range(MiaCellaW1.Offset(1, 0), MiaCellaW1.Offset(1, 0).End(xlToRight).End(xlDown))
MioRangeL.Select
ActiveWorkbook.Worksheets("Foglio2").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Foglio2").Sort.SortFields.Add Key:=Intersect(MioRangeL, MioRangeL.Columns(1)) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
ActiveWorkbook.Worksheets("Foglio2").Sort.SortFields.Add Key:=Intersect(MioRangeL, MioRangeL.Columns(2)) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Foglio2").Sort.SortFields.Add Key:=Intersect(MioRangeL, MioRangeL.Columns(3)) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Foglio2").Sort
.SetRange MioRangeL
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub |