How to use If & CountIf on VBA

ExcelPupper

Board Regular
Joined
Mar 2, 2020
Messages
112
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:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
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"","""")"
 
Upvote 0
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"","""")"
 
Upvote 0
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.
 
Upvote 0
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","")
 
Upvote 0
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?
 
Upvote 0
For an IF function test, zero counts as FALSE, any other numerical value (positive or negative) counts as TRUE
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,382
Members
448,889
Latest member
TS_711

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