简体   繁体   English

根据列值过滤工作表数据存在于另一工作表中

[英]Filter sheet data based on a column value exists in another sheet

Ive a sheet with data similar to this 我有一张数据与此类似的工作表

Users  Role
-----------
Name1   a
Name2   b
Name3   c
Name4   d
Name5   e
Name6   f
Name7   g

Another Sheet 另一张纸

Users Req
------
Name4
Name6
Name7

So the sheet data should be filtered based on the column values exists in second sheet. 因此,应基于第二张工作表中存在的列值对工作表数据进行过滤。 So after filtering the first sheet looks like this. 因此,过滤后的第一张纸看起来像这样。

Users   Role
-------------
Name4    d
Name6    f
Name7    g

How can we achieve like this 我们怎样才能做到

Save the values in the Other sheet in an array, in my code below it's FilterArr , and then you can AutoFilter according to the values in this array by using Criteria1:=FilterArr . 保存在其他表中的值的数组,在我的代码下面是FilterArr ,然后就可以AutoFilter通过使用根据此数组中的值Criteria1:=FilterArr

More explanations inside the code below as comments: 下面的代码内有更多解释作为注释:

Code

Option Explicit

Sub FilterAccordingtoValuesInOtherSheet()

Dim Sht1 As Worksheet, sht2 As Worksheet
Dim FilterArr As Variant
Dim LastRow As Long

Set Sht1 = ThisWorkbook.Sheets("Sheet1") ' modify to your sheet's name where you have the data you want to filer
Set sht2 = ThisWorkbook.Sheets("Sheet2") ' modify to your sheet's name where you have values you want to use for the filter

With sht2
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' get last row with data in column A

    FilterArr = Application.Transpose(.Range("A2:A" & LastRow).Value2)  ' get the values from the second sheet to an array
End With

With Sht1
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' get last row with data in column A

    ' set the Auto-Filter by column A, to the values in the array
    .Range("A1:B" & LastRow).AutoFilter Field:=1, Criteria1:=FilterArr, Operator:=xlFilterValues
End With

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查工作表中是否存在列,如果存在将数据复制到另一个工作表 - Checking if a column exists in a sheet and if exists copies data to another sheet 根据另一个工作表中的数组过滤列 - Filter column based on array from another sheet 根据另一个excel表中的数据过滤一个excel表中的数据 - filter data in one excel sheet based on data in another excel sheet 在“列”中查找相同的数据并将其过滤到另一张表 - Find identical data in Column and filter it to another sheet 根据另一张纸上的订单号从一张纸上查找列值 - Find column value from one sheet based on the order # on another sheet Excel:从另一个工作表中的值筛选工作表 - Excel: Filter a sheet from value in another sheet 如何根据另一个工作表上的工作表值将图片添加到另一个工作表? - How to add a picture to another sheet based on the sheet value on another sheet? 在另一张纸上的一行中查找值,然后将其值放置在当前纸上的匹配行和列标题中(如果存在),如果没有则将其放置为0 - Finding a value in a row on another sheet and placing a value in the matching row and column header on current sheet if exists and 0 if it doesnt 过滤 excel 列并将粘贴复制到另一个新工作表,工作表名称应为过滤器值 - Filter a excel column and copy paste to another new sheet and the sheet name should be filter value 如何根据另一个参考列中的数据值填充或更新 excel 工作表中的列值 - How to populate or update column value in an excel sheet based on data values in another reference column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM