0

全部。我是VBA和MS EXCEl 2010的新手。我昨天才开始玩EXCEL中的宏模块,几乎是零经验。

我试图做的描述如下。首先在 sheet1 的某个范围内搜索值记录,然后对于找到的每个单元格,我找到该行并在该行中提取另一个单元格值。使用此值在 sheet2 的范围内进行另一次搜索。我会在我的代码之后指出我遇到的问题。这是伪代码。

 Dim Found As Range
 With RangeInSheet1
        Set Found = .Find(value1)
        If Not Found Is Nothing Then
            firstAddress = Found.Address
            Do  
                With RangeInSheet2
                    ColumnIndex = .Find(value2).Column
                End With
                Set Found = .FindNext(Found)
            Loop While Not Found Is Nothing And Found.Address <> firstAddress
        End If
End With

value1 是我用来在 RangeSheet1 中搜索的键,而 value2 是在 RangeSheet2 中搜索的键。上面的代码遍历了我在工作表 1 中为 value1 找到的每条记录,并在 Sheet2 中进行了另一次搜索。

现在假设 value1 = 1, value2 =2007,并且在工作表 1 中有 5 条记录包含 value1。问题出在这行代码“ColumnIndex = .Find(value2).Column”。

假设对于所有五个找到的记录,在执行“Set Found = .FindNext(Found)”之后,Found的值应该始终为1。但是,在我添加了这个 ColumnIndex 代码之后,Found 的值设置为 2007,这对我来说太奇怪了。任何人都知道问题是什么?任何帮助将不胜感激。我真的需要按照我的意愿保持 Found 的行为“正常”。

如果有任何不清楚的地方,请告诉我

4

1 回答 1

3

.Find/.Findnext记住最后的设置。因此,始终建议完全指定参数。特别After:=参数。它还会记住您的最后一个搜索词,即What:=

这是有关如何使用的演示.Find/.Findnext

也不要Value2用作变量。它是一个保留字。而不是使用Value1and Value2,我在下面的代码中使用sSearch1andsSearch2

假设您的床单看起来像这样

在此处输入图像描述

现在试试这个代码

Sub Sample()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim rngWs1 As Range, rngWs2 As Range
    Dim aCell As Range, bCell As Range, cCell As Range, dCell As Range, eCell As Range, cl As Range
    Dim sSearch1, sSearch2

    Set ws1 = ThisWorkbook.Sheets("Sheet1")
    Set rngWs1 = ws1.Range("A1:A10")

    Set ws2 = ThisWorkbook.Sheets("Sheet2")
    Set rngWs2 = ws2.Cells

    With ws1
        For i = 1 To 10
            sSearch1 = .Range("A" & i).Value
            Set aCell = .Range("A" & i)

            If Len(Trim(sSearch1)) <> 0 Then
                Set aCell = rngWs1.Find(What:=sSearch1, After:=aCell, LookIn:=xlValues, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)

                If Not aCell Is Nothing Then
                    sSearch2 = aCell.Offset(, 1).Value

                    With ws2
                        Set bCell = rngWs2.Find(What:=sSearch2, After:=.Range("A1"), LookIn:=xlValues, _
                                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                MatchCase:=False, SearchFormat:=False)

                        If Not bCell Is Nothing Then
                            Debug.Print "For " & sSearch1 & ", " & sSearch2 & " Found in " & bCell.Address

                            Set cCell = bCell

                            Do
                                Set bCell = rngWs2.FindNext(After:=bCell)

                                If Not bCell Is Nothing Then
                                    If bCell.Address = cCell.Address Then Exit Do
                                    Debug.Print "For " & sSearch1 & ", " & sSearch2 & " Found in " & bCell.Address
                                Else
                                    Exit Do
                                End If
                            Loop
                        End If
                    End With
                End If
            End If
        Next
    End With
End Sub

这是我们得到的结果。

在此处输入图像描述

于 2013-11-09T07:45:34.540 回答