I have a column with a list of countries. I wish to delete all rows which do not have any of the countries listed in arrchoices. (i.e. I want to keep all rows with these countries). My data has about 4,000 rows. My macro ends up deleting all rows instead.
Sub delete_countries(sh As Worksheet, col As String)
Dim a As Variant, aWords As Variant
Dim i As Long, j As Long
Dim BanWords As String
Dim lastrow As Long
Dim arrChoices() As String
Application.StatusBar = "Deleting Countries..."
'Exceptions List
arrChoices = Split("Bahamas,Greece,South Africa,Kuwait,Germany,Brazil,Spain,Taiwan,Switzerland,USA,United Kingdom,Australia,Austria,British Virgin Islands,Vatican City,Cayman Islands,Bermuda,Canada,Turks and Caicos Islands,India,Israel,Ireland,Iran,Japan,Mexico,Trinidad and Tobago,US Virgin Islands,Italy,France, Portugal,United Arab Emirates,Belarus,Netherlands,Norway,Brunei,Kenya,Sweden,China,Hong Kong,Seychelles,Saudi Arabia,Turkey,Anguilla,Thailand,Maldives, ", ", ")
''Find Last Row
sh.Activate
With sh
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
'Reverse Iteration
For i = lastrow To 2 Step -1
For j = LBound(arrChoices) To UBound(arrChoices)
If InStr(1, Range(col & i), arrChoices(j)) = 0 Then Rows(i).EntireRow.Delete
Next j
Next i
End Sub
As discussed I am trying to setup the array for the autofilter but I am getting an error in the application of the filter:
Set imp_obj = x_seasonal.ListObjects(1)
imp_obj.AutoFilter.ShowAllData
arrChoices = Split("Bahamas,Greece,South Africa,Kuwait,Germany,Brazil,Spain,Taiwan,Switzerland,USA,United Kingdom,Australia,Austria,British Virgin Islands,Vatican City,Cayman Islands,Bermuda,Canada,Turks and Caicos Islands,India,Israel,Ireland,Iran,Japan,Mexico,Trinidad and Tobago,US Virgin Islands,Italy,France, Portugal,United Arab Emirates,Belarus,Netherlands,Norway,Brunei,Kenya,Sweden,China,Hong Kong,Seychelles,Saudi Arabia,Turkey,Anguilla,Thailand,Maldives, ", ", ")
'Filter Values
With imp_obj.Range
.AutoFilter Field:=2, Criteria1:=arrChoices
End With