使用数组搜索活动工作表

时间:2017-09-09 13:13:14

标签: arrays excel vba excel-vba

我有一张工作表,会定期更新数据库中重复记录的详细信息。

我正在尝试编辑我们当前搜索特定记录的宏,但是因为我是Excel VBA的新手,我正在努力。

宏工作得很好但是它只返回每个ID号的单元格引用一次。我正在努力弄清楚如何在每次列出指定的ID号时返回每个单元格引用。

我目前拥有的代码如下(代码中有更多ID号,但为了节省我删除这些代码的时间):

            Sub IDSearch()
'


Dim rg As Range
Dim lnglastrow As Long
Dim intnamemax As Integer
Dim strName() As String
Dim fnd As Boolean


intnamemax = 42

ReDim strName(1 To intnamemax)

strName(1) = "OR123456"
strName(2) = "C00123456"
strName(3) = "UK123456"


lnglastrow = ActiveSheet.UsedRange.Rows.Count

For I = 1 To intnamemax
    Set c = Range("j2:j" & lnglastrow).Find(strName(I), LookIn:=xlValues)
    If Not c Is Nothing Then
        MsgBox "Proxy Candidate Found at " & c.Address
        fnd = True
    End If
Next I
If Not fnd Then
    MsgBox "No Proxy Candidates Found ", vbOKOnly, "Success!"
End If

End Sub

3 个答案:

答案 0 :(得分:0)

请尝试以下代码:

Sub IDSearch()
'


Dim rg As Range
Dim lnglastrow As Long
Dim intnamemax As Integer
Dim strName() As String
Dim fnd As Boolean    


intnamemax = 42

ReDim strName(1 To intnamemax)

strName(1) = "OR123456"
strName(2) = "C00123456"
strName(3) = "UK123456"


lnglastrow = ActiveSheet.UsedRange.Rows.Count

For I = 1 To intnamemax
    For J = 2 to lnglastrow
        'Set c = Range("j2:j" & lnglastrow).Find(strName(I), LookIn:=xlValues)
        If Instr(Range("J" & J).Value, strName(I)) > 0 Then
        'If Not c Is Nothing Then
            'MsgBox "Proxy Candidate Found at " & c.Address
            MsgBox "Proxy Candidate Found at " & "J" & J
            fnd = True
        'End If
    Next J
Next I

If Not fnd Then
    MsgBox "No Proxy Candidates Found ", vbOKOnly, "Success!"
End If

End Sub

希望这个帮助

答案 1 :(得分:0)

试试这个

Option Explicit

Sub IDSearch()

    Dim intnamemax As Integer
    intnamemax = 42

    Dim strName() As String
    ReDim strName(1 To intnamemax)

    strName(1) = "OR123456"
    strName(2) = "C00123456"
    strName(3) = "UK123456"

    Dim lnglastrow As Long
    lnglastrow = ActiveSheet.UsedRange.Rows.Count

    Dim c As Range
    Dim fnd As Boolean

    Dim i As Integer
    For i = 1 To intnamemax
        Set c = Range("j2")
        Do While True
            If strName(i) = "" Then Exit Do
            Set c = c.Resize(lnglastrow).Offset(1).Find(strName(i), LookIn:=xlValues)
            If Not c Is Nothing Then
                Debug.Print strName(i); " found at " & c.Address
    '           MsgBox "Proxy Candidate Found at " & c.Address
                fnd = True
            Else
                Exit Do
            End If
        Loop
    Next i

    If Not fnd Then
        MsgBox "No Proxy Candidates Found ", vbOKOnly, "Success!"
    End If

End Sub

答案 2 :(得分:-1)

结帐.FindNext。像这样的东西(没有测试过它)。

Dim f As Range
For I = 1 To intnamemax
    Set c = Range("j2:j" & lnglastrow).Find(strName(I), LookIn:=xlValues)
    If Not c Is Nothing Then
        f = c.Address
        MsgBox f
        Do
            Set c = c.FindNext(c)
            MsgBox c.Address
        Loop While Not c Is Nothing And c.Address <> f
    End If
Next I