excel宏中的Match函数仅给出第一个结果

excel宏中的Match函数仅给出第一个结果,excel,vba,Excel,Vba,我是excal宏/vba新手,遇到了一个我不知道如何解决的问题 我有一本包含几页的工作簿。有1个文件或多或少是一个主列表,还有3个文件是一个装箱单 我在3个装箱单中分别插入了一个带有宏的命令按钮,它告诉我主装箱单中是否存在某个项目,如果存在,它会告诉我它出现在哪一行。这很好,但是我的问题是,如果某个特定项目在主列表中出现多次(由于购买日期不同),宏只给出第一个结果 我想知道是否有任何方法可以让所有可能的结果都出现,而不仅仅是第一个结果 下面是我使用的代码示例 Private Sub Comman

我是excal宏/vba新手,遇到了一个我不知道如何解决的问题

我有一本包含几页的工作簿。有1个文件或多或少是一个主列表,还有3个文件是一个装箱单

我在3个装箱单中分别插入了一个带有宏的命令按钮,它告诉我主装箱单中是否存在某个项目,如果存在,它会告诉我它出现在哪一行。这很好,但是我的问题是,如果某个特定项目在主列表中出现多次(由于购买日期不同),宏只给出第一个结果

我想知道是否有任何方法可以让所有可能的结果都出现,而不仅仅是第一个结果

下面是我使用的代码示例

Private Sub CommandButton1_Click()

Dim k As Integer

For k = 3 To 1000

Cells(k, 24).Value = Application.Match(Cells(k, 2), Sheets("master").Range("B2:B1000"), 0)

Next k

End Sub
我会使用a来存储主工作表中的每个项目,每次您发现它重复时,都会添加另一个编号及其行,如下所示:

Option Explicit
Private Sub CommandButton1_Click()

    Dim MasterKeys As Object

    MasterKeys = FillDictionary(MasterKeys)

    With ThisWorkbook.Sheets("MySheet") 'change MySheet for your actual sheet name

        Dim arr As Variant
        arr = .UsedRange.Value 'drop your data inside an array

        Dim i As Long

        For i = 3 To UBound(arr) 'loop through all the rows in your data
            If MasterKeys.Exists(arr(i, 2)) Then arr(i, 24) = MasterKeys(arr(i, 2))
        Next i

        .UsedRange.Value = arr 'drop back your data

    End With

End Sub
Function FillDictionary(MasterKeys As Object) As Object

    Set MasterKeys = CreateObject("Scripting.Dictionary")

    With Workbooks("MasterWorkbook.xlsx").Sheets("master") 'change MasterWorkbook for the actual filename of your master workbook

        Dim LastRow As Long
        LastRow = .Cells(.Rows.Count, 2).End(xlUp).Row 'find the last row on column B

        Dim C As Range
        For Each C In .Range("B2:B" & LastRow) 'loop through the range
            If Not MasterKeys.Exists(C.Value) Then
                MasterKeys.Add C.Value, C.Row
            Else
                MasterKeys(C.Value) = MasterKeys(C.Value) & "," & C.Row
            End If
        Next C

    End With

End Function
如果您的“主”工作表数据是从B2到最后一个非空单元格的连续非空单元格列表,那么这里有一种不同的方法来处理

Option Explicit

Private Sub CommandButton1_Click()
    Dim cell As Range

    With Worksheets("master") ' reference your "master" sheet
        With .Range("B2", .Cells(.Rows.Count, "B").End(xlUp)) ' reference referenced sheet column B range from row 2 down to last not empty one
            For Each cell In Range("B3", Cells(Rows.Count, "B").End(xlUp)) ' loop through packinglist sheet (i.e. where button resides) column B cells from row 3 down to last not empty one
                If Not .Find(what:=cell.Value2, LookIn:=xlValues, lookat:=xlWhole) Is Nothing Then ' if current packinglist item is in "master"
                    .Replace what:=cell.Value2, replacement:=vbNullString, lookat:=xlWhole ' temporarily replace master item with a blank
                    cell.Offset(, 22).Value2 = Replace(.SpecialCells(xlCellTypeBlanks).Address(False, False), "B", "") ' write master list blanks rows in packinglist sheet current item row and column "X"
                    .SpecialCells(xlCellTypeBlanks).Value = cell.Value2 ' restore master list current packinglist item value
                End If
            Next
        End With
    End With
End Sub

Match
只能返回一个结果。如果查找多个结果,请查看
AutoFilter
功能和
Find
功能。你会发现很多关于这方面的教程关于代码的2个注释:① 我建议使用
Long
而不是
Integer
Excel的行数超过
Integer
所能处理的行数。② 确保为每个
单元格
区域
对象指定一个工作表,就像您在代码中的最后一个区域所做的那样。您好,谢谢您的评论,我将仔细阅读这些内容,并试着看看是否可以将其合并到我需要的内容中。您好!是的,我在该列中的行都已填充,中间没有任何空白单元格。我已经试过你的代码,这个函数正是我想要的。我试图从那里了解每个函数的逻辑和用途,因此编辑我的其他工作簿/工作表,并尝试根据不同的列(S/N)而不是行号本身给出输出。非常感谢!