查找并替换长度在2个符号之间的可变长度的字符串

时间:2020-05-10 10:40:45

标签: excel vba if-statement str-replace

我已经看了很多遍,但是找不到命令如何替换两个特殊字符之间的单词。 我想删除两个方括号之间的所有内容。

我只能删除一个特殊字符“(”,但不能删除括号之间的整个单词。

结果应该如何:

示例-之前: 26125奥尔登堡(奥尔登堡),亚历山大斯费尔德

示例-之后: 26125奥尔登堡,亚历山大斯费尔德

Option Explicit

Sub Bereinigen()

Dim Text As String
'Text = ActiveSheet.Cells(1, 1)
Text = "26125 Oldenburg (Oldenburg), Alexandersfeld"

If InStr(Text, "(") > 0 Then

    Dim strSearchFor As String
    Dim strReplaceWith As String
    Dim strNewText As String

    strSearchFor = "("
    strReplaceWith = ""

    strNewText = Replace(Text, strSearchFor, strReplaceWith)
    Debug.Print strNewText
End If
End Sub

3 个答案:

答案 0 :(得分:3)

如果您正在编写VBA过程,则可以使用LeftMidInStr的组合:

Sub sRemoveBracketData()
    Dim strData As String
    strData = "26125 Oldenburg (Oldenburg), Alexandersfeld"
    If (InStr(strData, ")") > 0) And (InStr(strData, ")") > 0) Then
        strData = Left(strData, InStr(strData, "(") - 1) & Mid(strData, InStr(strData, ")") + 1)
        Debug.Print strData
    End If
End Sub

使用工作表功能可以实现类似的结果:

=IFERROR(LEFT(A15,FIND("(",A15)-1) & MID(A15,FIND(")",A15)+1,999),A15)

此致

答案 1 :(得分:2)

您还可以使用正则表达式来做到这一点。

Option Explicit

Sub BereinigenRgEx()
Dim Text As String, outText As String
Dim RgEx As Object
'Text = ActiveSheet.Cells(1, 1)
Text = "26125 Oldenburg (Oldenburg), Alexandersfeld"
Set RgEx = CreateObject("VBScript.RegExp")
With RgEx
    .Global = True
    .Pattern = "\(.+\)"
    outText = .Replace(Text, "")
    Debug.Print outText
End With
End Sub

答案 2 :(得分:1)

UDF cleanText(Bereinigen)

代码

Option Explicit

' In a string, removes the string from a first (left) and a second (right)
' specified string (the two specified strings inclusive).
Function cleanText(Text As String, LeftString As String, RightString As String, _
    Optional trimLeft As Boolean = False, _
    Optional trimRight As Boolean) As String

    cleanText = Text

    Dim foundLeft As Long, foundRight As Long
    Dim LeftStr As String, RightStr As String

    foundLeft = InStr(1, Text, LeftString)
    If foundLeft > 0 And foundLeft <= Len(Text) - Len(LeftString) Then
        foundRight = InStr(foundLeft + Len(LeftString), Text, RightString)
        If foundRight > 0 Then
            LeftStr = Left(Text, foundLeft - 1)
            RightStr = Right(Text, Len(Text) - foundRight - Len(RightString) + 1)
            If trimLeft Then LeftStr = Trim(LeftStr)
            If trimRight Then RightStr = Trim(RightStr)
            cleanText = LeftStr & RightStr
        End If
    End If

End Function

Sub cleanTextExample()

    Const Text = "26125 Oldenburg ($(Oldenburg)$) , Alexandersfeld"
    Dim Result As String

    Result = Text: Debug.Print Result
    Result = cleanText(Text, "($(", ")$)"): Debug.Print Result
    Result = cleanText(Text, "($(", ")$)", True): Debug.Print Result
    Result = cleanText(Text, "($(", ")$)", True, True): Debug.Print Result

' Results:
' 26125 Oldenburg ($(Oldenburg)$) , Alexandersfeld
' 26125 Oldenburg  , Alexandersfeld
' 26125 Oldenburg , Alexandersfeld
' 26125 Oldenburg, Alexandersfeld

End Sub

Sub cleanTextExampleSimple() ' Your Case.

    Const Text = "26125 Oldenburg (Oldenburg), Alexandersfeld"
    Dim Result As String

    Result = Text: Debug.Print Result
    Result = cleanText(Text, "(", ")", True): Debug.Print Result

' Results:
' 26125 Oldenburg (Oldenburg), Alexandersfeld
' 26125 Oldenburg, Alexandersfeld

End Sub
相关问题