Count Multiple Items in Same Column

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
553
Hi - How do I add another variable to this code? I want it to count how many times "Apple" or "Pear" appear in the column?


Code:
With Sheet1    Dim x As Long
   x = .Range("A" & Rows.Count).End(xlUp).Row


  Dim cnt As Integer
  cnt = 0
  For i = 1 To x
    If .Cells(i, 1).Value = "Apple" Then
      cnt = cnt + 1
    End If
  Next i




.Range("C1").Value = cnt


End With
 

Some videos you may like

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
53,698
Office Version
365
Platform
Windows
Change your IF line to:
Code:
If .Cells(i, 1).Value = "Apple" [COLOR=#ff0000]Or .Cells(i, 1).Value = "Pear"[/COLOR] Then
 
Last edited:

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
9,196
This one line of code will do it for "Apple":
Code:
Sub test()
    Range("C1") = WorksheetFunction.CountIf(Range("A:A"), "Apple")
End Sub
 

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
553
@mumps I was using .Range("C1") = Application.WorksheetFunction.CountIf(.Range("A2:A" & x), "Apples") + Application.WorksheetFunction.CountIf(.Range("A2:A" & x), "Pears") and it worked, but i could figure out the other code. @Joe4 I tried the Or, but forgot the range part.

thanks !!

if i have a sheet named "apples", how would i reference that in the code? so basically count if the sheet name appears in A:A?
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
9,196
Do you want to count how many times the sheet name "apple" appears in column A and also the sheet name "pears" appears?
 
Last edited:

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
53,698
Office Version
365
Platform
Windows
if i have a sheet named "apples", how would i reference that in the code?
If I understand what you are asking, instead of:
Code:
With Sheet1
you could use:
Code:
With Sheets("apples")
 

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
553
sorry, how would i do something like: Range("C1") = WorksheetFunction.CountIf(.Range("A:A"), SheetName)
 

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
553
Ideally i'd like to have a For Each ws in Worksheets function. So the code would cycle through each sheetname and put the count in C1 of the corresponding sheet.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
53,698
Office Version
365
Platform
Windows
If you set a Worksheet variable named "ws" and are looping through it, you can reference each name like:
Code:
ws.Name
 

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
553
So this code is working great. My question is how would I split out the ws? Meaning the these ws.Cells would be in ws1 and ws.Name/ws.Range("C1") would be in ws2?


Code:
[COLOR=#333333]
Dm ws As Worksheet
[/COLOR][COLOR=#333333]With Sheet1 [/COLOR][COLOR=#333333]
   Dim x As Long[/COLOR]   
x = .Range("A" & Rows.Count).End(xlUp).Row
End With

For each ws in Worksheets
  Dim cnt As Integer
  cnt = 0
  For i = 1 To x
    If ws.Cells(i, 1).Value = ws.Name and ws.Cells(i,2).Value = "Football" Then
      cnt = cnt + 1
    End If
  Next i

ws.Range("C1").Value = cnt

Next
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,102,630
Messages
5,488,004
Members
407,617
Latest member
Samanthad2007

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top