【问题标题】:Find All Instances in Excel Column在 Excel 列中查找所有实例
【发布时间】:2018-05-22 23:41:56
【问题描述】:

上下文

我正在尝试在特定列中搜索“January”的所有实例,然后引用该行,以便将单元格直接复制到它的左侧。单元格包含公式,所以我实际上是在搜索单元格的值而不是公式。

问题

我一直在尝试测试并确保它引用了正确的单元格地址(特别是行)。对于 1 月的每个实例,它会将“Testing”粘贴到列中所有值的末尾,而不是 FoundCell 本身中。 (例如,如果一月出现了 2 次,它会在列的末尾不断循环“测试”)

Excel 示例

Month      Month
January    =A2
February   =A3
April      =A4
April      =A5
January    =A6
December   =A7

当前代码

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

Set myRange = Range("B:B")
Set LastCell = myRange.Cells(myRange.Cells.Count)
Set FoundCell = myRange.Find(what:=January, after:=LastCell, LookIn:=xlValues)

'Test to see if anything was found
If Not FoundCell Is Nothing Then
  FirstFound = FoundCell.Address
Do
    MsgBox "Found it!"
    FoundCell.Value = "Testing"
    Set FoundCell = myRange.FindNext(FoundCell)
  Loop While Not FoundCell Is Nothing And FoundCell.Address <> FirstFound
Else
 MsgBox "Not Found!"
End If

Set rng = FoundCell

Exit Sub

如果你看到任何可以帮助的东西,请告诉我!

【问题讨论】:

  • 一月和测试,它们不是变量,您需要将它们用引号引起来。 Find(what:="January"... FoundCell.Value = "Testing"
  • 我建议使用Option Explicit 作为模块的第一条语句。这迫使您正确声明 all 变量,您会立即看到此类问题。特别是对于初学者来说,这非常有用。
  • 您解释了问题,但我(或其他任何人)(目前)不可能在不进行大量猜测和其他非凡努力来收集一些样本数据的情况下重现问题。那么,您可以编辑问题以包含可以复制此问题的几行数据的示例吗?
  • 您提供的代码遇到运行时 91 错误(VBA 不会短路链式布尔表达式,因此您的 Loop While 语句有问题。
  • 公式的来源是实际日期还是“一月”一词?此外,B 列是否仅包含真实日期或月份名称?另外 - 如果你想在FoundCell 中粘贴“测试”,那么你需要使用FoundCell.Offset(,1) = "Testing"。由于您没有更改正在搜索的值,因此您可以删除 Not FoundCell Is Nothing,因为它总是会在循环时找到一些东西。

标签: vba excel


【解决方案1】:

通过一些修改,我可以让您的代码运行。不确定您到底要做什么,但根据测试数据,此代码不再遇到您的代码在Loop While 遇到的运行时 91 错误:

Sub foo()
Dim fnd As String, FirstFound As String
Dim FoundCell As Range, rng As Range
Dim myRange As Range, LastCell As Range

Set myRange = Range("B:B")
Set LastCell = myRange.Cells(myRange.Cells.Count)
Set FoundCell = myRange.Find(what:="January", after:=LastCell, LookIn:=xlValues)

If Not FoundCell Is Nothing Then
    FirstFound = FoundCell.Address
    Do

        ' FoundCell.Interior.ColorIndex = 39
        FoundCell.Offset(,1).Value = "Testing!"  '<~~ I'm writing out to the adjacent cell, just to be safe. Modify as needed.
        Set FoundCell = myRange.FindNext(FoundCell)

    Loop While (FoundCell.Address <> FirstFound)
End If

Set rng = FoundCell  '<~~ Careful, as this is only the LAST instance of FoundCell.

End Sub

【讨论】:

  • 这正是我在循环正确数量的实例和正确地址方面所寻找的。我的总体目标是找到每个实例并将单元格直接复制到它的左侧。话虽如此,为什么 MsgBox = "" & FoundCell.Offset(0,-1).Value 放置在“Testing”行的位置显示错误“左侧的函数调用必须返回变量或对象?”
  • 去掉 =MsgBox 是一个函数调用,你不能赋值给它。
【解决方案2】:

您一遍又一遍地打印“Testing”这个词,因为您的循环正在搜索“Testing”这个词,而不是“January”。

If Not FoundCell Is Nothing Then
  FirstFound = FoundCell.Address
Do
    MsgBox "Found it!"
    FoundCell.Value = "Testing"
    Set FoundCell = myRange.FindNext(FoundCell)
  Loop While Not FoundCell Is Nothing And FoundCell.Address <> FirstFound
Else

FoundCell.Value = "Testing" 您正在将单元格值设置为使用此行进行测试

Set FoundCell = myRange.FindNext(FoundCell) 现在您正在搜索字符串“Testing”。 FoundCell 是一个范围。我相信单元格范围的默认属性是单元格的值(即 - “测试”)

因此,您最初会找到字符串“January”,将其更改为“Testing”,然后一遍又一遍地搜索字符串“Testing”,因为每次循环都将其写入新单元格,所以您一直在寻找它迭代。

至少我看起来是这样的,我实际上并没有尝试运行代码。您可以通过将 Set FoundCell = myRange.FindNext(FoundCell) 更改为 Set FoundCell = myRange.FindNext(FoundCell.Value) 来修复它

【讨论】:

  • 必须不同意那里。 OP 正在使用在 MSDN 找到的示例,它确实更新了找到的值并继续查找下一个原始值。
  • FindNext 方法不是这样工作的。该方法将Range 对象作为After 参数,因此它会查找由原始调用Find 指定的任何内容的下一个实例。它不会改变原始Findwhat 标准。
  • 你是对的。我在想它是 Find 功能,而不是 FindNext。感谢您的更正。
  • FindNext(FoundCell.Value) 实际上会引发 1004 错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-04
  • 1970-01-01
相关资源
最近更新 更多