【问题标题】:Excel - Find cell containing nth occurrence of dataExcel - 查找包含第 n 次出现数据的单元格
【发布时间】:2020-01-24 13:56:08
【问题描述】:

我在 Excel 中有一组与此类似的数据:

24/01/2020      25/01/2020      26/01/2020      27/01/2020
Item A          Item A          Item B          Item C
Item B          Item C          Item C          Item D
Item C                          Item D

我可以运行一个公式来确定某个项目在 dataRange 中出现的次数:=COUNTIF(dataRange,"Item C")

我现在要做的是获取与每第 n 次出现相关的日期。我在网上找到的所有内容都只涉及在单个列中找到第 n 个出现,而我想要一个公式来告诉我每个出现的单元格在整个范围内。例如。 “项目 C”第一次出现在单元格 A4 中,第二次出现在 B3 中,第三次出现在 C3 中,第四次出现在单元格 D2 中。

谢谢大家!

【问题讨论】:

  • 如果这与您之前(已删除)的问题有关。我觉得您希望通过 VBA 完成此操作。如果是这种情况,您可以使用Range.FindFindNext
  • 这确实是结合的。我不确定最好的协议(我知道最后一项非常模棱两可,需要一个更全面的问题)。非常感谢。我将阅读有关 Range.Find 和 FindNext 的信息

标签: excel


【解决方案1】:

感谢@JvdV 提供的帮助。使用 Microsoft Docs,我找到了 Range.FindFindNext 的组合,结果如下:

Sub cellAddresses()

    With Range("A1:D6")

        Set c = .Find("Item A", LookIn:=xlValues)
        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                MsgBox c.Address
                Set c = .FindNext(c)
                Loop While c.Address <> firstAddress
        End If
    End With

End Sub

这会返回每次出现的单元格地址,并确保一旦所有出现都循环通过,就不会反馈更多信息。

再次感谢!

【讨论】:

    【解决方案2】:

    你也可以试试Worksheet Change Event:

    在单元格 G1 中导入 Item_Code

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Not Intersect(Target, Me.Range("G1")) Is Nothing Then
    
            Dim i As Long, j As Long, Counter As Long, LastRow As Long
            Dim ItemCode As String
    
            Counter = 1
    
            With Me
    
                ItemCode = Target.Value
    
                For i = 1 To 4
    
                    For j = 2 To .Cells(.Rows.Count, i).End(xlUp).Row
    
                        If .Cells(j, i).Value = ItemCode Then
    
                            LastRow = .Cells(.Rows.Count, "I").End(xlUp).Row + 1
    
                            Application.EnableEvents = False
    
                                .Range("I" & LastRow).Value = .Cells(1, i).Value
                                .Range("J" & LastRow).Value = Counter
                                .Range("K" & LastRow).Value = .Cells(j, i).Address
    
                            Application.EnableEvents = True
    
                            Counter = Counter + 1
    
                        End If
    
    
                    Next j
    
                Next i
    
            End With
    
        End If
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-07
      • 2015-12-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      相关资源
      最近更新 更多