我正在尝试编写一个宏来搜索整个工作表中的值,然后在整个工作表中存储该值的所有匹配的值和位置。我稍后会对位置和值进行一些操作,但我需要先使用它。
最初,我使用Range.Find函数进行迭代,并注意到我返回了相同的值。然后,我尝试在每次找到值时更改搜索范围。我将获取先前找到的值的地址,并使其成为范围的下限。
这在某种程度上起作用了,但最后我最终得到了一个无限循环。我的循环的结束条件是Range.Find什么也没找到(因为工作表的大小总是在改变,我不知道真正的上限是多少)。发生的事情是Range.Find会卡在最后一个值上并且拒绝从该点移动,无论我在该范围内做出什么改变。
我最近尝试解决此问题的方法是更改 After:= 输入以查看是否会强制程序继续运行。它最终破坏了我已经拥有的过程,现在我陷入了第一个值的无限循环中。所以,当然,我只是把那部分拿出来希望能再次使用它。没有运气。
这是代码: [代码]
Sub SearchLibrary()
'
' SearchLibrary Macro
' Searches MC library for inputed value and returns all related inforamtion
in Search sheet
'
' Keyboard Shortcut: Ctrl+s
'
'Search code to find all matching values and corresponding headers
' Define variables
Dim searchn As Integer ' The value input for the search
If IsNumeric(Sheets("Search").Range("C2")) Then
searchn = Sheets("Search").Range("C2").Value
End If
Dim i As Integer ' Simple counter for loops (column number)
i = 0
Dim j As Integer 'Simple counter for loops (row number)
Dim Data As Worksheet ' Define the search area as all of the sheet MC Library
Worksheets("MC library").Activate
Set Data = Sheets("MC library")
Dim loc As Range
Dim rang As Range
Dim spce As Range
Dim mass() As Single
Dim Found As Variant
Set rang = Sheets("MC library").Range("C3:Z500")
Set loc = Sheets("MC library").Range("C3")
On Error Resume Next
Do
Set Found = rang.Find(What:=searchn, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
MsgBox (Found)
Set loc = Sheets("MC library").Range(Found.Address)
If Found > 0 Then
ReDim Preserve mass(i)
mass(i) = Found
i = i + 1
Set rang = Sheets("MC library").Range(loc, "Z500")
End If
Loop Until Found Is Nothing
End Sub
[/代码]
这是一项正在进行的工作,因此有一些事情尚不相关。 Do 循环是真正的问题所在。
searchn从一个单元格中调用一个值,该单元格是用户搜索的输入,通常是一个四位数字。 MsgBox行仅用于调试,不会在最终版本中。
非常感谢任何建议和帮助。最大的问题(我认为)是找到一种方法将单元格的位置存储在变量中,然后使用它来改变范围。
答案 0 :(得分:0)
这是一个有效的查找例程
Sub findAll()
Dim aaa As Range
With ActiveSheet.Cells
Set aaa = .Find(3, LookIn:=xlValues) ' find number 3
If Not aaa Is Nothing Then
firstFind = aaa.Address
Do
Debug.Print aaa.Address, aaa.Value
Set aaa = .FindNext(aaa)
Loop While aaa.Address <> firstFind
End If
End With
End Sub
答案 1 :(得分:0)
Sub SearchLibrary()
Dim searchn
Dim shtData As Worksheet
Dim hits As Collection, hit
searchn = Sheets("Search").Range("C2").Value
If Len(searchn) = 0 Or Not IsNumeric(searchn) Then
MsgBox "Search term should be numeric!", vbExclamation
Exit Sub
End If
Set shtData = Worksheets("MC library")
Set hits = FindAll(shtData.Range("C3:Z500"), searchn)
For Each hit In hits
Debug.Print hit.Address, hit.Value
Next hit
End Sub
Public Function FindAll(rng As Range, v) As Collection
Dim rv As New Collection, f As Range
Dim addr As String
Set f = rng.Find(what:=v, after:=rng.Cells(1), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If Not f Is Nothing Then addr = f.Address()
Do Until f Is Nothing
rv.Add f
Set f = rng.FindNext(after:=f)
If f.Address() = addr Then Exit Do
Loop
Set FindAll = rv
End Function