0

I am trying to prevent a subroutine from running if the column "L" it's working with doesn't have any value "Yes"

    If Range("L2:L").WorksheetFunction.CountIf(col, "Yes") > 0 Then
    Call DeleteSubparts
    End If

There's a problem with my if function and I'm not sure what am I writing wrong. Could someone, please, help me?

1

1 Answer 1

1

I believe you want to do this:

If WorksheetFunction.CountIf(Range("L2:L100"), "Yes") > 0 Then
    DeleteSubparts
End If

The range goes inside the function, thought you should reference both the workbook and worksheet like this:

If WorksheetFunction.CountIf(Workbooks("WorkbookName").Worksheets("WorksheetName").Range("L2:L100"), "Yes") > 0 Then
    DeleteSubparts
End If
Sign up to request clarification or add additional context in comments.

3 Comments

The syntax seems right, but it doesn't count what I need. It still executes the subroutine I am calling when what I'm trying to do is to not call deletesubparts if column "L" has no value "Yes"
@Eduards the syntax should count the "Yes" in that column, maybe is the reference. If you don't reference both workbook & worksheet it will count on the active sheet which could be totally a different sheet that the one you intend.
Hm. Either you write Range("L:L") or something like Range("L2:L" & lastRow)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.