Find方法在excel';使用VBA的工作簿?
我正在客户提供的excel表格中搜索文本/字符串。问题是excel查找函数有时工作,有时不工作。例如,如果excel工作表如下所示:Find方法在excel';使用VBA的工作簿?,excel,vba,find,excel-2007,Excel,Vba,Find,Excel 2007,我正在客户提供的excel表格中搜索文本/字符串。问题是excel查找函数有时工作,有时不工作。例如,如果excel工作表如下所示: PartID Description Reference 1234 abc R3 4567 def C34 4512 ghi R2 假设我搜索了R2,找到了它,但当我搜索R3时,它就是打不到。它会在R2之后找到任何看起来像R3的东西,像R33,R31等等。看起来它从它的最后一个位置开始检查,那
PartID Description Reference
1234 abc R3
4567 def C34
4512 ghi R2
假设我搜索了R2,找到了它,但当我搜索R3时,它就是打不到。它会在R2之后找到任何看起来像R3的东西,像R33,R31等等。看起来它从它的最后一个位置开始检查,那就是R2的位置。以下是我的职责:
Sub addFeedernoToFile(PARTS As Integer, ByRef counter As Integer, fileptrsq As String, ws_sq As Worksheet, tempList() As String)
Dim i As Integer, k As Integer, found As Integer
Dim LastAddress As String
Dim xlSearchWithin1 As XlSearchWithin
Set Search_Range = Columns("C")
For i = 1 To PARTS
searchstring = tempList(counter)
With Search_Range
Set c = ws_sq.Cells.Find(What:=searchstring, _
After:=ws_sq.Range("C3"), _
SearchOrder:=xlByColumns, _
MatchCase:=False, _
LookAt:=xlPart, _
SearchDirection:=xlNext)
On Error Resume Next
' keep track of where we are. If we are in the loop below and hit
' LastAddress this means we have looped back to the begining.
LastAddress = c.Address
'loop until we find the part
Do Until c Is Nothing
found = 1
Dim splitter() As String
splitter = Split(c.Value, ",")
For k = 0 To UBound(splitter)
If splitter(k) = searchstring Then
firstaddress = c.Address
itemRow = Mid(firstaddress, 4, Len(firstaddress) - 3)
feederno = ws_sq.Range("F" & itemRow)
counter = counter - 1
found = 0
Exit For
End If
Next
Set c = ws_sq.Cells.FindNext(After:=c)
'we loop until we find our part the file, and if found
'we break out then.
If found = 0 Then
Exit Do
End If
If LastAddress = c.Address Then
Exit Do
End If
Loop ' end do until
End With ' end with search_range
Next ' end for
End Sub
谢谢。看起来它不会马上抓取R3,因为你从C3开始,最终它会在你搜索其他所有内容后命中。记住参数“After:=”表示在该单元格之后它将开始搜索,因此您可能希望在C2处开始搜索,因此您需要将
After:=ws_sq.Range("C1")
另外,如果你想要一个精确的搜索,你可以把
LookAt:=xlWhole
使用xlPart,您将获得其他所有功能。这段代码将使您从最后一个位置开始:
Set c = ws_sq.Cells.FindNext(After:=c)
但是您所做的主搜索不会这样做。没有“VB6宏”。我想您的意思是说VBA,并将其标记为相同的?你可能会得到更多的答案。你用它来搜索什么?搜索范围似乎不会在任何地方被引用。另外,为什么要在.Find?中使用“After:=”参数?@BobRiemersma,谢谢!。Bonjoe,搜索范围设置为Column C。我正在使用Ater,因为数据从C3开始。你能在web上的某个地方发布一个示例文件,以便我们调试你的代码吗?您似乎有未使用的/重叠的变量,这使得代码更难理解,即
Search\u Range
设置为C列,但随后您在ws\u sq.Cells
@brettdj上运行了一个Find,您能解释一下它的区别吗:)它没有击中它,我将它置于调试模式并逐步完成。如果我可以强制它搜索整个工作簿而不是工作表,那可能会有所帮助。但我不知道怎么做?它应该找到它。您可以在逐步查看范围的地址,以确保它们是正确的。要搜索整个工作簿,只需记录一个新宏,然后按ctrl+f(查找)并输入设置,然后在选项下将“内”从“工作表”更改为“工作簿”。