How to use If & CountIf on VBA

ExcelPupper

Board Regular
Joined
Mar 2, 2020
Messages
102
Office Version
  1. 2019
Platform
  1. Windows
I am trying to put IF & CountIF on the same formula on the code below but I get syntax error. What could be the problem?

VBA Code:
Sub CntNoEntry ()
Dim lr as long

Sheets(6).Range("A1").Formula = "=IF((COUNTIF("G8:G" & lr,"*No Entry*"))>0,"Provide Data","")"

End Sub
 
Last edited:

Some videos you may like

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
47,926
Office Version
  1. 365
Platform
  1. Windows
What could be the problem?
Quite a few things
  1. You have " before G8 which should not be there.
  2. After using lr in the formula you need an ampersand and to re-start the string with a quote mark.
  3. All quote marks the are to end up in the final formula need to be doubled-up in the code.
Try
VBA Code:
Sheets(6).Range("A1").Formula = "=IF((COUNTIF(G8:G" & lr & ",""*No Entry*""))>0,""Provide Data"","""")"
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
47,926
Office Version
  1. 365
Platform
  1. Windows
BTW, you don't actually need the >0 and there is an extra set of parentheses that is not needed.

VBA Code:
Sheets(6).Range("A1").Formula = "=IF(COUNTIF(G8:G" & lr & ",""*No Entry*""),""Provide Data"","""")"
 

ExcelPupper

Board Regular
Joined
Mar 2, 2020
Messages
102
Office Version
  1. 2019
Platform
  1. Windows
BTW, you don't actually need the >0 and there is an extra set of parentheses that is not needed.

VBA Code:
Sheets(6).Range("A1").Formula = "=IF(COUNTIF(G8:G" & lr & ",""*No Entry*""),""Provide Data"","""")"

Thanks for this. Most of the time, those "" placing are the reason for my errors.
Btw, I add the >0 because if the count of No Entry on G column is greater than zero, it should prompt the user by showing provide data on cell A2.
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
47,926
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Thanks for this. Most of the time, those "" placing are the reason for my errors.
That is common for a lot of people. :)


Btw, I add the >0 because if the count of No Entry on G column is greater than zero, it should prompt the user ..
It will still do that with or without the >0
In column A below you will see that one formula has the >0 & parentheses as you had them and one has them removed but both formulas return the same result.

20 07 21.xlsm
ABCDEFG
1Provide Data
2Provide Data
3
4
5There is no entry here
6
7No entry here either
8
Sheet6
Cell Formulas
RangeFormula
A1A1=IF((COUNTIF(G4:G8,"*No Entry*"))>0,"Provide Data","")
A2A2=IF(COUNTIF(G4:G8,"*No Entry*"),"Provide Data","")



.. and if I remove or replace the 'No Entry' values the formulas still return identical results to each other

20 07 21.xlsm
ABCDEFG
1 
2 
3
4
5Entry
6
7Data
8
Sheet6
Cell Formulas
RangeFormula
A1A1=IF((COUNTIF(G4:G8,"*No Entry*"))>0,"Provide Data","")
A2A2=IF(COUNTIF(G4:G8,"*No Entry*"),"Provide Data","")
 

ExcelPupper

Board Regular
Joined
Mar 2, 2020
Messages
102
Office Version
  1. 2019
Platform
  1. Windows
That is common for a lot of people. :)


It will still do that with or without the >0
In column A below you will see that one formula has the >0 & parentheses as you had them and one has them removed but both formulas return the same result.

20 07 21.xlsm
ABCDEFG
1Provide Data
2Provide Data
3
4
5There is no entry here
6
7No entry here either
8
Sheet6
Cell Formulas
RangeFormula
A1A1=IF((COUNTIF(G4:G8,"*No Entry*"))>0,"Provide Data","")
A2A2=IF(COUNTIF(G4:G8,"*No Entry*"),"Provide Data","")



.. and if I remove or replace the 'No Entry' values the formulas still return identical results to each other

20 07 21.xlsm
ABCDEFG
1 
2 
3
4
5Entry
6
7Data
8
Sheet6
Cell Formulas
RangeFormula
A1A1=IF((COUNTIF(G4:G8,"*No Entry*"))>0,"Provide Data","")
A2A2=IF(COUNTIF(G4:G8,"*No Entry*"),"Provide Data","")
Ohh nice. Does it mean if the CountIf formula returns a value greater than 0, it is regarded as TRUE? IS my understanding correct?
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
47,926
Office Version
  1. 365
Platform
  1. Windows
For an IF function test, zero counts as FALSE, any other numerical value (positive or negative) counts as TRUE
 

Watch MrExcel Video

Forum statistics

Threads
1,122,331
Messages
5,595,549
Members
413,996
Latest member
mabelO

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
Top