简体   繁体   English

使用VBA宏进行分组

[英]Grouping using VBA macro

I am using Excel 2010 and I would like to group rows of data using VBA. 我正在使用Excel 2010,并且想使用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. 我将在A列开始。 As you can see below I have a 10 rows. 正如您在下面看到的,我有10行。 The beginning of my first group should be "AAA" . 我的第一组的开头应该是"AAA" That group will take in all rows until "BBB" . 该组将占据所有行,直到"BBB" The next group will start at "BBB" , and take in all rows below to "CCC" . 下一组将从"BBB"开始,并在下面的所有行中进入"CCC" The 3rd row will start at "CCC" , take in all rows below and stop when it meets the blank line. 第三行将从"CCC"开始,进入下面的所有行,并在遇到空白行时停止。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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