Code Optimization - Two column filter

F_Ribeiro

New Member
Joined
Jan 14, 2021
Messages
32
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Good afternoon everybody!


After I received the answers to the topics I posted, I decided to apply those concepts in a previous work that had already ended. Basically I want to optimize the filtering of "0" values in two different columns. But to avoid errors, I used the logical operator NOT together with the IsEmpty () method so that all the filled lines were traversed with the WHILE and FOR decision structures, ensuring that all "0" were checked, also preventing them from being confused with empty cells.

However, the code can certainly be optimized, especially the filtering I did on the IF, which was very structured and takes all the cells in an entire column, making the process take longer. Trying to apply what I learned before, I was unable to produce the result I wanted for some reason (and unfortunately I no longer have the code I generated a short time ago).

So how can I make this task simpler? I will be very grateful if you have that answer. : - D



VBA Code:
Sub deleteZeros()

Application.ScreenUpdating = False

Dim W As Worksheet

Dim rowNumber As Long

Set W = Sheets("Sheet1")

W.Select

rowNumber = 2


While Not IsEmpty(W.Cells(rowNumber, "AB").Value) And Not IsEmpty(W.Cells(rowNumber, "AI").Value


Do Until (W.Cells(rowNumber, "AB").Value = 0) And (W.Cells(rowNumber, "AI") = 0)

rowNumber = rowNumber + 1

Loop

If ((W.Cells(rowNumber, "AB").Value = 0) And (W.Cells(rowNumber, "AI") = 0)) And (Not IsEmpty(W.Cells(rowNumber, "AB").Value) And Not IsEmpty(W.Cells(rowNumber, "AI").Value)) Then

W.Range("AB1").Select

ActiveSheet.Range("$A$1:$AQ$1048576").AutoFilter Field:=28, Criteria1:="0"

W.Range("AI1").Select

ActiveSheet.Range("$A$1:$AQ$1048576").AutoFilter Field:=35, Criteria1:="0"

ActiveSheet.Range("A2:AQ" & Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlVisible).EntireRow.Delete

W.ShowAllData

W.Cells(1, 1).Select

MsgBox ("The rows have been successfully deleted!")

End If

Wend

Application.ScreenUpdating = True

End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Do you want to delete rows with 0s in columns AB and AI?
What error are you looking to avoid? I think it does not need the do loop statement.

VBA Code:
Sub deleteZeros()

Application.ScreenUpdating = False

Dim W As Worksheet

Set W = Sheets("Sheet1")

With W
    .Activate
    .Range("A1").AutoFilter Field:=28, Criteria1:="0"
    .Range("A1").AutoFilter Field:=35, Criteria1:="0"
    If WorksheetFunction.Subtotal(3, .Range("A:A")) > 1 Then
        .Range("A1").CurrentRegion.Offset(1, 0).Resize(.Rows.Count - 1).EntireRow.Delete
        .Range("A1").AutoFilter
        .Range("A1").Select
        MsgBox "The rows have been successfully deleted!"
    End If
End With

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
Good Morning! Your code works, yeah! However, I used loops to insert conditions that would undo the filters as soon as the values were removed. I will still need to undo it manually without them, but just having improved this procedure is a great help.

I have a doubt. How does this filtering process work? Thank you, see you next time.
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,034
Members
448,543
Latest member
MartinLarkin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top