excelNewbie22
Well-known Member
- Joined
- Aug 4, 2021
- Messages
- 528
- Office Version
- 365
- Platform
- Windows
hi!
so i tried with watching videos in youtube, searching and testing for hours for several days, with no success
but i found a vba macro partly to my needs,
which is to delete ANY row without ANY or ALL the numbers i'll set (like 5 6 7 or more)
didn't manage to redefine the range (all rows in current sheet)
can you please help me out?
example:
so i tried with watching videos in youtube, searching and testing for hours for several days, with no success
but i found a vba macro partly to my needs,
which is to delete ANY row without ANY or ALL the numbers i'll set (like 5 6 7 or more)
didn't manage to redefine the range (all rows in current sheet)
can you please help me out?
VBA Code:
Sub DeleteRows()
' Defines variables
Dim Cell As Range, cRange As Range, LastRow As Long, x As Long
' Defines LastRow as the last row of data based on column A
LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
' Sets check range as A1 to the last row of A
Set cRange = Range("A1:A" & LastRow)
' For each cell in the check range, working from the bottom upwards
For x = cRange.Cells.Count To 1 Step -1
With cRange.Cells(x)
' If the cell does not contain one of the listed values then...
If .Value <> "5" And .Value <> "6" And .Value <> "7" Then
' Delete that row
.EntireRow.Delete
End If
End With
' Check next cell, working upwards
Next x
End Sub
example:
567.xlsx | ||||||
---|---|---|---|---|---|---|
A | B | C | D | |||
6 | 1 | 1 | 2 | 7 | ||
7 | 1 | 1 | 2 | 8 | ||
8 | 1 | 1 | 3 | 2 | ||
9 | 1 | 1 | 3 | 3 | ||
10 | 1 | 1 | 3 | 4 | ||
11 | 1 | 1 | 3 | 5 | ||
12 | 1 | 1 | 3 | 6 | ||
13 | 1 | 1 | 3 | 7 | ||
14 | 1 | 1 | 3 | 8 | ||
15 | 1 | 1 | 4 | 2 | ||
16 | 1 | 1 | 4 | 3 | ||
17 | 1 | 1 | 4 | 4 | ||
18 | 1 | 1 | 4 | 5 | ||
19 | 1 | 1 | 4 | 6 | ||
20 | 1 | 1 | 4 | 7 | ||
21 | 1 | 1 | 4 | 8 | ||
22 | 1 | 1 | 5 | 2 | ||
23 | 1 | 1 | 5 | 3 | ||
24 | 1 | 1 | 5 | 4 | ||
25 | 1 | 1 | 5 | 5 | ||
26 | 1 | 1 | 5 | 6 | ||
27 | 1 | 1 | 5 | 8 | ||
28 | 1 | 1 | 6 | 2 | ||
29 | 1 | 1 | 6 | 3 | ||
30 | 1 | 1 | 6 | 4 | ||
31 | 1 | 1 | 6 | 5 | ||
32 | 1 | 1 | 6 | 6 | ||
33 | 1 | 1 | 6 | 7 | ||
34 | 1 | 1 | 6 | 8 | ||
35 | 1 | 1 | 7 | 2 | ||
567 |