更快的方式来筛选出基于特定值的数据

我正在与一个工作簿,目前有3张。 第一张表格是过滤数据出现的概况。 细胞D11有我正在寻找的颜色。 进入彩色单元格F3:I27填充颜​​色,形状,数量和动物等信息。

C2C,Tracker2

我将使用一个数据透视表,但是,我有另一组数据在K3:M27 。 这些数据是从工作簿中的另一个工作表中以类似的function提取的。

我正在使用的公式是:

 =IFERROR(INDEX(cases!A:A,SMALL(IF(EXACT($D$3,cases!$C:$C),ROW(cases!$C:$C)-ROW($F$1)+1),ROW(1:1))),"") 

当然,它是使用CTRL + SHIFT + ENTERinput它正常工作。

我尝试使用从下面的video中提取的VBAmacros:

Excel VBA循环查找logging匹配的search标准

如此多的数组公式可以使您的工作簿非常慢。

以下是使用数组填充数据Dataset1 1的代码。 它运行在不到一秒钟

希望这会让你开始。 我已经评论了代码,但如果你仍然有一个问题的理解,只是回发:)

 Sub Sample() Dim DSOne() As String Dim tmpAr As Variant Dim wsCas As Worksheet: Set wsCas = ThisWorkbook.Sheets("Cases") Dim wsMain As Worksheet: Set wsMain = ThisWorkbook.Sheets("Sheet1") Dim lRow As Long, i As Long, j As Long '~~> Check if user entered a color If wsMain.Range("D3").Value = "" Then MsgBox "Please enter a color first", vbCritical, "Missing Color" Exit Sub End If '~~> Clear data for input in main sheet wsMain.Range("F3:F" & wsMain.Rows.Count).ClearContents '~~> Get last row of Sheet Cases lRow = wsCas.Range("A" & wsCas.Rows.Count).End(xlUp).Row With wsCas '~~> Get count of cells which have that color i = Application.WorksheetFunction.CountIf(.Columns(3), wsMain.Range("D3").Value) '~~> Check if there is any color If i > 0 Then '~~> Define your array to hold those values ReDim DSOne(1 To i, 1 To 4) '~~> Store the Sheet Cases data in the array tmpAr = .Range("A1:D" & lRow).Value j = 1 '~~> Loop through the array to find the matches For i = LBound(tmpAr) To UBound(tmpAr) If tmpAr(i, 3) = wsMain.Range("D3").Value Then DSOne(j, 1) = tmpAr(i, 1) DSOne(j, 2) = tmpAr(i, 2) DSOne(j, 3) = tmpAr(i, 3) DSOne(j, 4) = tmpAr(i, 4) j = j + 1 End If Next i '~~> write to the main sheet in 1 Go! wsMain.Range("F3").Resize(UBound(DSOne), 4).Value = DSOne End If End With End Sub 

截图

在这里输入图像说明 现在使用上面的方法填充Dataset2 🙂