【问题标题】:Excel Macro That Programmatically finds a matching cell以编程方式查找匹配单元格的 Excel 宏
【发布时间】:2015-09-12 09:29:05
【问题描述】:

在单元格 R12 中,我有一个整数,每次我需要运行此宏时都会更改。我需要宏来分析该单元格中的数字,并在电子表格中找到与单元格 R12 的内容匹配的另一个单元格(保证在 M31:M40 范围内,因为这是匹配的位置) ,然后使匹配的单元格成为活动单元格。
显然,我可以告诉 excel R12 中的内容是什么,然后进行搜索,但我需要它自己确定 R12 中的内容......然后找到匹配的单元格,然后去那里。

没有人输入 R12 的值。它是计算出来的。 R12 是通过合计该列中其他十个单元格的内容创建的数字。我试图为我的宏留下“面包屑”以找到回到电子表格上某个位置的方式,宏可以继续在那里粘贴数据......我希望 Excel 可以确定 R12 中的数字,并找到表格上其他地方的确切数字..本身。相同的数字将存在于另一个数组中......(M31:M40)如果它可以以某种方式将活动单元格移动到匹配的数字,我可以让我的活动单元格回到我开始宏的位置。宏的完整内容未发布。希望澄清。

【问题讨论】:

  • R12 有什么变化?如果有人输入一个值,那么VBA 可以做到这一点。
  • 你能解释一下这个更大的目的吗?也许您的整体方法也可以使用一些帮助。
  • 您可以将 Activecell 地址存储在例程的开头,以便稍后在例程中检索。您应该考虑发布您的代码。从你目前所描述的情况来看,听起来你让事情变得比他们需要的更困难。

标签: excel match cell vba


【解决方案1】:

我从“通过信息搜寻和啄食以构建宏以提高在视频后期制作设施中完成的所有工作的效率”猜测您对 VBA 的了解有限并且您不知道可能是什么功能相关的。这个答案是对我认为你需要的事件例程的介绍。

发生事件:打开或关闭工作簿、添加工作表、更改单元格值等。对于其中许多事件,您可以创建一个例程,以便在该事件发生时调用。

为了演示这些例程,我创建了一个工作簿和一个工作表“Work”。我已将 12 个单元格设置为数值并将 R12 设置为它们的总和。我已将范围 J2:O25 设置为数字 1 到 144。这比您想要的范围更大,但这对原理没有影响。

在下面的代码中,我使用了两个事件例程:Workbook_OpenWorkbook_SheetChange

Workbook_Open中,我保存了R12的原始值。在Workbook_SheetChange 中,如果 R12 的值发生了变化,并且如果 J2:O25 包含新值,我将光标移动到它。

如果我理解您的问题,这就是您寻求的功能。如果没有,我希望这个答案能帮助您提出一个更好、更详细的问题。

您必须将此语句放在一个模块中:

  Public R12Last As Variant

您必须将以下代码放在ThisWorkbook 中,您可以在工作表下的Microsoft Excel Objects 中找到该代码。

Option Explicit
Sub Workbook_Open()

  ' This routine is called when the workbook is opened.

  With Worksheets("Work")
    ' Save the value of cell R12 when the workbook opens.
    R12Last = .Range("R12").Value
  End With

End Sub

Private Sub Workbook_SheetChange(ByVal WSht As Object, ByVal ChgRng As Range)

  ' This routine is called when cells in any worksheet are changed by the
  ' user or by an external link.

  ' WSht is the worksheet within which cells have changed.
  ' ChgRng can be a single cell or a range.

  ' For this application, we are not interested in the cell that has changed.
  ' We want to know if the change affected R12.

  Dim SearchRng As Range
  Dim FoundRng As Range

  With Worksheets("Work")
    If R12Last <> .Range("R12").Value Then
      ' Cell R12 has changed
      R12Last = .Range("R12").Value     ' Record the new value for next time

      ' I have a larger range containing the values that might be in R12
      ' but the principle is the same.  You will need to change this to M31:M40.
      Set SearchRng = .Range("J2:O25")

      ' Look for the new value of R12 in SearchRng.
      ' "What" is the value to be found.  "After" must be within the search
      ' range. "After" is the last cell to be searched.  I have set
      ' SearchDirection to xlNext and SearchOrder to xlByRows so the Find will
      ' check cells starting with J2.
      ' Look Find up in VBA Help for a fuller description.
      Set FoundRng = SearchRng.Find(What:=R12Last, After:=Range("O25"), _
                                    LookIn:=xlValues, LookAt:=xlWhole, _
                                    SearchOrder:=xlByRows, _
                                    SearchDirection:=xlNext)
      If FoundRng Is Nothing Then
        ' The value of R12 has not been found in the search range.
        ' Add code to handle this situation
        Call MsgBox("Target value not found", vbOKOnly)
      Else
        ' Make the cell with the matching value the active cell.
        FoundRng.Select
      End If
    End If
  End With

End Sub

【讨论】:

  • 实际上,似乎没有人理解我的问题,我会承担一半的责任......在另一个网站上搜索产生了我的解决方案:
【解决方案2】:

实际上,似乎没有人理解我的问题,我会承担一半的责任......在另一个网站上搜索产生了我的解决方案:

Range(ActiveCell.Address).Name = "StartCell"

LOOPING CODE HERE

Application.Goto "StartCell"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多