View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Andy Andy is offline
external usenet poster
 
Posts: 11
Default group rows in a range based on criteria from another range (vba)

Made some modifications to the code and I'm closer, but need some help on getting
the right description cell to sheet3. Below is the new code and resulting
output:

Sub GettingCloser()
Sheet2.Range("A2:B10").Name = "MyCodesArray"
Sheet2.Range("E2:E6").Name = "MyGroupsArray"
For Each groupcell In Range("MyGroupsArray")
Sheets("Sheet3").Range("A1").Offset(iCol, 0) = "Group " & groupcell & "
items"
iCol = iCol + 1
For Each codecell In Range("MyCodesArray")
desccell = Range("MyCodesArray").Cells(1, 2)
If Left(codecell, 3) = groupcell Then
Sheets("Sheet3").Range("A1").Offset(iCol, 0) = codecell
Sheets("Sheet3").Range("A1").Offset(iCol, 1) = desccell
iCol = iCol + 1
End If
Next codecell
iCol = iCol + 1
Next groupcell
End Sub

Group 010 items
01050 item 01050

Group 013 items
01315 item 01050

Group 025 items
02530 item 01050
02550 item 01050

Group 033 items
03360 item 01050
03370 item 01050
03390 item 01050

Group 042 items
04200 item 01050
04220 item 01050


-- Sample Data --
Sheet2.Range("A2:A11").Name = "MyCodesArray"
Code Description
01050 item 01050
01315 item 01315
02530 item 02530
02550 item 02550
03360 item 03360
03370 item 03370
03390 item 03390
04200 item 04200
04220 item 04220
04260 item 04260

Sheet2.Range("E2:E6").Name = "MyGroupsArray"
Group
010
013
025
033
042