Slette alle rækker undtagen værdi i array
HejJeg skal bruge en macro i Excel som kan slette alle rækker undtagen dem der indeholder bestemte værdier. Har fundet denne kode som næsten passer - bortset fra at den sletter netop de rækker som findes i arrayet. Har forsøgt at ændre "If Not rng Is Nothing Then rng.EntireRow.Delete" til "If rng Is Nothing Then rng.EntireRow.Delete" men får fejlen "Object variable or with block variable not set". Har I nogle bud på det?
Sub Delete_with_Autofilter_Array()
Dim rng As Range
Dim calcmode As Long
Dim myArr As Variant
Dim I As Long
With Application
calcmode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
'Fill in the values that you want to delete
myArr = Array("ron", "Dave", "Jelle")
For I = LBound(myArr) To UBound(myArr)
'Sheet with the data, you can also use Sheets("MySheet")
With ActiveSheet
'Firstly, remove the AutoFilter
.AutoFilterMode = False
'Apply the filter
.Range("A1:A" & .Rows.Count).AutoFilter Field:=1, Criteria1:=myArr(I)
Set rng = Nothing
With .AutoFilter.Range
On Error Resume Next
Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not rng Is Nothing Then rng.EntireRow.Delete
End With
'Remove the AutoFilter
.AutoFilterMode = False
End With
Next I
With Application
.ScreenUpdating = True
.Calculation = calcmode
End With
End Sub
