Countif giving me 0

tt.viquel

New Member
Joined
Apr 14, 2011
Messages
30
I have changed this countif formula into a macro: but it is giving me 0 instead of 10 does anyone know why??

Code:
=COUNTIF(KTPT80T!G:G,"Y")

Code:
Sub test()
Dim x As Long
x = Range("G" & Rows.Count).End(xlUp).Row
If x < 100 Then x = 100
Range("Z2") = Application.WorksheetFunction.CountIf(Range("G5:G" & x), "Y")
End Sub
 
Here is the code that is working for me.

Sub test()

Dim x As Long
x = Range("L" & Rows.Count).End(xlUp).Row
If x < 100 Then x = 100
Range("Z2") = Application.WorksheetFunction.CountIf(Range("L1:L" & x), "Y")

End Sub
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
thank you for your help, but i think it could be my excel then because it's giving me the error "excel has run out of resources can't process all formulas". So i know it is working I will use my other laptop.


Thank you again

quick question how would i change a counta statement e.g.
Code:
=COUNTA(KTPTZIT!$H:$H)-2
 
Upvote 0
Here's a thought...

Where are you putting this code?

It's possible it is not referring to the correct sheet.
Try adding the specific sheet name...

Rich (BB code):
Sub test()
 
Dim x As Long
With Sheets("Sheetname with Ys in Column L") 'Adjust sheet name as needed<ADJUST font Needed.< as SheetName>
    x = .Range("L" & Rows.Count).End(xlUp).Row
    If x < 100 Then x = 100
    .Range("Z2") = Application.WorksheetFunction.CountIf(.Range("L1:L" & x), "Y")
End With
End Sub

Hope that helps.
 
Upvote 0
I have tried to reference the sheetname and it is still giving me 0.:confused:
I am starting to think it is my excel now because the code is correct.
 
Upvote 0
So i have discovered what the problem is I wasnt specifying the right sheet to put the results. And I have amended that but now it is not returning anything:

Code:
[COLOR=black][FONT=Tahoma]Sub test()
Dim x As Long
x = Range("G" & Rows.count).End(xlUp).Row
With Sheets("KTPT80T")
If x > 100 Then x = 100
Sheets("Index").Range("Z4") = Application.WorksheetFunction.CountIf(Range("G5:G" & x), "Y")
End With
End Sub<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>[/FONT][/COLOR]
 
Upvote 0
nevermind my last response it now works I have fixed it. Now i wanted to find out how do i add other countif to the same sub, (6 in total) with different criterias as well e.g.

Code:
=COUNTIF(TABFR73!J:J,"Y")

Or it would be wiser to do subs for the each countif??
 
Upvote 0
Thought I would share this. Does pretty much the same thing, but on multiple worksheets.

Sub newtest()

Dim x As Long
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
ws.Select
x = ws.Range("L" & Rows.Count).End(xlUp).Row
If x > 100 Then x = 100
ws.Range("Z4") = Application.WorksheetFunction.CountIf(Range("L1:L" & x), "Y")
Next ws

End Sub
 
Upvote 0

Forum statistics

Threads
1,225,156
Messages
6,183,239
Members
453,152
Latest member
ChrisMd

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