2017-07-24 84 views
0

輸入的數據將在下面的格式 -Excel的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 | 
+--------------------------------------+--------+---------+----------+ 

我希望有一個新的列,可以基於2個標準如下告訴團體 -

  1. 如果相同的價值在desc col,那麼行組將會像第1組,第2組相同
  2. 如果訂單或文檔或發票列值存在於Desc列然後行將屬於同一組

輸出將是 -

+--------------------------------------+--------+---------+----------+----------+ 
|     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 
+0

你到目前爲止嘗試過什麼? – Zac

+0

編輯與我迄今嘗試過的問題 – baimzz

回答

0

如果我們假設說明是在C和文件,發票,訂單是H,I ,J,並且組可在U, 我改變了這樣的代碼:

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 

所以,我有像你一樣的組名,在列U.

希望這個幫助