How Autofilter works? [duplicate] - vba

This question already has answers here:
Handle "No cells were found" error when filtered range is empty
(4 answers)
Closed 5 months ago.
I got the following code,
Set V_RangeNamePlus = Range(V_RangeNameTop, LastRowFilter1)
V_RangeNamePlus.AutoFilter
ws1.Range(V_RangeNameTop, LastRowFilter1).AutoFilter Field:=LcolNRPlus, Criteria1:=""
ws1.Range(V_RangeNameTop, LastRowFilter1).AutoFilter Field:=FirsWhiteColNr, Criteria1:="<>"
V_RangeNameWhite.Resize(V_RangeNamePlus.Rows.Count-1).SpecialCells(xlCellTypeVisible).Rows.ClearContents
V_RangeNamePlus.AutoFilter
My question is regarding the case when the Criteria 1 & 2 are not available for selection at all or just one of them, what this line of code does, or how the autofilter works in these case
V_RangeNameWhite.Resize(V_RangeNamePlus.Rows.Count-1).SpecialCells(xlCellTypeVisible).Rows.ClearContents

Use on error resume next like this
On error resume next 'in case there are no visible rows
V_RangeNameWhite.Resize(V_RangeNamePlus.Rows.Count-1).SpecialCells(xlCellTypeVisible).Rows.ClearContents
On error goto 0
The perfect solution would be
Dim rgVisibleRows As Range
On Error Resume Next 'in case there are no visible Rows.
Set rgVisibleRows = V_RangeNameWhite.Resize(V_RangeNamePlus.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not rgVisibleRows Is Nothing Then
rgVisibleRows.Rows.ClearContents
End If
as in the previous solution the error could also result from clearing the contents.

Related

How to delete rows based on empty cell value in VBA [duplicate]

This question already has answers here:
Code in VBA loops and never ends. How to fix this?
(2 answers)
Closed 5 years ago.
I am trying to delete entire rows based on whether the cell value in the D column is NULL or not. My entire code so far is:
Sub DeleteNULL()
Dim i As Long
For i = 2 To 119713
If IsEmpty(Range("Di")) = True Then
Rows([i]).EntireRow.Delete
Else
If IsEmpty(Range("Di")) = False Then
Next i
End If
End Sub
I keep getting compile errors, either If without Else or Next without For, how should I fix this?
Thanks in advance.
A few things:
Placement of a lot of syntax is off.
When adding or deleting rows, you need to loop backwards based on how Excel handles these events.
See code below:
Sub DeleteNULL()
Dim i As Long
For i = 119713 To 2 Step -1
If IsEmpty(Range("D" & i)) Then Rows([i]).EntireRow.Delete
Next i
End Sub

Can't get excel macro to return after a for loop [duplicate]

This question already has answers here:
VBA macro to delete rows quickly
(4 answers)
Closed 5 years ago.
I'm trying to make a little code that deletes an entire row when certain text is written in a cell.
Sub Delete_Rows()
For Each c In Range("B1:B20").Cells
If c.Value = "text" Then
c.EntireRow.Delete
End If
Next c
End Sub
This is what I wrote in my excel sheet
The problem is when I run it and the condition to delete a cell is met, excel automatically scrolls to the next row without evaluating the current cell, it skips like this
I run it again and only "text" from numbers 4 and 9 remain
After I run it a third time it finally deletes every cell with "text" written in it.
I tried to use a while loop instead, offset the selection, use an integer to subtract the loop iteration and several other ways and have had no luck.
Any ideas?
Two options are:
delete from the bottom up, or
delete all relevant rows at once (shown below):
Sub Delete_Rows()
Dim deleteRange As Range
For Each c In Range("B1:B20")
If c.Value = "text" Then
If deleteRange Is Nothing Then
Set deleteRange = c
Else
Set deleteRange = Union(c, deleteRange)
End If
End If
Next c
deleteRange.EntireRow.Delete
End Sub

Excel VBA: Delete blank columns & entire rows if specific columns are blank on specific worksheet not working

My problem is the macro won't work on a specified worksheet, only the active one. I have two subroutines for deleting entire columns, and then deleting entire rows if specific columns are blank. I want to make it work for a specific worksheet, which I understood to be With Worksheets("OutPut") but it still culls the active worksheet.
It works as intended so long as the active worksheet is selected.
Sub DeleteBlankColumns()
With Worksheets("OutPut")
Set MyRange = Worksheets("OutPut").UsedRange
For iCounter = MyRange.Columns.Count To 1 Step -1
If Application.CountA(Columns(iCounter).EntireColumn) = 0 Then
Columns(iCounter).Delete
End If
Next iCounter
End With
End Sub
And
Sub QuickCull()
With Worksheets("OutPut")
On Error Resume Next
Columns("B").SpecialCells(xlBlanks).EntireRow.Delete
On Error Resume Next
Columns("C").SpecialCells(xlBlanks).EntireRow.Delete
On Error Resume Next
Columns("D").SpecialCells(xlBlanks).EntireRow.Delete
On Error Resume Next
Columns("E").SpecialCells(xlBlanks).EntireRow.Delete
End With
End Sub
There's a button to Call both of them, which again, will work if the worksheet I want to transform is active. For reference, this is intended to be appended on an existing company macro, so simply running it on the worksheet when it's active won't work.
Thank you!
For the first code sample,
If Application.CountA(Columns(iCounter).EntireColumn) = 0 Then
Columns(iCounter).Delete
should be
If Application.CountA(.Columns(iCounter).EntireColumn) = 0 Then
.Columns(iCounter).Delete
(a "." before Columns, to specify the sheet)
Same thing for the second code sample

Excel VBA: Detecting empty cell to end loop

In Excel 2007 I have imported data consisting of a column of rows. I wish to set a VBA script to read through the data from top to bottom, using a loop. The loop should continue to iterate while the cell in the sixth column is not empty.
I have tried:
Do While IsEmpty(Cells(i,6)) = False
...
i = i + 1
Loop
...and also
Do While Cells(i, 6).Value <> ""
...
i = i + 1
Loop
The loop does what it is supposed to but an error ensues because the program goes on reading below the block of data. (I inserted "On Error GoTo Catch" in the loop, then after the loop "Catch: MsgBox Cells(i, 6).Value & "Error" so as to confirm that an error occurs).
There is nothing in the loop that would affect Cells(i,6).
There are many questions in Stack Overflow about empty cells but the answers tend to avoid the issue how to detect an empty cell to make a loop end. Is there a way of doing this?
Provided you're on the right sheet (replace below) the code you posted should work
i = 1
Do While sheets("sheet1").Cells(i, 6).Value <> ""
i = i + 1
Loop
Could you post a screenshot of the column end? Your error handling may be preventing the error message too, try disabling that temporarily. Do the columns all end on a certain row? You could specify "do while not blank AND row < end row".

Next without For error in Select Case

I'm trying to write a macro that processes data but skips over data points that meet specific criteria. Here's a simplified version of my code:
Set gData = ThisWorkbook.Sheets("Dashboard").Range("E3:E16")
For Each cell In gData
Select Case cell.Value
Case Is = 20
Next cell 'error thrown here
Case Is > 20
'Do stuff
End Select
Next
The compiler is throwing a "Next without For" error in the location noted above. I haven't been able to find a working solution to this problem. Please help!
Set gData = ThisWorkbook.Sheets("Dashboard").Range("E3:E16")
For Each cell In gData
Select Case cell.Value
Case Is > 20
'Do stuff
End Select
Next