Add second criteria to VBA check IF/then statement

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
886
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am looking to add a second criteria to the solution I was provided. I thought this could work but I get an error code. Would someone be able to see what I am doing wrong? Below is an example of my data.


VBA Code:
'check if any dates and records in H in order to evaluate if not end sub
If Application.WorksheetFunction.CountIf(WsDIST.Range("B:B")) = 0 And WsDIST.Range("H" & rows.count).End(xlUp).row <= 1 Then

1734995962200.png

1734996266629.png
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
In the CountIf function, you are missing a counting criterion after WsDIST.Range(“B:B”).

Artik
 
Upvote 0
Okay got it but one issue. I need the second part of the code to be if anything in column C <> YY or ZZ. So If Column B doesn't equal today's date (WORKS) and doesn't equal YY or ZZ (NEW). Replacing the H criteria.

VBA Code:
d = Format(Date, "yyyymmdd") + 0

'check if any ex dates to evaluate if not end sub
If Application.WorksheetFunction.CountIf(WsDIST.Range("B:B"), d) = 0 And WsDIST.Range("H" & rows.count).End(xlUp).row <= 1 Then
 
Upvote 0
The following condition is:
There is no today's date in column B AND there is no “YY” in column C AND there is no “ZZ” in column C.
VBA Code:
    Dim d As Long
    Dim WF As WorksheetFunction
    Dim lRow As Long

    Set WF = Application.WorksheetFunction

    d = Format(Date, "yyyymmdd") + 0
    lRow = WsDIST.Range("H" & Rows.Count).End(xlUp).Row

    If WF.CountIf(WsDIST.Range("B1:B" & lRow), d) = 0 And WF.CountIf(WsDIST.Range("C1:C" & lRow), "YY") = 0 And WF.CountIf(WsDIST.Range("C1:C" & lRow), "ZZ") = 0 Then
    '(...)
    End If
Artik
 
Upvote 0
I think I may have miss explained it so my bad in advance. Say if Column B has today's date, but it doesn't contain YY or ZZ in column C then it should trigger the warning of no activity. If today's date is in column B with a YY or ZZ then it should proceed to my else statement.

VBA Code:
d = Format(Date, "yyyymmdd") + 0
Set WF = Application.WorksheetFunction
lRow = WsDIST.Range("H" & rows.count).End(xlUp).row

'check if any activity if not kick a warning
If WF.CountIf(WsDIST.Range("B1:B" & lRow), d) = 0 And WF.CountIf(WsDIST.Range("C1:C" & lRow), "YY") = 0 And WF.CountIf(WsDIST.Range("C1:C" & lRow), "ZZ") = 0 Then

With WsRUN
    .Activate
End With
   
MsgBox "No Activity Today"
   
Else
 
Upvote 0

Forum statistics

Threads
1,225,161
Messages
6,183,273
Members
453,155
Latest member
jaydenwalden

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