Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Count how many instances of an item appear in a range

I have a spreadsheet with 4 columns: name,city state, and age.
Let's say I have 350 rows. I have sorted the rows by State. What I
want to do is count the number of times each state appears in the
state column and place the results somewhere else.
Example:
John Doe California
Pete Smith California
Dan Doe California
Jane Dill Idaho
Bill Will Nevada
Dan John Nevada
etc.....

I need a macro that will place in another location (let's say starting
a cell G5) the following
California 3
Idaho 1
Nevada 2

Thanks for any help you can give.
Jonco
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 563
Default Count how many instances of an item appear in a range

Do you really want a macro?
You could use Filter/Unique to get a list of unique state names (if needed)
Let's say the first unique state name is pasted to G5
If you data is in A1:E400 with state in column C
In H5 enter =COUNTIF(C1:C400,G5) to get the count
best wishes
--
Bernard Liengme
Microsoft Excel MVP
http://people.stfx.ca/bliengme

"Jon" wrote in message
...
I have a spreadsheet with 4 columns: name,city state, and age.
Let's say I have 350 rows. I have sorted the rows by State. What I
want to do is count the number of times each state appears in the
state column and place the results somewhere else.
Example:
John Doe California
Pete Smith California
Dan Doe California
Jane Dill Idaho
Bill Will Nevada
Dan John Nevada
etc.....

I need a macro that will place in another location (let's say starting
a cell G5) the following
California 3
Idaho 1
Nevada 2

Thanks for any help you can give.
Jonco


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 913
Default Count how many instances of an item appear in a range

On Sun, 17 Jan 2010 13:50:41 -0800 (PST), Jon
wrote:

I have a spreadsheet with 4 columns: name,city state, and age.
Let's say I have 350 rows. I have sorted the rows by State. What I
want to do is count the number of times each state appears in the
state column and place the results somewhere else.
Example:
John Doe California
Pete Smith California
Dan Doe California
Jane Dill Idaho
Bill Will Nevada
Dan John Nevada
etc.....

I need a macro that will place in another location (let's say starting
a cell G5) the following
California 3
Idaho 1
Nevada 2

Thanks for any help you can give.
Jonco


For this problem you could use a pivot table.

But if you prefer to have a macro, here is one that you can try;

Sub test()
With Worksheets("Sheet1")
first_row = 1
state_column = 2
Set output_cell = .Range("G5")
last_row = .Cells(first_row, state_column).End(xlDown).Row
MsgBox last_row
n = 0
k = 0
For i = first_row To last_row
n = n + 1
If .Cells(i + 1, state_column) < .Cells(i, state_column) Then
output_cell.Offset(k, 0) = .Cells(i, state_column)
output_cell.Offset(k, 1) = n
n = 0
k = k + 1
End If
Next i
End With
End Sub

Hope this helps / Lars-Åke


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
find all instances of a search item and put result in a single cel Art Excel Worksheet Functions 5 March 24th 10 12:46 AM
Count Instances Chad Wodskow[_2_] Excel Worksheet Functions 4 March 4th 10 08:29 PM
Item Count vs Item Cost Michael Koerner Excel Programming 2 October 31st 09 09:16 AM
subtract one cell from each cell in a range and count instances tworrall Excel Worksheet Functions 2 October 15th 09 05:31 PM
Count Instances Ken Excel Discussion (Misc queries) 2 April 2nd 05 12:41 AM


All times are GMT +1. The time now is 03:47 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"