【问题标题】:How To Find A Specific Cell With The Relevent Data?如何找到具有相关数据的特定单元格?
【发布时间】:2019-07-30 05:54:36
【问题描述】:

我正在尝试找到一些将在整个工作表中运行并选择所有相关单元格的宏。

我编写了一些宏,可以找到单元格但只找到一个单元格-它没有选择所有单元格。

Dim myRange As Range
Dim myCell As Range
Set myRange = Range("A1:GG1000")
Dim mynumer As Integer
mynumber = 7
For Each myCell In myRange
If myCell = mynumber Then

myCell.Select


End If
Next myCell

如何运行宏并查看所有相关单元格? 谢谢!

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    也许尝试一些.FindNext 迭代。

    刚刚改编自上面的链接:

    Sub Test()
    
    Dim cl As Range, rng As Range
    With ThisWorkbook.Sheets("Sheet1").Range("A1:GG1000")
        Set cl = .Find(7, LookIn:=xlValues, lookat:=xlWhole)
         If Not cl Is Nothing Then
            firstAddress = cl.Address
            Do
                If Not rng Is Nothing Then
                    Set rng = Union(rng, cl)
                Else
                    Set rng = cl
                End If
                Debug.Print rng.Address
                Set cl = .FindNext(cl)
            If cl Is Nothing Then
                GoTo DF
            End If
            Loop While cl.Address <> firstAddress
          End If
    DF:
        rng.Select
    End With
    
    End Sub
    

    问题真的是,你为什么要.Select 一个范围?大多数情况下是可以避免的,而且很可能上面的代码可以修改为更简洁的东西!

    【讨论】:

      【解决方案2】:

      请看一下这个答案: How to find a value in an excel column by vba code Cells.Find

      最高票数下方的答案向您展示了如何在整个电子表格中进行搜索。

      最好的问候,

      提莫

      【讨论】:

      • 您应该引用答案并提供链接。因为链接可能随时停止工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 2017-11-04
      相关资源
      最近更新 更多