为什么这个 VBA 查找函数找不到搜索字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9230925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is this VBA find function not finding the search string?
提问by Brian
I am using VBA in excel, and I am trying to find the text "Summary", without the quotes, in a cell. The cell is in the first colum, and I want find which row it is in. I know it is there, I see it when I look at the workbook. However, my condition 'If Not found Is Nothing Then' does not execute, implying the value was not found. What is wrong with my find, how can I fix this. Thanks.
我在 excel 中使用 VBA,我试图在单元格中找到没有引号的文本“摘要”。单元格在第一列中,我想找到它在哪一行。我知道它在那里,我在查看工作簿时看到了它。但是,我的条件 'If Not found Is Nothing Then' 不执行,这意味着未找到该值。我的发现有什么问题,我该如何解决这个问题。谢谢。
Sub RemoveFooterRows(theFile)
Dim found As Range
Set found = Workbooks(theFile).Worksheets(1).Columns(1).Find _
("Summary", , xlValues, xlPart, xlByRows, xlNext, False, , False)
If Not found Is Nothing Then
MsgBox ("val is inif " & found.Row)
' Workbooks(theFile).Close
End If
End Sub
回答by Alpha
Public Sub RemoveFooterRows(theFile)
Dim res As Variant, ws As Worksheet
On Error Resume Next
For Each ws In Workbooks(theFile).Worksheets
res = Application.WorksheetFunction.Match("Summary", ws.Range("A:A"), 0)
If IsEmpty(res) = False Then
MsgBox "Val is inif " & res
GoTo Skip
End If
Next ws
On Error GoTo 0
End Sub