Solved

vba get rows number in each group in excel

Posted on 2013-01-02
5
475 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
5 Comments
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dynamic Excel Input Form 29 34
EXCEL 2013 question. 4 27
Auto Populate Day Month  2 digit Date 4 19
Select/Copy row and pasting it lower in sheet 7 19
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

773 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