[英]Excel - find a value in column A that appears less than or equal to 4 times and print in column B
I have a list of usernames sorted alphabetically in column A, with some appearing numerous times. 我在A列中按字母顺序排列了一个用户名列表,其中一些出现了无数次。 I want to prnt the username in column B if it appears less than or equal to 4 times. 如果用户名出现少于或等于4次,我想打印B列中的用户名。
Do I need an array to go through all the different username values in the column to find the ones that appear less than or equal to 4 times? 我是否需要一个数组来遍历列中所有不同的用户名值,以查找出现少于或等于4次的值?
Consider: 考虑:
Sub dural()
Dim A As Range, B As Range, v As String, K As Long
Set A = Intersect(Range("A:A"), ActiveSheet.UsedRange)
Set B = Range("B:B")
K = 1
With Application.WorksheetFunction
For Each aa In A
v = aa.Value
If v <> "" Then
If .CountIf(A, v) <= 4 Then
If .CountIf(B, v) = 0 Then
Cells(K, "B").Value = v
K = K + 1
End If
End If
End If
Next aa
End With
End Sub
Add a helper column and place the following formula in the second row: 添加一个帮助器列,并将以下公式放在第二行中:
=IF(AND(COUNTIF(A:A,A2)<=4,COUNTIF($A$2:A2,A2)=1),MAX($B$1:B1)+1,"")
And copy down: 并抄下来:
At this point you can filter on the non Blank Cells and copy them to another range. 此时,您可以过滤非空白单元格并将其复制到另一个范围。
If you want to use a formula to get the list then Put this in another column row 2: 如果要使用公式来获取列表,则将其放在另一列的第2行中:
=IFERROR(INDEX(A:A,MATCH(ROW(1:1),B:B,0)),"")
And copy down. 并抄下来。
No need for helper columns or VBA, just a bit of finely-tuned IF functions :) 不需要辅助列或VBA,只需调整一下IF函数即可:)
=IF(COUNTIFS(BE:BE,BE2)<=4,IF(COUNTIFS($BE$1:BE2,BE2)=1,BE2,"0"),"0")
^Here, BE is where all your data is, starting on Row 2 ^在这里,BE是您所有数据的存放地,从第2行开始
What it does: 它能做什么:
If the Name appears 4 or fewer times, 如果名称出现4次或以下,
If this is the first time the Name appears in the column 如果这是第一次,“名称”将出现在列中
To remove the 0 values ie empty rows: 要删除0值,即空行:
Replace All
( Ctrl-H ) "0"s with nothing "" (so that you can..) .. Replace All
( Ctrl-H )将“ 0”替换为“”(以便可以..) Go To
( Ctrl-G ) > Special > Blanks
..使用Go To
( Ctrl-G ) > Special > Blanks
来选择空白行 You can also simply Filter out the Blank/0 values 您也可以简单地过滤出Blank / 0值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.