在没有列信息的表Excel中查找值

时间:2017-01-31 14:36:45

标签: excel

我遇到了一个我认为不难解决的问题。然而,我没有解决它很难我迫切希望找到解决方案。

情况:

+---+----+-----------+----------+-----------+----------+-----------+----------+-----------+----------+
|   | A  |     B     |    C     |     D     |    E     |     F     |    G     |     H     |    I     |
+---+----+-----------+----------+-----------+----------+-----------+----------+-----------+----------+
| 1 | ID | Firstname | Lastname | Firstname | Lastname | Firstname | Lastname | Firstname | Lastname |
| 2 | A. | Mia       | Smith    | David     | Miller   | Logan     | Jackson  |           |          |
| 3 | B. | Avery     | Johnson  | Elizabeth | Davis    | Oliver    | Thomas   | Joseph    | Lee      |
| 4 | C. | Chloe     | Williams | Sofia     | garcia   |           |          |           |          |
| 5 | D. | Daniel    | Brown    | Ella      | Wilson   | David     | Sanchez  |           |          |
| 6 | E. | Lucas     | Jones    |           |          |           |          |           |          |
+---+----+-----------+----------+-----------+----------+-----------+----------+-----------+----------+

我想要的是使用公式查找大卫的姓氏;这是米勒和桑切斯,他们出现在大卫右侧的牢房里。就像我在A8单元格中输入David一样,Miller出现在A9单元格中,Sanchez出现在A10单元格中。上面给出的信息存在于文件中。

2 个答案:

答案 0 :(得分:1)

使用以下数据:

enter image description here

在标准模块中输入以下用户定义函数:

Public Function GetSurname(rng As Range, fName As String) As Variant
    Dim r As Range, ary(), K As Long
    K = 0
    ReDim ary(1)
    For Each r In rng
        If r.Value = fName Then
            If K = 0 Then
                ary(0) = r.Offset(0, 1).Value
                K = K + 1
            Else
                ReDim Preserve ary(K)
                ary(K) = r.Offset(0, 1).Value
                K = K + 1
            End If
        End If
    Next r
    GetSurname = ary
End Function

选择一个单元格,说 C10 并输入指定的名称。然后从 D10 开始选择相邻的单元格并输入数组公式:

=getsurname(B2:I6,C10)
必须使用 Ctrl + Shift + 输入输入

数组公式,而不仅仅是 Enter key。

enter image description here

之后可以抑制 #N / A

答案 1 :(得分:0)

我认为用VBA解决这个问题可能更容易。我确信有一种方法可以用所有公式做你想要的,但我认为它超出了给定多个重复列的公式的合理范围。

这是我创造的:

Sub test()

first_nm = Worksheets(1).Range("K2").Value
col1_row = Application.WorksheetFunction.CountA(Range("B:B"))
col2_row = Application.WorksheetFunction.CountA(Range("D:D"))
col3_row = Application.WorksheetFunction.CountA(Range("F:F"))
col4_row = Application.WorksheetFunction.CountA(Range("H:H"))

Worksheets(1).Range("L2:L500").ClearContents

y = 2
x = 2
Do While x <= col1_row
    If Cells(y, 2) = first_nm Then
      If Cells(2, 12) = "" Then
         Cells(2, 12) = Cells(y, 3).Value
      Else: Range("L1").End(xlDown).Select
            ActiveCell.Offset(1, 0) = Cells(y, 3).Value
      End If
    End If

    If Cells(y, 2) <> "" Then
       x = x + 1
    End If
y = y + 1
Loop

y = 2
x = 2
Do While x <= col2_row
    If Cells(y, 4) = first_nm Then
      If Cells(2, 12) = "" Then
         Cells(2, 12) = Cells(y, 5).Value
      Else: Range("L1").End(xlDown).Select
            ActiveCell.Offset(1, 0) = Cells(y, 5).Value
      End If
    End If

    If Cells(y, 4) <> "" Then
       x = x + 1
    End If
y = y + 1
Loop


y = 2
x = 2
Do While x <= col3_row
    If Cells(y, 6) = first_nm Then
      If Cells(2, 12) = "" Then
         Cells(2, 12) = Cells(y, 7).Value
      Else: Range("L1").End(xlDown).Select
            ActiveCell.Offset(1, 0) = Cells(y, 7).Value
      End If
    End If

    If Cells(y, 6) <> "" Then
       x = x + 1
    End If
y = y + 1
Loop


y = 2
x = 2
Do While x <= col4_row
    If Cells(y, 8) = first_nm Then
      If Cells(2, 12) = "" Then
         Cells(2, 12) = Cells(y, 9).Value
      Else: Range("L1").End(xlDown).Select
            ActiveCell.Offset(1, 0) = Cells(y, 9).Value
      End If
    End If

    If Cells(y, 8) <> "" Then
       x = x + 1
    End If
y = y + 1
Loop


End Sub

我将是第一个承认通过循环遍历列可以更有效地完成这项工作的人,但我觉得因为只有四列,这样写起来会更简单。

为了实现这一点,您需要做的就是在同一工作表的单元格K2中输入您要查找的名字,在您的情况下为“David”(区分大小写),然后运行宏。它将从单元格L2开始按降序填充姓氏。当然,如果需要,您可以更改输入和输出值的位置。

如果您希望它在不必手动按下按钮或通过开发人员选项卡运行它的情况下运行,您可以在单元格K2中更改调用宏。