查找包含条件的单元格的行号

时间:2019-06-21 04:20:45

标签: excel vba find criteria

我需要在C列中查找包含“ 120”且没有重复的单元格的第一行(我的数据中每个数字代码有10个以上,我只需要第一个)。因此,代码应选择包含例如120、7120、81200。

我在下面尝试的代码仅设法找到包含120的单元格的第一行号。作为参考,AGCL是从另一个find函数派生的列字母,而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

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

2 个答案:

答案 0 :(得分: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

现在要检查是否已找到该值,您可以在此循环的“如果条件”中进行播放。这样您就不会再次获得相同的值。

答案 1 :(得分:0)

已更新:好的,我上次更新了。如前所述,我不知道您要如何使用这些额外的值...但是此函数会将它们输出到任何地方...?

祝你好运。

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

如果您确实只想“包含”部分匹配,则可以使用更多

clear_meta = sa.MetaData(bind=session.connection(), schema=None)