如何将此字符串拆分为电子表格中的单独列?

时间:2015-06-11 15:54:07

标签: excel split spreadsheet

从网上下载后,我有以下数据集。原始数据应该是Type is" preference",Properties is" Create,Filter,Group,Nillable,Sort,Update"和描述是" 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.

有在线教程教授如何使用分隔符来分割字符串,例如",空格或其他符号"在电子表格中,但我的数据集是不同的。我该如何拆分"输入","属性"和"描述"出?

1 个答案:

答案 0 :(得分: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