删除字符串的外括号不起作用

时间:2017-12-07 07:41:56

标签: excel vba excel-vba

我正在尝试使用值搜索字符串,然后获取我想要的值,然后再次搜索它,直到找不到任何其他内容。

一切都运行良好,直到我删除字符串外括号的代码的一部分不执行我想要的。它没有给出错误,它什么都不做。

总结:

valGrep = "lights"

SecVal = "#define lights()(broad())"

我要做的是设置TempGrep = broad(),但是直到函数结束它仍然是(broad()),括号仍然存在。

这是我的代码。我把评论'这是它不起作用的部分 - 在线上不起作用

Sub SearchExt()

Dim FirstAddress As String
Dim MyArr As Variant
Dim Rng As Range
Dim Rcount As Long
Dim I As Long
Dim TempGrep As String

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

' Fill in the search Value
MyArr = Array(valGrep)

With Sheets("Extension").UsedRange

    Rcount = 0

    For I = LBound(MyArr) To UBound(MyArr)

        ' If you use LookIn:=xlValues it will also work with a
        ' formula cell that evaluates to tmpAnaly
        ' Note : I use xlPart in this example and not xlWhole
        Set Rng = .Find(What:=MyArr(I), _
                        After:=.Cells(.Cells.Count), _
                        LookIn:=xlFormulas, _
                        LookAt:=xlPart, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)
        If Not Rng Is Nothing Then
            FirstAddress = Rng.Address
            SecVal = Rng.Value
                ' get grep result
                TempGrep = Trim(Replace(SecVal, valGrep, " "))
                If InStr(SecVal, "#define") <> 0 Then
                    TempGrep = Trim(Replace(TempGrep, "#define", " "))
                End If
                ' Delete comment
                If InStr(1, TempGrep, "/*") <> 0 Then
                    TempGrep = Left(TempGrep, InStr(1, TempGrep, "/*") - 1)
                End If
                ' When type is included
                If InStr(1, TempGrep, "U1") <> 0 Or InStr(1, TempGrep, "U2") <> 0 Or InStr(1, TempGrep, "U4") <> 0 Or _
                InStr(1, TempGrep, "S1") <> 0 Or InStr(1, TempGrep, "S2") <> 0 Or InStr(1, TempGrep, "S4") <> 0 Then
                    searchFlg = False

                End If
                If InStr(1, TempGrep, " ") <> 0 Then
                    TempGrep = Trim(Replace(TempGrep, " ", ""))
                End If

                ' Edit the Grep result and get the value in parentheses
                TempGrep = Mid(TempGrep, 2, Len(TempGrep) - 2) 'This is the part where it does not work

                ' In the case of forced type conversion
                If InStr(1, TempGrep, "U1)") <> 0 Or InStr(1, TempGrep, "U2)") <> 0 Or InStr(1, TempGrep, "U4)") <> 0 Or InStr(1, TempGrep, "S1)") <> 0 Or _
                InStr(1, TempGrep, "S2)") <> 0 Or InStr(1, TempGrep, "S4)") <> 0 Then
                    TempGrep = Right(TempGrep, Len(TempGrep) - InStr(1, TempGrep, ")"))
                End If

            Do
                Rcount = Rcount + 1

                valGrep = TempGrep

                Set Rng = .FindNext(Rng)
            Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
        End If
    Next I
End With

With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With

End Sub

1 个答案:

答案 0 :(得分:0)

@jsotola如果发布答案就应该获得积分,因为他是解决问题的人。这篇文章只是为了扩展基础问题的解决方案。

正如jsotola的评论中指出并经OP验证,问题是隐形字符。

您的代码已使用trim函数,该函数会删除给定字符串开头和结尾的剩余空格。但是此函数不会影响字符串中间的空格或不可打印的字符。

然而,使用clean函数是删除大部分非printables的好方法(请注意,它不会删除所有这些)。要访问它,您将使用WorksheetFunction.Clean(string)

如果您有其他不可见的字符,例如clean未涵盖的unicode字符,那么至少在Excel 2013中没有本地方法可以处理这些字符。前段时间我遇到了类似的问题,并将一个非常丑陋且未经优化(但功能齐全)的解决方案整合在一起:

Function cleanSpecialCharacters(str As String)
    Dim spaceChars As String, bannedChars As String
    bannedChars = Chr(127) & "," & Chr(129) & "," & Chr(141) & "," & Chr(143) & "," & Chr(144) & "," & Chr(157) & "," & Chr(160)
    Application.ScreenUpdating = False

    spaceChars = Chr(32) & Chr(32)

    Dim tVal As String, bChar As Variant
    tVal = str

    tVal = Application.WorksheetFunction.Clean(tVal)
    tVal = Application.WorksheetFunction.Trim(tVal)

    For Each bChar In Split(bannedChars, ",")
        tVal = Replace(tVal, bChar, "")
    Next

    tVal = Replace(tVal, spaceChars, Chr(32), 1)

    cleanSpecialCharacters = tVal
    Application.ScreenUpdating = True
End Function

用法:TempGrep = cleanSpecialCharacters(TempGrep)

如果您有任何其他需求,应该很容易修改。