VB代码删除列中的特殊字符

时间:2014-07-15 09:32:41

标签: vba excel-vba excel

我有一个包含两个特殊字符“,”和“_”的整数值的列。我试图删除这些字符,例如1,10_2,2_3,3应该像1102233.提前感谢您的建议。

1 个答案:

答案 0 :(得分:1)

这个功能并非万无一失,但它是一个好的开始。

Function trim(aStringToTrim As String, aElementToTrinm() As Variant) As String
Dim elementToTrim As Integer
Dim IndexInString As Integer

For elementToTrim = LBound(aElementToTrinm) To UBound(aElementToTrinm)
    IndexInString = InStr(aStringToTrim, aElementToTrinm(elementToTrim))

    Do While IndexInString > 0
        aStringToTrim = Left(aStringToTrim, IndexInString - 1) & Right(aStringToTrim, Len(aStringToTrim) - IndexInString - Len(aElementToTrinm(elementToTrim)) + 1)

        IndexInString = InStr(aStringToTrim, aElementToTrinm(elementToTrim))
    Loop
Next

End Function

可以这样使用:

Sub main()
    Dim myString As String
    Dim caracterstoRemove As Variant

    caracterstoRemove = Array(",", ".")

    myString = "This, is. a, string, with. caracters to remove."

    myString = trim(myString, caracterstoRemove)

End Sub