简体   繁体   中英

Grouping using VBA macro

I am using Excel 2010 and I would like to group rows of data using VBA. I will be looping through each row to identify the start and end point of the group. Column A is where I will start. As you can see below I have a 10 rows. The beginning of my first group should be "AAA" . That group will take in all rows until "BBB" . The next group will start at "BBB" , and take in all rows below to "CCC" . The 3rd row will start at "CCC" , take in all rows below and stop when it meets the blank line. The groupings should take in any amount of rows given the required group headings. Once I have these in I want to sort the rows in each group, and then use conditional formatting.

Could you help with the groupings and where to start?

   A
 1 AAA
 2 ROW CONTENT
 3 ROW CONTENT
 4 BBB
 5 ROW CONTENT 
 6 CCC
 7 ROW CONTENT
 8 ROW CONTENT
 9 
10

This should get you started. It will define each range by looking for your section names and then sort those ranges.

' Array of section names...
Dim a As Variant
a = Array("AAA", "BBB", "CCC", "")

Dim i As Long, intStart As Long, intEnd As Long
For i = 0 To UBound(a) - 1

    intStart = Range("A:A").Find(a(i)).Row
    intEnd   = Range("A:A").Find(a(i + 1)).Row - 1

    With Sort
        .SetRange Range(Cells(intStart, 1), Cells(intEnd, 1))
        .Header = xlYes
        .Apply
    End With

Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM