Specifying range for COUNTIF

coop123

Board Regular
Joined
Dec 18, 2018
Messages
66
Office Version
  1. 365
Hello

As part of a macro I want to put a countif formula in. The code below works but, the problem is the starting cell this can change each tiem I run report.

For example cell J66 may be J80 and cell B66 may be B80 next time. The find will put me to that starting row, I want to know how to get that into my formula. I've tried ranges without any success.

Any ideas would be much appreciated.

Macro Code

Range("A3").Select

Cells.Find(What:="EHP", After:=ActiveCell, LookIn:=xlFormulas2, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate

Selection.End(xlDown).Select
ListEnd2 = ActiveCell.Offset(0, 0).Row

Range("J3").Select
Selection.End(xlDown).Select
Selection.Offset(6, 0).Select

ActiveCell.Formula = _
"=COUNTIF(J66:J" & ListEnd2 & ",""Qualifies"")/COUNTA(B66:B" & ListEnd2 & ")"

Selection.NumberFormat = "0%"

Selection.Offset(1, 0).Select
ActiveCell.Formula = "=COUNTIF(J66:J" & ListEnd2 & ",""Qualifies"")"
Selection.Offset(1, 0).Select
ActiveCell.Formula = "=COUNTIF(J66:J" & ListEnd2 & ",""No"")"


coop123
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
You don't explain how you arrived at J66--is that supposed to the cell that resulted from Find? If so, what is B66? Same row but in column B? Let's assume so for this example but you need to confirm.

Assign the result of Find to a Range variable, then use the address of that range.

Also I recommend against selecting cells then using ActiveCell or Selection to refer to them later. It is better to be explicit. I did not go through your code to change this, just something to consider.

Also, if you use CODE tags around your VBA code, the spacing will be preserved and it will be much easier to read.
VBA Code:
   Dim Found As Range
   
  
   Set Found = Cells.Find(What:="EHP", _
                          After:=Range("A3"), _
                          LookIn:=xlFormulas2, _
                          LookAt:=xlPart, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlNext, _
                          MatchCase:=False, _
                          SearchFormat:=False)
   
   Found.End(xlDown).Select
   ListEnd2 = ActiveCell.Row
   
   Range("J3").Select
   Selection.End(xlDown).Select
   Selection.Offset(6, 0).Select
   
   ActiveCell.Formula = _
   "=COUNTIF(" & Found.Address(False, False) & ":J" & ListEnd2 & ",""Qualifies"")/COUNTA(" & Cells(Found.Row, "B").Address(False, False) & ":B" & ListEnd2 & ")"
   
   Selection.NumberFormat = "0%"
   
   Selection.Offset(1, 0).Select
   ActiveCell.Formula = "=COUNTIF(" & Found.Address(False, False) & ":J" & ListEnd2 & ",""Qualifies"")"
   Selection.Offset(1, 0).Select
   ActiveCell.Formula = "=COUNTIF(" & Found.Address(False, False) & ":J" & ListEnd2 & ",""No"")"
 
Upvote 0
Solution
Hello

Thanks for you response. The code works perfectly, the assumptions you made about J66 and B66 were correct. I'll take on board you advice about ActiveCell or Selection and using CODE tags.

Thanks again

coop123
 
Upvote 0

Forum statistics

Threads
1,214,387
Messages
6,119,222
Members
448,877
Latest member
gb24

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