Excel VBA基于多个标准创建行组

时间:2017-07-24 13:18:36

标签: excel excel-vba vba

输入数据将采用以下格式 -

+--------------------------------------+--------+---------+----------+
|                 Desc                 |  Doc   | Invoice |  Order   |
+--------------------------------------+--------+---------+----------+
| XXXXXXX;SFFFFFFFF;QQWEWEE;878dDDss11 |  98988 |   27891 | XXX32222 |
| XXXXXXX;SFFFFFFFF;QQWEWEE;878dDDss11 |  45444 |  422445 | #        |
| XXX32222                             |   4333 |   23444 | #        |
| #                                    |   4454 |    3345 | XXX32333 |
| #                                    |  32333 |   44222 | ddh33333 |
| XXX32333                             | 323222 |   45333 | #        |
| XXX32333                             | 322433 |   34333 | #        |
| 4454                                 |   3333 |  232222 | #        |
| XXXSDDD-ddh33333                     |  34433 |   45555 | XX344444 |
+--------------------------------------+--------+---------+----------+

我想要一个新列,它可以根据以下两个标准告诉小组 -

  1. 如果desc col中的值相同,则行组将与组1,组2等相同
  2. 如果Desc列中有订单或doc或invoice列值,则row将属于同一组
  3. 输出将是 -

    +--------------------------------------+--------+---------+----------+----------+
    |                 Desc                 |  Doc   | Invoice |  Order   | Grouping |
    +--------------------------------------+--------+---------+----------+----------+
    | XXXXXXX;SFFFFFFFF;QQWEWEE;878dDDss11 |  98988 |   27891 | XXX32222 | Group 1  |
    | XXXXXXX;SFFFFFFFF;QQWEWEE;878dDDss11 |  45444 |  422445 | #        | Group 1  |
    | XXX32222                             |   4333 |   23444 | #        | Group 1  |
    | #                                    |   4454 |    3345 | XXX32333 | Group 2  |
    | #                                    |  32333 |   44222 | ddh33333 | Group 3  |
    | XXX32333                             | 323222 |   45333 | #        | Group 2  |
    | XXX32333                             | 322433 |   34333 | #        | Group 2  |
    | 4454                                 |   3333 |  232222 | #        | Group 2  |
    | XXXSDDD-ddh33333                     |  34433 |   45555 | XX344444 | Group 3  |
    +--------------------------------------+--------+---------+----------+----------+
    

    到目前为止,我已尝试编写以下代码

    i = 0
    For Each cell1 In Range("C5:C" & Cells(Rows.Count, "A").End(xlUp).Row).Cells
    Debug.Print cell1.Row
    jvno = cell1.Offset(0, 5).Value
    invno = cell1.Offset(0, 6).Value
    ordno = cell1.Offset(0, 7).Value
            For Each cell3 In Range("C5:C" & Cells(Rows.Count, "A").End(xlUp).Row).Cells
            Debug.Print cell3.Row
                If ((cell1.Value = cell3.Value) And cell1.Value <> "#") Or InStr(cell3.Value, jvno) > 0 Or InStr(cell3.Value, invno) > 0 Or (InStr(cell3.Value, ordno) > 0 And (Not IsEmpty(ordno)) And ordno <> "#") Then
                    cell1.Offset(0, 18).Value = "Group " & i
                    cell3.Offset(0, 18).Value = "Group " & i
                End If
            Next cell3
            i = i + 1
    Next cell1
    

1 个答案:

答案 0 :(得分:0)

If we suppose that Desc is in C and Doc, Invoice, Order are in H, I, J, and the groups are in U, I changed your code like this :

i = 1
Set cell1 = Range("C5")
repeat:
cell1.Offset(0, 18).Value = "Group " & i
    jvno = cell1.Offset(0, 5).Value
    invno = cell1.Offset(0, 6).Value
    ordno = cell1.Offset(0, 7).Value

    For Each cell3 In Range("C6:C" & Cells(Rows.Count, "A").End(xlUp).Row).Cells
        If ((cell1.Value = cell3.Value) And cell1.Value <> "#") Or InStr(cell3.Value, jvno) > 0 Or InStr(cell3.Value, invno) > 0 Or (InStr(cell3.Value, ordno) > 0 And (Not IsEmpty(ordno)) And ordno <> "#") Then
            If cell3.Offset(0, 18).Value = "" Then
                cell3.Offset(0, 18).Value = "Group " & i
            End If
        End If
    Next cell3
i = i + 1
Set cell4 = cell1.Offset(0, 18)
While cell4.Value <> ""
    Set cell4 = cell4.Offset(1, 0)
Wend
If cell4.Row <= Cells(Rows.Count, "A").End(xlUp).Row Then
    Set cell1 = Range("C" & cell4.Row)
    GoTo repeat
End If

So, I have the same group names like you, ine the column U.

Hope this help