Come usare esempi di macro per eliminare elementi duplicati in un elenco in Excel
Ulteriori informazioni
Microsoft fornisce esempi di programmazione a scopo puramente illustrativo, senza alcuna garanzia di qualsiasi tipo, sia espressa che implicita, ivi incluse, senza limitazioni, le garanzie implicite di commerciabilità o idoneità per uno scopo particolare. In questo articolo si presuppone che l'utente conosca il linguaggio di programmazione in questione e gli strumenti utilizzati per creare ed eseguire il debug delle procedure. Gli esperti Microsoft sono autorizzati a fornire spiegazioni in merito alla funzionalità di una particolare routine, ma in nessun caso a modificare questi esempi per fornire funzionalità aggiuntive o a creare routine atte a soddisfare specifiche esigenze.
Esempio 1: Eliminare elementi duplicati in un singolo elenco
La macro di esempio seguente cerca un singolo elenco nell'intervallo A1:A100 ed elimina tutti gli elementi duplicati nell'elenco. Questa macro richiede che non siano presenti celle vuote nell'intervallo di elenchi. Se l'elenco contiene celle vuote, ordinare i dati in ordine crescente in modo che le celle vuote siano tutte alla fine dell'elenco.
Sub DelDups_OneList()
Dim iListCount As Integer
Dim iCtr As Integer
' Turn off screen updating to speed up macro.
Application.ScreenUpdating = False
' Get count of records to search through.
iListCount = Sheets("Sheet1").Range("A1:A100").Rows.Count
Sheets("Sheet1").Range("A1").Select
' Loop until end of records.
Do Until ActiveCell = ""
' Loop through records.
For iCtr = 1 To iListCount
' Don't compare against yourself.
' To specify a different column, change 1 to the column number.
If ActiveCell.Row <> Sheets("Sheet1").Cells(iCtr, 1).Row Then
' Do comparison of next record.
If ActiveCell.Value = Sheets("Sheet1").Cells(iCtr, 1).Value Then
' If match is true then delete row.
Sheets("Sheet1").Cells(iCtr, 1).Delete xlShiftUp
' Increment counter to account for deleted row.
iCtr = iCtr + 1
End If
End If
Next iCtr
' Go to next record.
ActiveCell.Offset(1, 0).Select
Loop
Application.ScreenUpdating = True
MsgBox "Done!"
End Sub
Esempio 2: Confrontare due elenchi ed eliminare elementi duplicati
La macro di esempio seguente confronta un elenco (master) con un altro elenco ed elimina gli elementi duplicati nel secondo elenco che si trovano anche nell'elenco master. Il primo elenco si trova in Sheet1 nell'intervallo A1:A10. Il secondo elenco si trova in Sheet2 nell'intervallo A1:A100. Per utilizzare la macro, selezionare un foglio e quindi eseguire la macro.
Sub DelDups_TwoLists()
Dim iListCount As Integer
Dim iCtr As Integer
' Turn off screen updating to speed up macro.
Application.ScreenUpdating = False
' Get count of records to search through (list that will be deleted).
iListCount = Sheets("sheet2").Range("A1:A100").Rows.Count
' Loop through the "master" list.
For Each x In Sheets("Sheet1").Range("A1:A10")
' Loop through all records in the second list.
For iCtr = 1 To iListCount
' Do comparison of next record.
' To specify a different column, change 1 to the column number.
If x.Value = Sheets("Sheet2").Cells(iCtr, 1).Value Then
' If match is true then delete row.
Sheets("Sheet2").Cells(iCtr, 1).Delete xlShiftUp
' Increment counter to account for deleted row.
iCtr = iCtr + 1
End If
Next iCtr
Next
Application.ScreenUpdating = True
MsgBox "Done!"
End Sub