删除string的外部括号不起作用

我正在尝试search一个带有值的string,然后获取我想要的值,然后再次search,直到找不到其他任何东西。

所有运行良好,直到我的代码的一部分,我删除我的string的外部括号不执行我想要的。 它不会给出错误,它什么也不做。

总结:

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 

如果他回答问题,他应该得到信任,因为他是解决问题的人。 这篇文章只是为了解决底层问题。

正如jsotola在评论中指出的那样,经过OP的validation,问题是无形的字符。

你的代码已经使用了trim函数,它可以删除给定string开始和结尾的多余空格。 但是这个函数不会影响string或非打印字符中间的空格。

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

如果您还有其他不可见的字符,例如unicode字符未被clean ,则至less在Excel 2013中没有本机方式来处理这些字符。 前一段时间,我有一个类似的问题,一起攻击一个非常丑陋的,未优化(但function齐全)的解决scheme:

 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)

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