2017-08-07 60 views
0

我有出現了像集合:獨立字符串的內容轉換成單獨的集合(VBA)

130each 09/01/2017 09/01/2017 
12each 09/01/2017 09/01/2017 
1each 09/01/2017 09/01/2017 

我得到這些值與經歷我的Excel工作表下面的代碼:

Dim col as New Collection 
For i = 1 To lastRow 
    If InStr(ws1.Cells(i, 1), "each") 
     col.Add ws1.Cells(i, 1) 
    End If 
Next i 

現在我想在第一個空格之後分開字符串,並將每個部分存儲在兩個單獨的集合中。例如第二收集和第三收集應包含以下內容:對如何處理這個我知道,我將通過收集來循環

130each [at index 0 of col2] 
09/01/2017 09/01/2017 [at index 0 of col3] 
12each [at index 1 of col2] 
10/11/2017 10/11/2017 [at index 1 of col3] 
...so on 

任何想法,但我怎麼會在第一空間後獨立成兩個單獨的集合?

+0

'左(ws1.Cells(I,1)。價值,INSTR(ws1.Cells(I,1)。價值,「「) - 1)'和' Mid(ws1.Cells(i,1).Value,Instr(ws1.Cells(i,1).Value,「」)+ 1)' – YowE3K

+1

@TimWilliams將'1'傳遞給'Split'返回一個包含整個字符串作爲單個元素。你不是指'Split(stringHere,「」,2)'? –

+0

@ZevSpitz - 是的,謝謝。我把「最大元素數」與「最大分割數」 –

回答

1

像這樣:

Dim col2 As New Collection 
Dim col3 As New Collection 
Dim x As Variant 

For Each x In col 
    Dim parts As Variant 
    parts = Split(x, " ", 2) 
    col2.Add parts(0) 
    col3.Add parts(1) 
Next