如何使用匹配功能获取多个值在列中的位置

时间:2019-04-12 15:56:39

标签: excel match

出于这个问题的目的,我正在使用一个数据集,该数据集的值为0、1和-1。我想运行两个单独的匹配函数。我想找到所有包含1s的列,然后找到所有包含-1s的列。我希望将这些结果在新单元格中横向放置。我一直在成功使用match函数,但是仅当每个数字只有一次出现时才使用。

我已经在Google上进行了广泛的搜索,但是我只能找到人们将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函数正常运行的正确方法。预先谢谢你!

1 个答案:

答案 0 :(得分:0)

您会反对使用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