How to set a skip a step when filter result is 0?

Martin1991

New Member
Joined
Nov 14, 2017
Messages
24
Hello,
I m trying to figure out, how to set my formula to skip a similar step after filtering out any values with 0 results. I m searching second day and can´t find any tip for this specific case. I´m beginner with VBA, so i hope it´ll be easy question for you.

To clarify, i got small simple macro, which should fill special cells with some "memo" according the criterial values.
1. Macro filters out, what i need.
2. Fills special cells with "memo".
3. Reset my filters.

Problem is step 2. When filter can´t find defined criteria (because i work daily with another documents, and sometimes there are those criterias, sometimes not), it fills up not only special cells, but every single cell (not only visible) in current column except the first raw, which is header.

Code:
Sub HQstatuses()

    Sheets("Data").Select
    ActiveSheet.UsedRange.AutoFilter Field:=88, Criteria1:=Array( _
        "HQ can´t support*", "HQ pending - shortage*"), Operator:=xlFilterValues
    
    ActiveSheet.UsedRange.AutoFilter Field:=65, Criteria1:=Array( _
        "Y"), Operator:=xlFilterValues
        
    x = ActiveWorkbook.Sheets("Data").Range("CA1").CurrentRegion.Rows.Count


    vzoreccheck = "oow/cid-HQ pending - shortage, please choose a sub with different color"
    
    Range(Cells.SpecialCells(xlCellTypeVisible)(2, 79), Cells.SpecialCells(xlCellTypeVisible)(x, 79)).Value = vzoreccheck
    ActiveSheet.Calculate
    ActiveSheet.ShowAllData
    
End Sub


What kind of changes would you recommend to me? I think, its about some IF condition, tryed to set it by IF with another formula "y": y = SpecialCells(xlCellTypeVisible).Count ---> If y > 1 Then calculate, but it doesn´t work.

I´m new to the Forum, read all board rules, so i hope its all understandable. :)
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Welcome to the board.
Give this a go
Code:
Sub HQstatuses()

    Sheets("Data").Select
    x = Cells.Find("*", LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    ActiveSheet.UsedRange.AutoFilter Field:=39, Criteria1:=Array( _
        "HQ can´t support*", "HQ pending - shortage*"), Operator:=xlFilterValues
    
    ActiveSheet.UsedRange.AutoFilter Field:=35, Criteria1:=Array( _
        "Y"), Operator:=xlFilterValues
        


    vzoreccheck = "oow/cid-HQ pending - shortage, please choose a sub with different color"
    
    On Error Resume Next
    Range("CA2:CA" & x).SpecialCells(xlCellTypeVisible).Value = vzoreccheck
    On Error GoTo 0
    ActiveSheet.Calculate
    ActiveSheet.ShowAllData
    
End Sub
 
Upvote 0
I tryed it and seems it works properly. I will test it for a few days and will post, if it doesn´t make any errors.
But still can´t get the point, why just this formula. On Error means "if finds no data"?
Anyway thanks a lot. Right now it works well. :)
 
Upvote 0
Code:
On Error Resume Next
Means that if there are no visible cells in the range the code will simply ignore any errors & skip to the next line of code. But you should always follow this as soon as possible with
Code:
On Error GoTo 0
Which then re-sets the error handler back to it's default setting
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
Glad to help & thanks for the feedback

Can i ask u one more think even its kind of out of topic? But its still connected with this makro.

Code:
Dim x    x = ActiveWorkbook.Sheets("Sheet1").Range("BZ1").CurrentRegion.Rows.Count
    
    For Each c In Worksheets("Sheet1").Range("BZ1:BZ5000")
     If c.Value > 88 Then
        Range(Cells.SpecialCells(xlCellTypeVisible)(2, 78), Cells.SpecialCells(xlCellTypeVisible)(x, 78)).EntireRow.Select
        Selection.Style = "Bad"
     End If

I found only solutions on delete rows, which are in some condition, but not for this filling. I understand the problem is in my range, but not sure.
If i want this code to fill only rows with value (in that specified column BZ) which is higher than 88, then should i change condition or range settings? For now its filling every filtered raw and not rows with c.Value.
 
Upvote 0
Try
Code:
Dim x, c
x = ActiveWorkbook.Sheets("ma").Range("Z1").CurrentRegion.Rows.Count
    
    For Each c In Worksheets("ma").Range("Z2:Z5000")
     If c.Value > 88 Then
        c.EntireRow.Style = "Bad"
     End If
    Next
 
Upvote 0
Well, its doing everything right, but there is small problem, i forgot to mention. Before this part of macro, there is step with filter: (filter Y, from Y/N options), and it works for every non-filtered cells aswell. I hope I´m writting it understandable. Thats, why i tryed to get in that SpecialCells(xlCellTypeVisible).
 
Upvote 0

Forum statistics

Threads
1,215,328
Messages
6,124,299
Members
449,149
Latest member
mwdbActuary

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