2016-08-05 45 views
0

我有一個具有13138個細胞中有一個大的範圍,就像這樣:如何獲得一定的項目超出範圍的VBA

 **(A)** 
    (1) Item: 
    (2) HO 
    (3) Item: 
    (4) HO 
    (5) Item: 
    (6) HO 
    (7) Item: 
    (8) HO 
    (9) Item: 
    (10) MO 
    (11) Item: 
    (12) MFG 
    (13) MO 
    (14) Item: 
    (15) MO 
    (16) Item: 
    (17) HO 
    (18) Item: 
    (19) MFG 
    (20) MO 
    (21) Item: 
    (22) MFG 
    (23) Item: 
    (24) MFG 

我需要所有2間的細胞「項目:」細胞分組像

HO 
HO 
HO 
HO 
MO 
MFG, MO 
MO 
MO 
HO 

等等。這怎麼能實現?

回答

1

嘗試用下面的代碼


Option Explicit 
Sub test() 
    Dim lastrow As Long, i As Long 
    Dim out As String 
    lastrow = Range("A" & Rows.Count).End(xlUp).Row 
    For i = 1 To lastrow 
     Do While Range("A" & i).Value <> "Item" 
      If out = "" Then 
       out = Range("A" & i).Value 
      Else 
       out = out & ", " & Range("A" & i).Value 
      End If 
      If i < lastrow Then 
       i = i + 1 
      Else 
       Exit Do 
      End If 
     Loop 
     Range("B" & Range("B" & Rows.Count).End(xlUp).Row + 1).Value = out 
     out = "" 
    Next i 
End Sub 

結果,將在乙柱。請看下面的截圖

enter image description here