【问题标题】:find row number of cell that contains criteria查找包含条件的单元格的行号
【发布时间】:2019-06-21 04:20:45
【问题描述】:

我需要在 C 列中找到包含“120”且不重复的单元格的第一行编号(我拥有的数据有超过 10 个每个数字代码,我只需要第一个)。所以代码应该选择包含例如的第一行号。 120、7120、81200。

我在下面尝试的代码仅设法找到包含 120 的单元格的第一行号。作为参考,AGCL 是从另一个查找函数派生的列字母,tbAC 是用户输入文本框。

Dim AGCN As Long
Dim AGCL As String
Dim AGNN As Long
Dim AGNL As String
Dim i As Long
Dim RowD As Long
Dim AAC As String
Dim rng As Range
Dim rownumber As Long
Dim AGC As Range
Dim AGN As Range
Dim firstaddress As Long
Dim nextaddress As Long

Set rng = Sheet1.Columns(AGCL & ":" & AGCL).Find(what:="*" & tbAC & "*", 
LookIn:=xlValues, lookat:=xlPart)
    rownumber = rng.Row
    Debug.Print rownumber '9

With Sheet1.Range(AGCL & ":" & AGCL)
    Set c = .Find("*" & tbAC & "*", LookIn:=xlValues)
    If Not c Is Nothing Then
        firstaddress = c.Value
        Debug.Print firstaddress
            With Me.ListBox2
           .ColumnCount = 3
           .ColumnWidths = "50;150;70"
           .AddItem
           .List(i, 0) = Str(firstaddress)
           i = o + 1
           End With

        Do
            Set c = .FindNext(c)
            If c Is Nothing Then
                GoTo donefinding
            ElseIf firstaddress <> c.Value Then
                nextaddress = c.Value
                Debug.Print nextaddress 'it doesn't print any value here
                'With Me.ListBox2
                '   .ColumnCount = 3
                '   .ColumnWidths = "50;150;70"
                '   .AddItem
                '   .List(i, 0) = Str(nextaddress)
                '   Debug.Print nextaddress
                '   i = o + 1
                'End With
            End If
        Loop While c.Address <> firstaddress 

    End If
donefinding: Exit Sub
End With

任何帮助将不胜感激,谢谢!

【问题讨论】:

  • 这是您使用的完整代码吗?
  • 您可以在一个数组中赋值,并循环遍历数组以匹配找到的下一个值。这样,您将获得包含 120 的所有不同单元格的第一行。
  • 我需要一个关于如何继续的补充,这部分工作,但我找不到 7120 和 81200 的其他行号,它们位于此工作表的第 58 和 256 行
  • @Mikku 你是怎么做的?对数组函数不是很熟悉
  • 不需要数组,我想了更多,我下面的答案将在没有任何数组的情况下完成。尝试遵循这一点。

标签: excel vba find criteria


【解决方案1】:

这里是 Range.FindNext 函数,您可以使用它来检索所有具有 120 的单元格。

With Sheet1.Range(AGCL & ":" & AGCL)
     Set c = .Find("*" & tbAC & "*", lookin:=xlValues)
     If Not c Is Nothing Then
        firstAddress = c.Address
        Do

            Set c = .FindNext(c)
        If c is Nothing Then
            GoTo DoneFinding
        Elseif not firstaddress.value = c.value

          ''Whatever you want to do with the Second Found Value
          debug.print c.value

        End If
        Loop While c.Address <> firstAddress
      End If
      DoneFinding:
End With

现在要检查是否已找到该值,您可以在此循环的 If 条件中播放。这样你就不会再得到相同的值了。

【讨论】:

  • 这对我来说似乎是一个很好的答案!我会支持你让我更接近展示我是一项 F#@$ 的好运动!
  • @Mikku 所以我已经尝试过了,但是 excel 要么崩溃,要么不打印值。以上是我尝试过的修改后的代码
  • @Valwrie .. 我没有看到您将任何值设置为 AGCL ... AGCL 是什么?
  • @Valwrie .. 还可以尝试使用 F8 调试代码。这样你就会看到错误在哪里。
  • @Mikku AGCL 是我之前推导出的一个列字母,已提取所有声明以供参考,只有在我寻找“下一个地址”时才会弹出错误
【解决方案2】:

已更新:好的,我上次更新了。如前所述,我不知道你想用额外的值做什么......但是这个函数会将它们输出到任何地方......?

祝你好运。

这是一个与您要查找的内容相匹配的自定义函数,它将在单元格中第一次出现 120 时返回...

如果您真的想要“包含”仅部分匹配项,您可以使用以下一个。

Function SuperSearcherTHING(ivalue As Variant, theColumn As Range) As String
Dim rCell As Range
Const theSPACER As String = "|"
For Each rCell In Intersect(theColumn.EntireColumn, theColumn.Worksheet.UsedRange).Cells
    If InStr(1, rCell.Value, ivalue, vbTextCompare) > 0 Then
        SuperSearcherTHING = rCell.Value & theSPACER & SuperSearcherTHING
    End If

Next rCell

SuperSearcherTHING = Left(SuperSearcherTHING, Len(SuperSearcherTHING) - Len(theSPACER))

End Function

【讨论】:

  • 你将如何使用这个找到多个值??
  • 哦...所以你想找到一个不是120120而是120的单元格?
  • 是的,那么 searcherTHING 功能最好(第一个)
猜你喜欢
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 2022-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多