Solved

vba get rows number in each group in excel

Posted on 2013-01-02
5
499 Views
Last Modified: 2013-01-02
I need help in VBA code. Here is my excel, the first column is order ID, the second column Item is kind of group on ID column, how can I get number of each group ( same as maximum number in Item column) and write to the calculate column ( only the first row of each group) ? Thanks,

ID     Item     calculate

1       1          1

2       1          2

2       2      

5      1          4

5      2        

5      3

5      4
0
Comment
Question by:HemlockPrinters
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 50
ID: 38738458
Hello,

This can be done without VBA, just with a formula. In the Calculate column try

=IF(A2<>A1,COUNTIF(A:A,A2),"")

starting in row 2 and copy down.

This assumes that the data is sorted by the ID column and that there are no blank rows in the data. See attached sample file.

cheers, teylyn
BlockRows.xlsx
0
 

Author Comment

by:HemlockPrinters
ID: 38738673
Thanks, this works very well. I have another question:

Here is my excel, the first column is order ID, the second column Qty is kind of group on ID column, how can I get sum  of each group and write to the calculate column ( only the first row of each group) ? Thanks,

ID     Qty    calculate

1       1          1

2       3         5

2       2      

5      1          6

5      3      

5      1

5      1
0
 
LVL 10

Accepted Solution

by:
SANTABABY earned 500 total points
ID: 38738677
Just in case your IDs are not sorted, the following can help:
Formula for row2, copy & paste for other rows.
=IF(MATCH(A2,A:A,0)=ROW(),COUNTIF(A:A,A2),"")
0
 
LVL 10

Expert Comment

by:SANTABABY
ID: 38738686
Try this formula for second question:
=IF(MATCH(A2,A:A,0)=ROW(),SUMIF(A:A,A2,B:B),"")
0
 
LVL 16

Expert Comment

by:terencino
ID: 38738725
If you actually did want VBA instead of teylyn's excellent suggestion which I would also recommend, here is an update of the file with sample code
Sub CountID()
For Each c In Range("A2:A" & ActiveCell.SpecialCells(xlCellTypeLastCell).Row)
  i = i + 1
  j = j + 1
  If c.Value <> c.Offset(1, 0).Value Then
    Range("A2").Offset(j - i, 2).Value = i
    i = 0
  End If
Next c
End Sub

Open in new window

BlockRows.xlsm
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question