Deleting Multiple Rows based on a Set Criteria - excel

Sub Macro()
Dim i As Long
For i = Cells(Rows.Count, 14).End(xlUp).Row To 2 Step -1
If Cells(i, 14).Value2 = "APPLE" Then
Rows(i).Delete
End If
Next i
Dim f As Long
For f = Cells(Rows.Count, 14).End(xlUp).Row To 2 Step -1
If Cells(f, 14).Value2 = "NAME" Then
Rows(f).Delete
End If
Next f
End Sub
I have the above mentioned code to delete all the rows that have apple and name on them, If possible I would like excel to execute the code in one or two lines. Your help would be greatly appreciated!

Dim i As Long
For i = Cells(Rows.Count, 14).End(xlUp).Row To 2 Step -1
IF Cells(i, 14).Value2 = "APPLE" OR Cells(i, 14).Value2 = "NAME" THEN Rows(i).Delete
Next i

The fast way to delete rows is using AutoFilter:
Sub FastDelete()
Dim rng As Range, rngVisible As Range
'//Remove filter if any
ActiveSheet.AutoFilterMode = False
'// Get range of only one column (N)
Set rng = Range(Cells(1, 14), Cells(Rows.Count, 14).End(xlUp))
'// Field:=1 because filter has only one field
rng.AutoFilter Field:=1, Criteria1:=Array("APPLE", "NAME"), Operator:=xlFilterValues
'// Have error handling in case if no data is found
On Error Resume Next
With rng
'// Use Offset and Resize to exclude header
Set rngVisible = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
End With
'// If rows were found (i.e. there's no error), delete them
If Err = 0 Then rngVisible.EntireRow.Delete
On Error GoTo 0
'// Remove filter
ActiveSheet.AutoFilterMode = False
End Sub

Related

Select only first 5 values of a column after applying a filter to a particular column on a particular condition,without duplicates

i have applied autofilter to the column,that part pf the code is running properly ,but on that condition there are suppose 20 values in that column but i want only 5 ,any particular code would help
Dim rFirstFilteredRow As Range
With Worksheets("Sheet1")
With .Cells(1, 1).CurrentRegion
ActiveSheet.Range("$A$1:$D$6").AutoFilter Field:=4, Criteria1:="1"
With .Resize(.Rows.Count - 1, .Rows.Count).Offset(1, 0)
If CBool(Application.Subtotal(103, .Cells)) Then
Set rFirstFilteredRow = _
.SpecialCells(xlCellTypeVisible).Columns(2).Cells
rFirstFilteredRow.Copy
Range("G16").Select
ActiveSheet.Paste
End If
End With
End With
End With
End Sub
this helps in getting first column after filter but not the first five
Just add .Resize(5) when setting the width of rFirstFilteredRow to resize the selection to 5 rows high.
Example below (I shortened the code a lot):
Sub Answer()
Dim rFirstFilteredRow As Range
ActiveSheet.Range("$A$1:$D$6").AutoFilter Field:=4, Criteria1:="1"
With Worksheets("Sheet1").Cells(1, 1).CurrentRegion
With .Resize(.Rows.Count - 1, .Rows.Count).Offset(1, 0)
If CBool(Application.Subtotal(103, .Cells)) Then
' Select first 5 columns starting at column 2
Set rFirstFilteredRow = _
.SpecialCells(xlCellTypeVisible).Columns(2).Resize(5)
rFirstFilteredRow.Copy
Range("G16").Select
ActiveSheet.Paste
End If
End With
End With
End Sub
Sub macro2()
Const MAXROWS = 5
Dim ws As Worksheet, rng As Range
Dim i As Long, c As Range
Set ws = ThisWorkbook.Worksheets("Sheet1")
ws.Range("$A$1:$D$6").AutoFilter Field:=4, Criteria1:="1"
With ws.Cells(1, 1).CurrentRegion.Columns(2)
Set rng = .Cells.SpecialCells(xlCellTypeVisible)
End With
i = 0
For Each c In rng.Cells
If i > 0 Then ' skip header
ws.Range("G16").Offset(i - 1) = c.Value2
End If
i = i + 1
If i > MAXROWS Then Exit For
Next
End Sub

Rows are not getting deleted

The below code is not deleting the rows as expected. Can someone tell me what is wrong with it?
With Worksheets("Alerts Ack By Cops")
For rw = .Cells(.Rows.Count, "A").End(xlUp).Row To 2 Step -1
Select Case UCase(.Cells(rw, "A").Value2)
Case "Punna,", "Juleas,"
.Rows(rw).EntireRow.Delete
i = i - 1
End Select
Next rw
End With
When you are looking for text with capital letter using UCase then you need to match with upper case values.
Change
Case "Punna,", "Juleas,"
to
Case "PUNNA,", "JULEAS,"
Also since you want to match the cell which starts with those words, you can use LIKE as shown below
With Worksheets("Alerts Ack By Cops")
For rw = .Cells(.Rows.Count, "A").End(xlUp).Row To 2 Step -1
If UCase(.Cells(rw, "A").Value2) Like "PUNNA,*" Or _
UCase(.Cells(rw, "A").Value2) Like "JULEAS,*" Then
.Rows(rw).EntireRow.Delete
End If
Next rw
End With
Also, using autofilter would be faster. You may want to see Delete row based on partial text. Here the cirteria becomes "=" & strSearch & "*" instead of "=*" & strSearch & "*"
If you still want to delete the rows using a loop then you may want to see Union way of doing it in Excel VBA - Delete empty rows which will increase the speed drastically over a large number of rows.
Delete Rows Using AutoFilter
The first solution illustrates the use of two criteria.
The second solution illustrates how to do the same with an array. Unfortunately AutoFilter can have only two criteria containing wild characters. If you add more, nothing will be filtered.
The third solution illustrates a workaround which allows more than two criteria containing wild characters.
The Code
Option Explicit
Sub deleteTwoCriteria()
Application.ScreenUpdating = False
With Worksheets("Alerts Ack By Cops")
.AutoFilterMode = False
With .Range("A1", .Cells(.Rows.Count, "A").End(xlUp))
.AutoFilter _
Field:=1, _
Criteria1:="Punna,*", _
Operator:=xlOr, _
Criteria2:="Juleas,*"
With .Resize(.Rows.Count - 1).Offset(1)
On Error Resume Next
.SpecialCells(xlCellTypeVisible).EntireRow.Delete
On Error GoTo 0
End With
End With
.AutoFilterMode = False
End With
Application.ScreenUpdating = True
End Sub
Sub deleteArray()
Application.ScreenUpdating = False
With Worksheets("Alerts Ack By Cops")
.AutoFilterMode = False
With .Range("A1", .Cells(.Rows.Count, "A").End(xlUp))
.AutoFilter _
Field:=1, _
Criteria1:=Array("Punna,*", "Juleas,*"), _
Operator:=xlFilterValues
With .Resize(.Rows.Count - 1).Offset(1)
On Error Resume Next
.SpecialCells(xlCellTypeVisible).EntireRow.Delete
On Error GoTo 0
End With
End With
.AutoFilterMode = False
End With
Application.ScreenUpdating = True
End Sub
Sub deleteList()
Const CriteriaList As String = "Punna,*|Juleas,*|R2*"
Dim Criteria() As String: Criteria = Split(CriteriaList, "|")
Application.ScreenUpdating = False
With Worksheets("Alerts Ack By Cops")
.AutoFilterMode = False
With .Range("A1", .Cells(.Rows.Count, "A").End(xlUp))
If .Cells.Count > 1 Then
Dim Data As Variant: Data = .Value
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
dict.CompareMode = vbTextCompare
Dim i As Long
Dim n As Long
For i = 2 To UBound(Data, 1)
For n = 0 To UBound(Criteria)
If LCase(Data(i, 1)) Like LCase(Criteria(n)) Then
dict(Data(i, 1)) = Empty
Exit For
End If
Next n
Next i
If dict.Count > 0 Then
.AutoFilter _
Field:=1, _
Criteria1:=dict.Keys, _
Operator:=xlFilterValues
With .Resize(.Rows.Count - 1).Offset(1)
.SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
.Worksheet.AutoFilterMode = False
End If
End If
End With
End With
Application.ScreenUpdating = True
End Sub

VBA: Looping a condition through a range that compares values from other columns until the list ends

Public Sub MainTOfomat()
Dim ShippingQty As Range
Dim ReceivedQty As Range
ActiveSheet.Columns("A:P").AutoFit
ActiveSheet.Range("A:P").AutoFilter Field:=13, Criteria1:="No"
ActiveSheet.Range("K:L").AutoFilter Field:=2, Criteria1:="<>"
Set ShippingQty = Range("K2")
Set ReceivedQty = ShippingQty.Offset(0, 1)
ShippingQty.Select
Do Until IsEmpty(ActiveCell)
If ShippingQty.Value = 0 Then
ShippingQty.Offset(0, 5) = "Needs Fulfillment"
ElseIf ShippingQty.Value > ReceivedQty.Value Then
ShippingQty.Offset(0, 5) = "Needs Receipt"
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub
The code is program is supposed to loop though each row in the column and fill in the statement based on the result of the condition for values in two other columns. The problem is that the loop goes through, but only the first line actually changes, and the auto filter code before the loop gets skipped.
Here is your macro fixed up.
As mentioned before your ShippingQty range and ReceivedQty do not change with the activecell. When moving to the next cell, that is the activecell. The filter range need to be the same. A:P is filtered, when changing to K:L ,field 2 actually becomes column B, so if you want to filter out non-blanks in column L you need the field 12.
Sub YourMacro()
Dim ShippingQty As Range
Dim ReceivedQty As Range
ActiveSheet.Columns("A:P").AutoFit
With ActiveSheet.Range("A:P")
.AutoFilter Field:=13, Criteria1:="No"
.AutoFilter Field:=12, Criteria1:="<>"
End With
Set ShippingQty = Range("K2")
Set ReceivedQty = ShippingQty.Offset(0, 1)
ShippingQty.Select
Do Until IsEmpty(ActiveCell)
If ActiveCell.Rows.Hidden = False Then
If ActiveCell.Value = 0 Then
ActiveCell.Offset(0, 5) = "Needs Fulfillment"
ElseIf ActiveCell.Value > ActiveCell.Offset(, 1).Value Then
ActiveCell.Offset(0, 5) = "Needs Receipt"
End If
End If
ActiveCell.Offset(1, 0).Select
Loop
ActiveSheet.AutoFilterMode = 0
End Sub
You can use this option as well without using selects.
Sub Option1()
Dim rng As Range, c As Range
Dim ws As Worksheet
Set ws = ActiveSheet
Application.ScreenUpdating = 0
With ws
Set rng = .Range("K2:K" & .Cells(.Rows.Count, "K").End(xlUp).Row)
With .Range("A:P")
.AutoFilter Field:=13, Criteria1:="No"
.AutoFilter Field:=12, Criteria1:="<>"
End With
For Each c In rng.SpecialCells(xlCellTypeVisible)
If c = 0 Then c.Offset(, 5) = "Needs Fulfillments"
If c > c.Offset(, 1) Then c.Offset(, 5) = "Needs Receipts"
Next c
.AutoFilterMode = False
End With
End Sub

Delete rows with multiple criteria in VBA

my goal is to delete rows with column 3 with the cell value that has inventory (>0) and column 4 that has the cell value TRUE in the current sheet. I tried to use the code to this website and I'm pretty sure I did something wrong where it says ActiveSheet.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
Public Sub FilterStock()
ActiveSheet.Range("A1").AutoFilter Field:=4, Criteria1:="TRUE"
ActiveSheet.Range("A1").AutoFilter Field:=3, Criteria1:=">0"
Application.DisplayAlerts = False
ActiveSheet.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True
ActiveSheet.AutoFilter.ShowAllData
End Sub
This code worked for me:
Sub DeletelRows()
Dim lastRow As Long
Dim debug1 As Variant
Dim debug2 As Variant
'Find the last non-blank cell in column C
lastRow = Cells(Rows.Count, 3).End(xlUp).Row
For x = lastRow To 2 Step -1 'Start at bottom and go up to avoid complications when the row is deleted.
debug1 = Cells(x, 3).Value 'You can set breakpoints to see what the values are.
debug2 = Cells(x, 4).Value
If (Cells(x, 3).Value > 0 And UCase(Cells(x, 4).Value) = "TRUE") Then
Rows(x).Delete
End If
Next x
End Sub

VBA Extracting all relevant data and sorting plus validation

Ok here's the scenario,
I have 4 Criteria:
District
Max Price
Min Size
Rooms
I've got a list of data that all the values required on a worksheet(OnSale) i just need to run certain algorithm in between to sort out these criteria :
Whether the district(integer) chose is the one the client chose
If the Price(Integer) is lesser than the Max Price
If the size is greater than the Min Size (Integer)
If the house has the number of rooms (Integer) that the client choose.
If the data within the list on the worksheet(OnSale) matches the requirements above, it will first create a table then add the details of the home that fits all the criteria above as per below. (Project|Unit Number|Price|Price(psf)|Price(psm)|Size (sqm)|BedRooms|Tenure) (Found on OnSale)
Lastly, If the table churns no results i need it to delete the new sheet automatically and inform the user that there's no such sale currently. <-- Possibly MsgBox. I really hope someone can help me with this cus i'm really new to VBA and need to make these things happen :( Would really appreciate it if someone could help.
Thanks in advance!
Here's where i got to so far but the code doesnt churn me any results in
Option Explicit
Sub finddata()
Dim district As String
Dim maxPrice As Long
Dim minSize As Integer
Dim room As Integer
Dim finalRow As Integer
Dim i As Integer
Sheets("Alakazam").Range("A2:M1048576").ClearContents
district = Sheets("RealEstateAmigo!").Range("T4").Value
maxPrice = Sheets("RealEstateAmigo!").Range("T5").Value
minSize = Sheets("RealEstateAmigo!").Range("T6").Value
room = Sheets("RealEstateAmigo!").Range("T7").Value
finalRow = Sheets("OnSale").Range("A10000").End(xlUp).Row
For i = 2 To finalRow 'to loop & check every single value
If Cells(i, 1) = district Then ' if district match
If Cells(i, 3) < maxPrice Then 'if less than MaxPrice
If Cells(i, 6) > minSize Then 'if greater than minSize
If Cells(i, 7) = room Then ' if room number match
Range(Cells(i, 1), Cells(i, 13)).Copy 'Copy the rows
Sheets("Alakazam").Range("A2").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
End If
End If
End If
End If
Next i
Sheets("Alakazam").Select
Sheets("Alakazam").Range("A2").Select
End Sub
As I mentioned in comments above, you can use Autofilter to get desired result. I've commented code in details, but if you have some questions, ask in comments:)
Sub finddata()
Dim district As String
Dim maxPrice As Long, minSize As Integer, room As Integer, finalRow As Long
Dim sh As Worksheet
Dim data As Range
Dim rng As Range
'try to get sheet if it exist
On Error Resume Next
Set sh = Sheets("Alakazam")
On Error GoTo 0
'if it not exist - create it
If sh Is Nothing Then
Set sh = ThisWorkbook.Worksheets.Add
sh.Name = "Alakazam"
End If
sh.Range("A2:M" & Rows.Count).ClearContents
'get criterias
With Sheets("RealEstateAmigo!")
district = .Range("T4").Value
maxPrice = .Range("T5").Value
minSize = .Range("T6").Value
room = .Range("T7").Value
End With
With Sheets("OnSale")
finalRow = .Range("A" & .Rows.Count).End(xlUp).Row
Set data = .Range("A1:M" & finalRow)
'clear all previous filters
.AutoFilterMode = False
'apply filters to match criterias
With data
.AutoFilter Field:=1, Criteria1:=district
.AutoFilter Field:=3, Criteria1:="<" & maxPrice
.AutoFilter Field:=6, Criteria1:=">" & minSize
.AutoFilter Field:=7, Criteria1:="=" & room
'try to get visible rows - thouse that matches criteria
On Error Resume Next
Set rng = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If rng Is Nothing Then
'if nothing found - show error message + delete sheet
MsgBox "There is no rows matched all criterias"
Application.DisplayAlerts = False
sh.Delete
Application.DisplayAlerts = True
Else
'if data found - copy to sheet Alakazam
data.Rows(1).Copy
sh.Range("A1").PasteSpecial xlPasteValues
sh.Range("A1").PasteSpecial xlPasteFormats
'copy headers
rng.Copy
sh.Range("A2").PasteSpecial xlPasteValues
sh.Range("A2").PasteSpecial xlPasteFormats
Application.CutCopyMode = False
sh.Select
End If
End With
'disable all filters
.AutoFilterMode = False
End With
End Sub

Resources