【发布时间】:2017-08-07 13:41:30
【问题描述】:
我有一个包含如下字符串的集合:
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
关于如何解决这个问题的任何想法我知道我将遍历集合,但我将如何在第一个空格之后分成两个单独的集合?
【问题讨论】:
-
Left(ws1.Cells(i, 1).Value, Instr(ws1.Cells(i, 1).Value, " ") - 1)和Mid(ws1.Cells(i, 1).Value, Instr(ws1.Cells(i, 1).Value, " ") + 1) -
@TimWilliams 将
1传递给Split返回一个数组,其中整个字符串作为单个元素。你不是说Split(stringHere, " " , 2)吗? -
@ZevSpitz - 是的,谢谢。我将“最大元素数”与“最大分割数”混淆了