简体   繁体   中英

How incorporate string comparison with count-If in VBA

I have an If condition that checks id number, start date and end date. To this if condition I want to add a string comparison that checks True or False values in two different columns. I was wondering I could make another application.worksheetfunction.countif for this?

    If Application.WorksheetFunction.CountIf(completed.Range(COLUMN_A_IN_COMPLETE & iTotRecsD), Val2) > 0 _
    And Application.WorksheetFunction.CountIf(completed.Range(COLUMN_B_IN_COMPLETE & iTotRecsB), Val1) > 0 _
    And Application.WorksheetFunction.CountIf(completed.Range(COLUMN_C_IN_COMPLETE & iTotRecsE), Val3) > 0 _
    And (StrComp(Val4, Val5) = 0) Then

This code is just running even if values are exactly the same.

Edit* Val4 is worksheet2.Cells(row 2, column4) Val5 is worksheet1.Cells(row 2, column4)

And Application.WorksheetFunction.CountIf(completed.Range(COLUMN_D_IN_COMPLETE & iTotRecsF), Val4) > 0 Then

Doesn't seem to work well for string comparison, I wonder why?

Instead of using multiple countIf, a single countIf's does the job.

    If (Application.WorksheetFunction.CountIfs(completed.Range(COLUMN_A_IN_COMPLETE & iTotRecsD), Val2, _
        completed.Range(COLUMN_B_IN_COMPLETE & iTotRecsB), Val1, _
        completed.Range(COLUMN_C_IN_COMPLETE & iTotRecsE), Val3, _
        completed.Range(COLUMN_D_IN_COMPLETE & iTotRecsF), Val4)) > 0 Then

It works now. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM