Multiple Conditions on table with If Statement in Macro

Nerdalert314

New Member
Joined
Mar 28, 2018
Messages
23
Hello all!

I am trying to use and If statement with a table. If eligibility is before today and if complete says no I am wanting to highlight. My problem is when there is nothing to highlight it is highlighting the whole table. It is not proceeding on to the else statement. On another table I am wanting to highlight anything for this month and next month that says no. Same problem when nothing says no. Any help would be appreciated! .... I do have some repetitive stuff in there trying to get this to work. I am very new to even reading code so don't expect this to be correct.


Code:
[COLOR=#0000ff]ActiveSheet.ListObjects("Table3").Range.AutoFilter Field:=4, Criteria1:= _
        "<=" & Date, Operator:=xlAnd
 
If Evaluate(WorksheetFunction.CountIf(Range("C1:C3000"), "NO")) > 0 Then
    ActiveSheet.ListObjects("Table3").Range.AutoFilter Field:=3, Criteria1:= _
        "=NO"
    Range("Table3").Select
    Selection.Style = "Accent4"
    ActiveSheet.ListObjects("Table3").Range.AutoFilter Field:=3
    Columns("A:A").Select
    With Selection
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Columns("A:A").Select
    With Selection.Interior
        .PatternThemeColor = xlThemeColorAccent3
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    With Selection.Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
        Range("Table3").Select
    With Selection.Font
        .Name = "Arial"
        .Size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With
    With Selection
        .VerticalAlignment = xlBottom
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
ActiveSheet.ListObjects("Table3").Range.AutoFilter Field:=3
ActiveSheet.ListObjects("Table3").Range.AutoFilter Field:=4
 
 Range("B2").Select
 
Else
ActiveSheet.ListObjects("Table3").Range.AutoFilter Field:=3
ActiveSheet.ListObjects("Table3").Range.AutoFilter Field:=4
    Range("B2").Select
End If[/COLOR]
 
Now it always skips. It is acting like quantity is always 0.

Code:
Sub test()
Dim Qty As Long
ActiveSheet.ListObjects("Table3").Range.AutoFilter Field:=4, Criteria1:= _
        "<=" & Date, Operator:=xlAnd
On Error Resume Next
Qty = WorksheetFunction.CountIf(Range("C1:C8000").SpecialCells(xlVisible), "NO")
On Error GoTo 0
If Qty > 0 Then
    ActiveSheet.ListObjects("Table3").Range.AutoFilter Field:=3, Criteria1:= _
        "=NO"
 Range("Table3").Select
    Selection.Style = "Accent4"
    ActiveSheet.ListObjects("Table3").Range.AutoFilter Field:=3
    Columns("A:A").Select
    With Selection
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Columns("A:A").Select
    With Selection.Interior
        .PatternThemeColor = xlThemeColorAccent3
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    With Selection.Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
        Range("Table3").Select
    With Selection.Font
        .Name = "Arial"
        .Size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With
    With Selection
        .VerticalAlignment = xlBottom
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
ActiveSheet.ListObjects("Table3").Range.AutoFilter Field:=3
ActiveSheet.ListObjects("Table3").Range.AutoFilter Field:=4
 
 Range("B2").Select
 
Else
ActiveSheet.ListObjects("Table3").Range.AutoFilter Field:=3
ActiveSheet.ListObjects("Table3").Range.AutoFilter Field:=4
    Range("B2").Select
End If
End Sub
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Once the first filter is in place, do you have any visible cells in col C with a value of NO?
 
Upvote 0
Check that they don't have any leading or trailing spaces.
 
Upvote 0
There isn't :confused: Here are columns C and D
Complete?Eligibility
YES02/27/18
NO02/27/18
YES03/08/18
YES03/09/18
YES03/15/18
Researched Incomplete03/22/18
YES03/23/18
NO03/30/18
YES04/02/18
NO04/02/18
NO04/26/18
NO05/07/18
NO05/10/18
NO05/22/18
NO03/31/28
<colgroup><col width="145" style="width: 109pt; mso-width-source: userset; mso-width-alt: 5302;"> <col width="81" style="width: 61pt; mso-width-source: userset; mso-width-alt: 2962;"> <tbody> </tbody>
 
Upvote 0
I'm guessing it's got something to do with the fact that it's a table.
As I don't have much experience with tables, I'm not sure I can help much more.
 
Upvote 0
Does this work for you?
Code:
Sub TEST()
Dim Qty As Long
Dim Ws As Worksheet
Set Ws = ActiveSheet
   Ws.ListObjects("Table3").Range.AutoFilter Field:=4, Criteria1:= _
        "=" & Date, Operator:=xlAnd
    Ws.ListObjects("Table3").Range.AutoFilter Field:=3, Criteria1:= _
        "=NO"
    On Error Resume Next
    Qty = Ws.ListObjects("Table3").DataBodyRange.SpecialCells(xlVisible).Count
    On Error GoTo 0
    If Qty > 0 Then
    Range("Table3").Select
    Selection.Style = "Accent4"
    Ws.ListObjects("Table3").Range.AutoFilter Field:=3
    Columns("A:A").Select
    With Selection
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Columns("A:A").Select
    With Selection.Interior
        .PatternThemeColor = xlThemeColorAccent3
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    With Selection.Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
        Range("Table3").Select
    With Selection.Font
        .Name = "Arial"
        .size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With
    With Selection
        .VerticalAlignment = xlBottom
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
Ws.ListObjects("Table3").Range.AutoFilter Field:=3
Ws.ListObjects("Table3").Range.AutoFilter Field:=4
 
 Range("B2").Select
 
Else
Ws.ListObjects("Table3").Range.AutoFilter Field:=3
Ws.ListObjects("Table3").Range.AutoFilter Field:=4
    Range("B2").Select
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,777
Messages
6,126,838
Members
449,343
Latest member
DEWS2031

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