excelNewbie22
Well-known Member
- Joined
- Aug 4, 2021
- Messages
- 531
- Office Version
- 365
- Platform
- Windows
hi!
i had a mistake (sorry) back then explaining this:
(in here i use the numbers 1-4-9 but now i use others)
cause i need something slightly else, and i'll try to elaborate:
i need the macro to delete any lines/rows with the combination of 4 numbers (a1:d1, a2:d2 and etc) containing only the numbers 1-5-7 like 1577 or 7117 or 7115 or 7775,
but to keep lines with only part of them like 1572 or 1178 or 7558
so basically to delete lines containing only these numbers 1-5-7 and nothing else
if the line contains any other number then 1-5-7 it stays
i had a mistake (sorry) back then explaining this:
deleting rows with macro
hi! how can i delete rows with multiple cells with specific values ? example: i want to delete the rows (entirely) which contain only the numbers 1 and 4 and 9 ? ABCDE151149211159342419412129512489613519712459882469922479
www.mrexcel.com
cause i need something slightly else, and i'll try to elaborate:
i need the macro to delete any lines/rows with the combination of 4 numbers (a1:d1, a2:d2 and etc) containing only the numbers 1-5-7 like 1577 or 7117 or 7115 or 7775,
but to keep lines with only part of them like 1572 or 1178 or 7558
so basically to delete lines containing only these numbers 1-5-7 and nothing else
if the line contains any other number then 1-5-7 it stays
sheet1 | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | 1 | 1 | 4 | 6 | keep | ||
2 | 1 | 1 | 4 | 7 | keep | ||
3 | 1 | 1 | 4 | 8 | keep | ||
4 | 1 | 1 | 5 | 1 | delete | ||
5 | 1 | 1 | 5 | 2 | keep | ||
6 | 1 | 1 | 5 | 3 | keep | ||
7 | 1 | 1 | 5 | 4 | keep | ||
8 | 1 | 1 | 5 | 5 | delete | ||
9 | 1 | 2 | 5 | 7 | keep | ||
10 | 1 | 1 | 7 | 8 | keep | ||
11 | 7 | 5 | 5 | 8 | keep | ||
12 | 7 | 7 | 7 | 5 | delete | ||
13 | 5 | 1 | 7 | 7 | delete | ||
sheet1 |
VBA Code:
Sub RemoveRowsIf()
'removes entire rows containing Num1 and Num2 and Num3
'assumes data start in cell A1
Dim R As Range, V As Variant, i As Long, j As Long, Ct As Long, d As Object
Const Num1 As Long = 1: Const Num2 As Long = 5: Const Num3 As Long = 7 ' change Nums to suit
Set R = Range("A1").CurrentRegion
Set d = CreateObject("Scripting.dictionary")
V = R.Value
For i = 1 To UBound(V, 1)
For j = 1 To UBound(V, 2)
If V(i, j) = Num1 Or V(i, j) = Num2 Or V(i, j) = Num3 Then
If Not d.exists(V(i, j)) Then
d.Add V(i, j), ""
Ct = Ct + 1
If Ct = 3 Then
V(i, j) = "#N/A"
GoTo Nx
End If
End If
End If
Next j
Nx: d.RemoveAll
Ct = 0
Next i
R.Value = V
On Error Resume Next
R.SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
On Error GoTo 0
End Sub