[英]How to use match function to get the position of multiple values across columns
我正在使用一个数据集,出于此问题的目的,其值为0,1和-1。 我想运行两个单独的匹配函数。 我想找到包含1的所有列,然后找到包含-1的所有列。 我希望这些结果可以水平地放在新单元格中。 我一直在成功使用匹配功能,但只在每个号码只出现一次时才会使用。
我已经广泛搜索过,但我只能找到人们正在组合INDEX + MATCH( https://exceljet.net/formula/extract-multiple-matches-into-separate-columns )的案例。
id 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
1 0 0 0 0 0 -1 0 0 1 0
2 0 0 0 -1 0 0 1 0 0 0
3 0 0 0 -1 0 0 0 1 0 0
4 0 -1 0 0 0 0 1 0 0 0
5 0 0 0 1 0 0 -1 1 0 0
# Match function:
= MATCH(-1, B2:B11,0)
= MATCH(1, B2:B11,0)
# This would output, for example:
id 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 neg1.pos1
1 0 0 0 0 0 -1 0 0 1 0 6
1.pos1
9
# But this doesn't work if there is more than one instance of -1 or 1, it just gives the index of the first column that contains either number.
我想要:
id 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 neg1.pos1
5 0 0 0 1 0 0 -1 1 0 0 7
1.pos1 1.pos2
4 8
正如您所看到的,对我来说无关紧要的是列中的值,我只想要特定值所在的列号。如果每行有多个值,我想这样做。
如果有一个公式可以做到这一点,请告诉我,如果我甚至在正确的轨道上使MATCH功能工作。 先感谢您!
你会反对使用VBA吗? 我不确定如何在数组公式中可视化所有值,但我可以想到一种循环遍历每一行并创建一个数组来填充每列#的方法。
我做了一些假设:
列“id”在A列中
标题在第1行
将有变量#行
不会有变量#列
Sub ColCheck()
Dim ColNum, rCount, i As Long
Dim OneArray, TwoArray As Variant
rCount = Sheet2.Range("A" & Rows.Count).End(xlUp).Row
i = 2
Do While i <= rCount
OneArray = ""
TwoArray = ""
For Each cell In Sheet2.Range("B" & i & ":K" & i)
If cell.Value = -1 And OneArray = "" Then
OneArray = cell.Column
ElseIf cell.Value = -1 And OneArray <> "" Then
OneArray = OneArray & ", " & cell.Column
End If
Next
For Each cell In Sheet2.Range("B" & i & ":K" & i)
If cell.Value = 1 And TwoArray = "" Then
TwoArray = cell.Column
ElseIf cell.Value = 1 And TwoArray <> "" Then
TwoArray = TwoArray & ", " & cell.Column
End If
Next
If OneArray = "" Then OneArray = "No value found"
If TwoArray = "" Then TwoArray = "No value found"
With Sheet2
.Range("M1").Value = "-1 Position"
.Range("M" & i).Value = OneArray
.Range("N1").Value = "1 Position"
.Range("N" & i).Value = TwoArray
End With
i = i + 1
Loop
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.