I am using VBA code in Excel 2016 to delete rows (on sheet "Data") that don't match criteria on another sheet (Column A, on sheet "Unique values") in Excel. However, the code only deletes selected cells in one column (C), so the remaining cells then don't match the original data in other columns (A:BH).
Could someone help to modify the code so that the entire row is deleted, and the rows move up/down together? I have tried code from a similar thread, but it didn't delete the correct rows, whereas this code does, and the portion of the code to select the list is very helpful as this list can be moved.
Any guidance is much appreciated.
Could someone help to modify the code so that the entire row is deleted, and the rows move up/down together? I have tried code from a similar thread, but it didn't delete the correct rows, whereas this code does, and the portion of the code to select the list is very helpful as this list can be moved.
Any guidance is much appreciated.
Code:
Sub DeleteRowsThatDoNotMatch()
Dim rng As Range
Dim Rng1 As Range, Rng2 As Range
Dim arr1 As Variant
Dim arr2 As Variant
Dim dic2 As Variant
Dim OutArr As Variant
xTitleId = "Delete Row"
Set Rng1 = Application.Selection
Set Rng1 = Application.InputBox("Select data to delete :", xTitleId, Rng1.Address, Type:=8)
Set Rng2 = Application.InputBox("Select unique values:", xTitleId, Type:=8)
Set Rng1 = Rng1.Columns(1)
Set Rng2 = Rng2.Columns(1)
Set dic2 = CreateObject("Scripting.Dictionary")
arr1 = Rng1.Value
arr2 = Rng2.Value
For i = 1 To UBound(arr2, 1)
xKey = arr2(i, 1)
dic2(xKey) = ""
Next
Rng1.ClearContents
OutArr = Rng1.Value
xIndex = 1
For i = 1 To UBound(arr1, 1)
xKey = arr1(i, 1)
If dic2.Exists(xKey) Then
OutArr(xIndex, 1) = xKey
xIndex = xIndex + 1
End If
Next
Rng1.Value = OutArr
End Sub