Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按顺序查找引用--VBA_Vba_Excel_Search - Fatal编程技术网

按顺序查找引用--VBA

按顺序查找引用--VBA,vba,excel,search,Vba,Excel,Search,我正在使用我从本网站获得的代码。一切正常,但出于某种原因,它从第二个引用开始循环到文件末尾,然后获得第一个引用 例如: --样本数据: Origin X Y S 45 65 W 78 7 S 45 5 D 6 3 B 75 68 S 19 87 T 23 98 S 33 94 Q 21 105 S 17 117 T 12 128 当我试图在字母“S”的“原点”列中查找所有匹配项时,我通过Debug.Print(rng.a

我正在使用我从本网站获得的代码。一切正常,但出于某种原因,它从第二个引用开始循环到文件末尾,然后获得第一个引用

例如:

--样本数据:

Origin  X   Y
S   45  65
W   78  7
S   45  5
D   6   3
B   75  68
S   19  87
T   23  98
S   33  94
Q   21  105
S   17  117
T   12  128
当我试图在字母“S”的“原点”列中查找所有匹配项时,我通过
Debug.Print(rng.address)
检索地址,它将提供
$A$4、$A$7、$A$9、$A$11、$A$2

为什么$A$2最后才显示?这在我所有不同的excel文件中都发生过

代码如下:

Sub FindAll()

'PURPOSE: Find all cells containing a specified values
'SOURCE: www.TheSpreadsheetGuru.com

Dim fnd As String, FirstFound As String
Dim FoundCell As Range, rng As Range
Dim myRange As Range, LastCell As Range

'What value do you want to find (must be in string form)?
  fnd = "S"

Set myRange = ActiveSheet.UsedRange
Set LastCell = myRange.Cells(myRange.Cells.Count)
Set FoundCell = myRange.Find(what:=fnd, after:=LastCell)

'Test to see if anything was found
  If Not FoundCell Is Nothing Then
    FirstFound = FoundCell.Address
  Else
    GoTo NothingFound
  End If

Set rng = FoundCell

'Loop until cycled through all unique finds
  Do Until FoundCell Is Nothing
    'Find next cell with fnd value
      Set FoundCell = myRange.FindNext(after:=FoundCell)

    'Add found cell to rng range variable
      Set rng = Union(rng, FoundCell)

    'Test to see if cycled through to first found cell
      If FoundCell.Address = FirstFound Then Exit Do

  Loop

'Select Cells Containing Find Value
  rng.Select

  Debug.Print (rng.Address)

Exit Sub

'Error Handler
NothingFound:
  MsgBox "No values were found in this worksheet"

End Sub

将中间的环更改为:

'What value do you want to find (must be in string form)?
fnd = "S"

Set myRange = ActiveSheet.UsedRange

With myRange
    Set FoundCell = .Find(fnd, LookIn:=xlValues)
    If Not FoundCell Is Nothing Then
        firstAddress = FoundCell.Address

        Do
            'Add found cell to rng range variable
            If rng Is Nothing Then
                Set rng = FoundCell '<-- add first range found
            Else
                Set rng = Union(rng, FoundCell) '<-- add ranges by using Union
            End If

            Set FoundCell = .FindNext(FoundCell)
            If FoundCell Is Nothing Then
                GoTo DoneFinding
            End If
            Loop While Not FoundCell Is Nothing And FoundCell.Address <> firstAddress
    End If
DoneFinding:
End With

Debug.Print (rng.Address)
“您要查找什么值(必须是字符串形式)?
fnd=“S”
设置myRange=ActiveSheet.UsedRange
用myRange
Set FoundCell=.Find(fnd,LookIn:=xlValues)
如果不是FoundCell,那就什么都不是了
firstAddress=FoundCell.Address
做
'将找到的单元格添加到rng范围变量
如果rng不算什么,那么

设置rng=FoundCell'您的循环实际上会找到A2作为第一个单元格,但随后它会再次找到它,因为在
Find()
返回到第一个找到的单元格后,您将再次循环一次

因此,
Set rng=Union(rng,FoundCell)
再次将A2添加到
rng
中,作为最后找到的单元格,这就是为什么您会看到它列在底部的原因

您必须将检查作为循环的结束条件移动,并且在换行后不允许运行
Set rng=Union(rng,FoundCell)

具体如下:

Option Explicit

Sub FindAll()
    'PURPOSE: Find all cells containing a specified values
    'SOURCE: www.TheSpreadsheetGuru.com

    Dim fnd As String, FirstFound As String
    Dim FoundCell As Range, rng As Range

    'What value do you want to find (must be in string form)?
    fnd = "S"

    With ActiveSheet.UsedRange '<--| reference the range to search into
        Set FoundCell = .Find(what:=fnd, after:=.Cells(.Cells.Count)) '<--| find the first cell

        If Not FoundCell Is Nothing Then 'Test to see if anything was found
            FirstFound = FoundCell.Address ' <--| store the first found cell address
            Set rng = FoundCell '<--| initialize the range collecting found cells. this to prevent first 'Union()' statement from failing due to 'rng' being 'Nothing'
            Do
                Set rng = Union(rng, FoundCell)  'Add found cell to rng range variable

                'Find next cell with fnd value
                Set FoundCell = .FindNext(after:=FoundCell)
            Loop While FoundCell.Address <> FirstFound 'Loop until cycled through all finds

            rng.Select 'Select Cells Containing Find Value
            Debug.Print (rng.Address)
        Else
            MsgBox "No values were found in this worksheet"
        End If
    End With
End Sub
选项显式
副财务长()
'目的:查找包含指定值的所有单元格
来源:www.TheSpreadsheetGuru.com
Dim fnd作为字符串,FirstFound作为字符串
Dim FoundCell作为范围,rng作为范围
'要查找什么值(必须是字符串形式)?
fnd=“S”

使用ActiveSheet.UsedRange'
单元格(myRange.Cells.Count)
可能无法获取最后一个单元格,如果该区域与信息不相交。我的实际数据是不相交的,但这个示例还没有,它仍然提供了第一个,最后一个,一致性。我会在我上了一台真正的计算机后尝试你的代码…欢迎你,你的帖子问它:“为什么$a$2最后显示?”