在带下划线的单词vba上拆分单元格

时间:2016-08-26 18:29:47

标签: arrays excel vba excel-vba

有没有办法在vba中使用split函数根据带下划线的单词拆分单元格?如何将分隔符设置为下划线?

d = Trim(cell.Value2)
arr = Split(d, " ")

1 个答案:

答案 0 :(得分:2)

这是一种方法。那里有很多错误处理范围,但是这会让你朝着正确的方向前进。

Test eA1下划线的任何其他字符进行测试。

Public Function getArray(rng As Range)

    Dim arr()

    Dim lCtr        As Long
    Dim strText     As String
    Dim strDelim    As String

    '/ Create a delim which is qunique, so you dont miss any data.
    strDelim = "!!_<{>}##"

    For lCtr = 1 To rng.Characters.Count

        If rng.Characters(lCtr, 1).Font.Underline = XlUnderlineStyle.xlUnderlineStyleSingle Then
            '/ Splits exluding the underlined char
            strText = strText & strDelim

             '/ Splits including  the underlined char
            'strText = strText & rng.Characters(lCtr, 1).Text & strDelim

        Else
             strText = strText & rng.Characters(lCtr, 1).Text
        End If

    Next

    getArray = Split(strText, strDelim)



End Function


Sub test()
    MsgBox getArray(Cells(1, 1))(0)
End Sub