VBA If cell not empty - ignore formulas

Kra

Board Regular
Joined
Jul 4, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi all!

I have a macro which sets auto filters, but it is not working in one case and I am not able to understand why. Here is the part which is not working.
If there is any cell with "X" in column U and any cell in column O is not empty (there are formulas in column O, but result of formula is ""), then apply filters. Else show message box.

Now if there is no "X" in column U it works fine, it shows msg box. But if there is "X" and all cells in column O are blank (only formulas) it still applies the filters, but it should show message box instead. Any idea how to fix it?

VBA Code:
Sub PricingTransferMPG()


Dim lngLastRow As Long

With ActiveSheet
    lngLastRow = .range("A" & .Rows.Count).End(xlUp).Row


 With .range("$A$3:$BB" & lngLastRow)

'THIS LINE IS NOT WORKING    
    If WorksheetFunction.CountIf(range("U3:U" & lngLastRow), "X") > 0 And WorksheetFunction.CountIf(range("O3:O" & lngLastRow), "<>") > 0 Then
        .AutoFilter Field:=21, Criteria1:="X"
        .AutoFilter Field:=15, Criteria1:="<>"
        .AutoFilter Field:=17, Criteria1:="1"
         Worksheets("SUM").range("AX3:BA" & lngLastRow).Copy
         Worksheets("LSMW ZVOL MAT PR GRP").range("A4").PasteSpecial xlPasteValues
    
      Else
        MsgBox "No MPG condition exist.", vbInformation, "No data"
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
It works when this line

VBA Code:
If WorksheetFunction.CountIf(range("U3:U" & lngLastRow), "X") > 0 And WorksheetFunction.CountIf(range("O3:O" & lngLastRow), "<>") > 0 Then

is changed to:

VBA Code:
With .range("$A$3:$BB" & lngLastRow)
        If WorksheetFunction.CountIf(range("U3:U" & lngLastRow), "X") > 0 And WorksheetFunction.CountA(range("O3:O" & lngLastRow)) - WorksheetFunction.CountBlank(range("O3:O" & lngLastRow)) > 0 Then

If somebody knows more clear and pretty solution then let me know!
 
Upvote 0
Solution
Try
VBA Code:
.AutoFilter Field:=15, Criteria1:="<>" & ""
 
Upvote 0

Forum statistics

Threads
1,214,789
Messages
6,121,593
Members
449,038
Latest member
Arbind kumar

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