Suppression de cellules visibles après le filtrage

Je ne sais pas pourquoi mon code VBA ne fonctionne pas:

J'ai donc essayé le code et ça marche bien pour CNHK

Mais comme j'ai répliqué le code vers le bas, il cesse de fonctionner

Donc pour TW en avant (j'ai seulement inclus TW) Je continue d'get ce message d'erreur:

"Supprimer la méthode de la class Range failed"

pour cette partie du code:

r.Offset (1, 0) .SpecialCells (xlCellTypeVisible) .EntireRow.Delete

Je ne suis pas trop sûr de savoir pourquoi c'est que la seule partie que je ajoute est la scope de chacun.

Sub CNHK() Dim oLo As ListObject Dim r As Range Set oLo = Sheets("Data").ListObjects("Table2") Set r = oLo.AutoFilter.Range oLo.Range.AutoFilter Field:=4, Criteria1:= _ Array("AUSTRALIA", "FUKUOKA", "INDIA", "INDONESIA", "LONDON", "MALAYSIA", "NAGOYA", _ "NORTH AMERICA", "OSAKA", "PHILIPPINES", "SINGAPORE", "SOUTH AMERICA", "SOUTH KOREA" _ , "TAIWAN", "THAILAND", "TOKYO", "VIETNAM"), Operator:=xlFilterValues r.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete oLo.Range.AutoFilter Sheets(Array("Dash Fwd", "Dash Bck")).Select Sheets("Dash Fwd").Activate Rows("40:75").Select Selection.EntireRow.Hidden = True Rows("110:459").Select Selection.EntireRow.Hidden = True Rows("635:1054").Select Selection.EntireRow.Hidden = True Sheets("Dash Bck").Activate Rows("40:75").Select Selection.EntireRow.Hidden = True Rows("110:459").Select Selection.EntireRow.Hidden = True Rows("635:1054").Select Selection.EntireRow.Hidden = True Sheets("Dash Fwd").Select ActiveSheet.Protect Password:="013054", DrawingObjects:=False, Contents:=True, Scenarios:= _ False, AllowFormattingCells:=True, AllowFormattingColumns:=False, _ AllowFormattingRows:=False, AllowInsertingColumns:=True, AllowInsertingRows _ :=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _ AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _ AllowUsingPivotTables:=True Range("A1").Select Sheets("Dash Bck").Select ActiveSheet.Protect Password:="013054", DrawingObjects:=False, Contents:=True, Scenarios:= _ False, AllowFormattingCells:=True, AllowFormattingColumns:=False, _ AllowFormattingRows:=False, AllowInsertingColumns:=True, AllowInsertingRows _ :=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _ AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _ AllowUsingPivotTables:=True Range("A1").Select End Sub Sub TW() Dim oLo As ListObject Dim r As Range Set oLo = Sheets("Data").ListObjects("Table2") Set r = oLo.AutoFilter.Range oLo.Range.AutoFilter Field:=4, Criteria1:= _ Array("AUSTRALIA", "FUKUOKA", "INDIA", "INDONESIA", "LONDON", "MALAYSIA", "NAGOYA", _ "NORTH AMERICA", "OSAKA", "PHILIPPINES", "SINGAPORE", "SOUTH AMERICA", "SOUTH KOREA" _ , "BEIJING", "THAILAND", "TOKYO", "VIETNAM", "CHENGDU", "GUANGZHOU", "HONG KONG", "SHANGHAI"), Operator:=xlFilterValues r.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete oLo.Range.AutoFilter Sheets(Array("Dash Fwd", "Dash Bck")).Select Sheets("Dash Fwd").Activate Rows("40:110").Select Selection.EntireRow.Hidden = True Rows("145:1055").Select Selection.EntireRow.Hidden = True Sheets("Dash Bck").Activate Rows("40:110").Select Selection.EntireRow.Hidden = True Rows("145:1055").Select Selection.EntireRow.Hidden = True Sheets("Dash Fwd").Select ActiveSheet.Protect Password:="013054", DrawingObjects:=False, Contents:=True, Scenarios:= _ False, AllowFormattingCells:=True, AllowFormattingColumns:=False, _ AllowFormattingRows:=False, AllowInsertingColumns:=True, AllowInsertingRows _ :=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _ AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _ AllowUsingPivotTables:=True Range("A1").Select Sheets("Dash Bck").Select ActiveSheet.Protect Password:="013054", DrawingObjects:=False, Contents:=True, Scenarios:= _ False, AllowFormattingCells:=True, AllowFormattingColumns:=False, _ AllowFormattingRows:=False, AllowInsertingColumns:=True, AllowInsertingRows _ :=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _ AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _ AllowUsingPivotTables:=True Range("A1").Select End Sub 

Probablement le problème est qu'il n'y a rien qui rest filtré. Essayez d'intégrer le code d'erreur avec la condition suivante:

 If not r is Nothing then r.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete end if 

En outre, pour voir si c'est le cas, écrivez debug.print r.Address dans la ligne avant l'erreur. Si elle n'est pas définie, cela devrait aussi être une erreur. Sinon, il imprimera l'adresse dans la window immédiate.

Remplacer cette partie

r.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete

de votre code avec ceci

 Application.DisplayAlerts = False r.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete Application.DisplayAlerts = True 

Vous n'avez pas besoin d'invoquer les SpecialCells avant de supprimer, car la méthode Delete agit uniquement sur les lignes visibles.