在Visual Basic VBA字符串中的每个引号之前插入斜杠

时间:2016-08-05 14:41:02

标签: excel vba excel-vba

正如标题所说,我想在Visual Basic VBA字符串中的每个引号之前插入斜杠。感谢。

这是我现在尝试做的事情(它甚至无法编译):

 Dim n As Integer
    n = UBound(value) - LBound(value) + 1
    Dim parsedValue As String
    parsedValue = value
    For i = 1 To n
        If parsedValue.Chars(i) = "'" Then
            n = n + 1
            parsedValue = Left(parsedValue, i) + "\'" + Right(parsedValue, n - i)
            i = i + 1
        End If
    Next

1 个答案:

答案 0 :(得分:2)

这是一个非常简单的示例,在字符串中的每个引号之前插入backslash而不循环遍历整个字符串。

Sub Sample()
    Dim sString As String

    sString = "This is a sample string has 'quotes' in it"

    Debug.Print sString
    'This is a sample string has 'quotes' in it

    sString = Replace(sString, "'", "\'")

    Debug.Print sString
    'This is a sample string has \'quotes\' in it
End Sub