【问题标题】:Separate string by contents into separate collections (VBA)按内容将字符串分隔到单独的集合中(VBA)
【发布时间】: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 - 是的,谢谢。我将“最大元素数”与“最大分割数”混淆了

标签: vba excel


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 2019-03-21
    • 2018-02-02
    • 1970-01-01
    • 2012-11-26
    相关资源
    最近更新 更多