【问题标题】:How to split this string into separate column in spreadsheet?如何将此字符串拆分为电子表格中的单独列?
【发布时间】:2015-06-11 15:54:06
【问题描述】:

我从网上下载了以下一组数据。原始数据应该是Type是“preference”,Properties是“Create、Filter、Group、Nillable、Sort、Update”,Description是“ID”。

TypereferencePropertiesCreate, Filter, Group, Nillable, Sort, UpdateDescriptionID
TypereferencePropertiesCreate, Filter, Group, Nillable, Sort, UpdateDescriptionID of the account associated with this opportunity.
TypecurrencyPropertiesCreate, Filter, Nillable, Sort, UpdateDescriptionEstimated total sale amount.

有在线教程教如何在电子表格中使用分隔符(例如“、空格或其他符号”)来拆分字符串,但我的数据集不同。如何拆分“类型”、“属性”和“描述”?

【问题讨论】:

  • 我认为我们将不得不看一些关于这个的示例数据,因为这里没有明确的模式。
  • 它看起来像 Type 等是一个 atom 与自定义分隔符,如零宽度空格(Unicode 8603 或 200b)。首先要做的是检查您是否确实保留了不可见的分隔符,或者代码页是否丢弃了它。你能检查一下LEN(A1),看看它是否正确地反映了可见字符,或者它返回的长度是否太长?
  • JNevill,这是样本的来源。图案都是这样的。见更新。模式是类型、属性、描述。

标签: excel split spreadsheet


【解决方案1】:

如果没有分隔符字符串,(@Jeeped 提到零宽度空格),那么您可以使用普通字符串函数,如 MidInstr 来解析它。我把输出放在一个新的工作表上:

Sub foo()
    Dim findType As Integer
    Dim findProperties As Integer
    Dim findDescription As Integer
    Dim rng As Range
    Dim r As Range
    Dim i As Integer
    Dim newSheet

    Set rng = Range("A1:A3")

    'Add a new sheet and put some header rows on it
    Set newSheet = Sheets.Add
        newSheet.Range("A1").Value = "Type"
        newSheet.Range("B1").Value = "Properties"
        newSheet.Range("C1").Value = "Description"
        i = 1
    For Each r In rng.Cells
        findType = InStr(1, r.Value, "Type")
        findProperties = InStr(1, r.Value, "Properties")
        findDescription = InStr(1, r.Value, "Description")

        '## Print some output values to a new worksheet

        With newSheet
            i = i + 1
            .Range("A" & i).Value = Mid(r.Value, findType + 4, findProperties - (findType + 4))
            .Range("B" & i).Value = Mid(r.Value, findProperties + 10, findDescription - (findProperties + 10))
            .Range("C" & i).Value = Mid(r.Value, findDescription + 11)
        End With
    Next

End Sub

这是将其放在同一个工作表上的替代方法(未经测试):

Sub foo2()
    Dim findType As Integer
    Dim findProperties As Integer
    Dim findDescription As Integer
    Dim typeStr as String, propStr as String, descStr as String

    Dim rng As Range
    Dim r As Range
    Dim i As Integer

    Set rng = Range("A1:A3")

    i = 1
    For Each r In rng.Cells
        findType = InStr(1, r.Value, "Type")
        findProperties = InStr(1, r.Value, "Properties")
        findDescription = InStr(1, r.Value, "Description")

        i = i + 1
        typeStr = Mid(r.Value, findType + 4, findProperties - (findType + 4))
        propStr = Mid(r.Value, findProperties + 10, findDescription - (findProperties + 10))
        descStr = Mid(r.Value, findDescription + 11)
        rng.Value = typStr
        rng.Offset(0,1).Value = propStr
        rng.Offset(0,2).Value = descStr
    Next
    Range("A1").EntireRow.Insert
    Range("A1").Value = "Type"
    Range("B1").Value = "Properties"
    Range("C1").Value = "Description"
End Sub

【讨论】:

  • 这个脚本运行良好。我还有一个问题。除了创建新工作表之外,是否可以将它们拆分到同一张工作表中示例数据的下一列?
  • 是的。而不是使用 newsheet,而是使用 activesheet。这可能会覆盖该工作表中的数据,因此您可能仍需要对其进行一些修改
  • 你能详细说明一下吗?我没有 vba 的背景。
  • 我真的不知道如何让它更清楚,代码中有一行说用 newsheet 改成用 activesheet 说
  • 另外,我现在不在电脑旁,所以可能要过几个小时才能真正帮到她,所以你应该试着修补一下,好吧
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-01
  • 1970-01-01
  • 2021-06-08
  • 2018-10-24
相关资源
最近更新 更多