文本到列分隔符

时间:2016-09-09 01:34:36

标签: excel vba excel-vba

我尝试使用OtherChar将文字添加到列中,而不仅限于一个。目前我的代码如下所示:

With Selection
    .TextToColumns Destination:=[A2], _
        DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, _
        ConsecutiveDelimiter:=True, _
        Tab:=True, _
        Semicolon:=False, _
        Comma:=True, _
        Space:=True, _
        Other:=True, _
        OtherChar:="-", _
     TrailingMinusNumbers:=True
End With

但我希望OtherChar包含这些字符"(, ), [, ], /, *"。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

快速而脏的代码。不要使用*作为分隔符,因为它将被视为全部替换

Dim aDelim
Dim delim
Dim oRange As Range

aDelim = Split("(,),[,],/", ",")
Set oRange = Selection

For Each delim In aDelim
    oRange.Replace What:=delim, Replacement:="-"
    Debug.Print oRange.Text, delim
Next

oRange.TextToColumns Destination:=[A2], _
    DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, _
    ConsecutiveDelimiter:=True, _
    Tab:=True, _
    Semicolon:=False, _
    Comma:=True, _
    Space:=True, _
    Other:=True, _
    OtherChar:="-", _
 TrailingMinusNumbers:=True