2016-08-03 188 views
-1

我有一個大的數據轉儲,需要通過下面的信息片段進行排序。循環遍歷範圍,行和列

Part # A B C Op 
    2403253 1 7 4 Foundry 
    2403253 2 8 5 Foundry 
    2403253 3 9 6 Foundry 
    2403253 4 1 7 Foundry 
    2403253 5 2 8 Outside 
    2403253 6 3 9 Machining 
    2403253 7 4 1 Machining 
    2403253 8 5 2 Polishing 
    2403253 9 6 3 Polishing 
    2403254 1 7 4 Foundry 
    2403254 2 8 5 Foundry 
    2403254 3 9 6 Machining 
    2403254 4 1 7 Polishing 
    2403256 5 2 8 Foundry 
    2403256 6 3 9 Foundry 
    2403256 7 4 1 Machining 
    2403256 8 5 2 Polishing 
    2403257 9 6 3 Foundry 
    2403257 1 7 4 Foundry 
    2403257 2 8 5 Machining 
    2403257 3 9 6 Polishing 
    2403258 4 1 7 Foundry 
    2403258 5 2 8 Foundry 
    2403258 6 3 9 Polishing 

我所希望做的是循環遍歷每個「部件#秒」,並與每一個「行動」的配對將A,B,& C一起得到這樣一個最終結果:

Part # A B C Op 
    2403253 X X X Foundry 
      X X X Machining 
      X X X Outside 
      X X X Polishing 
    2403254 X X X Foundry 
      X X X Machining 
      X X X Outside 
      X X X Polishing 
    2403256 X X X Foundry 
      X X X Machining 
      X X X Outside 
      X X X Polishing 
    2403257 X X X Foundry 
      X X X Machining 
      X X X Outside 
      X X X Polishing 
    2403258 X X X Foundry 
      X X X Machining 
      X X X Outside 
      X X X Polishing 

我不知道該如何去做。任何幫助表示讚賞。我嘗試瞭解循環,合併和其他一些。

+0

是否有固定數量的「操作」,一個如果所有這些Ops都顯示在最終結果中,即使A,B和C的值爲0,那麼它是否會關係? (也就是說結果是否爲每個零件編號生成了固定的行數,或者如果零件編號僅有源操作記錄爲3個操作步驟,您是否只需要顯示3行?) – YowE3K

+0

每個零件都需要4個操作單元中的每一個即使它是零。也可以更容易地使用4個相同的零件號,以後可以在Excel中使用Filter。 –

+0

並且當前數據是否可以在零件編號順序中排序或可以排序?如果是這樣,這個過程就簡單多了。 – YowE3K

回答

0

我很無聊而吃我的早餐,所以這裏是一些(未經測試)代碼:

Option Explicit 

Private rowDst As Long 
Private Results() As Integer 
Private currentPartNo As String 
Private Ops(4) As String 
Private wsDst As Worksheet 
Private op As Integer 
Private abc As Integer 

Sub RunMe() 
    'Switch off ScreenUpdating to speed up execution time 
    Application.ScreenUpdating = False 

    Dim wsSrc As Worksheet 
    Dim rowSrc As Long 
    Ops(1) = "Foundry" 
    Ops(2) = "Machining" 
    Ops(3) = "Outside" 
    Ops(4) = "Polishing" 

    Set wsSrc = Worksheets("Sheet1") 
    Set wsDst = Worksheets("Sheet2") 

    currentPartNo = "" 

    rowSrc = 2 
    rowDst = 2 
    Do While wsSrc.Cells(rowSrc, 1).Value <> "" 
     If wsSrc.Cells(rowSrc, 1).Value <> currentPartNo Then 
      'Different part #, so need to write out results for previous part # 
      WriteResults 
      'Keep track of current part # so we know when to write out results 
      currentPartNo = wsSrc.Cells(rowSrc, 1).Value 
      'Clear out current values from Results array 
      ReDim Results(3, 4) As Integer 
     End If 
     'Determine Op position 
     For op = 1 To 4 
      If wsSrc.Cells(rowSrc, 5).Value = Ops(op) Then 
       Exit For 
      End If 
     Next 
     'Loop through A, B and C, adding values into Results array 
     For abc = 1 To 3 
      Results(abc, op) = Results(abc, op) + wsSrc.Cells(rowSrc, abc + 1).Value 
     Next 
     'Increment row pointer 
     rowSrc = rowSrc + 1 
    Loop 
    'Write results for final part # 
    WriteResults 

    'Switch ScreenUpdating back on 
    Application.ScreenUpdating = True 

End Sub 

Private Sub WriteResults() 
    If currentPartNo <> "" Then 
     wsDst.Cells(rowDst + 0, 1).Value = currentPartNo 
     For op = 1 To 4 
      wsDst.Cells(rowDst + op - 1, 5).Value = Ops(op) 
      For abc = 1 To 3 
       wsDst.Cells(rowDst + op - 1, abc + 1).Value = Results(abc, op) 
      Next 
     Next 
     rowDst = rowDst + 4 
     currentPartNo = "" 
    End If 
End Sub 

(我真的不應該提供的代碼爲你 - 的StackOverflow是來幫助你解決問題,你有代碼,但正如我所說的,我很無聊。)

0

按下面的代碼的結果將投入G,H,I,J,K列

Dim records As Integer, count As Integer, x As String 
records = 24 
count = 1 
Dim ops(4) As Variant 
ops(1) = "Foundry" 
ops(2) = "Machining" 
ops(3) = "Outside" 
ops(4) = "Polishing" 

For i = 2 To records + 1 
    If x <> Cells(i, 1).Value Then 
     x = Cells(i, 1).Value 
     For op = 1 To 4 
      For j = 2 To records + 1 
       If Cells(j, 1).Value = x And Cells(j, 5).Value = ops(op) Then 
        a = a + Cells(j, 2).Value 
        b = b + Cells(j, 3).Value 
        c = c + Cells(j, 4).Value 
       End If 
      Next j 

      If op = 1 Then 
       Cells(count, 7).Value = x 
      End If 
      Cells(count, 8).Value = a 
      Cells(count, 9).Value = b 
      Cells(count, 10).Value = c 
      Cells(count, 11).Value = ops(op) 

      count = count + 1 
      a = 0 
      b = 0 
      c = 0 
     Next op 

    End If 
Next i 
0

如果您不需要上面顯示的精確格式,則數據透視表可以以略微不同的佈局爲您提供類似信息。

使用您的數據,將Part #Op拖到行區域;拖動ABC把價值觀面積之和

enter image description here

結果:

enter image description here

或者,如果你選擇大綱形式:

enter image description here