如何将一个单元格值映射到具有IF条件的另一个单元格

时间:2018-12-23 21:56:04

标签: excel conditional-statements

假设我在excel中有一个这样的表-

+-------------+----------+------------+
| Google Name | D11 Name |   Entry    |
+-------------+----------+------------+
| REN         | MLR      |            |
| THU         | SDT      |            |
| SIX         | SDS      |            |
| HOH         | HBH      |            |
| STR         | ADS      |            |
| HEA         | BRH      |            |
| PRS         | PS       |            |
| STA         | MLS      |            |
+-------------+----------+------------+

现在,我将在Entry栏中输入内容。如果该值与Google Name值匹配,则应更改为相应的D11 Name值。这意味着-

+-------------+----------+------------+
| Google Name | D11 Name |   Entry    |
+-------------+----------+------------+
| REN         | MLR      | SIX -> SDS |
| THU         | SDT      |            |
| SIX         | SDS      |            |
| HOH         | HBH      |            |
| STR         | ADS      |            |
| HEA         | BRH      |            |
| PRS         | PS       |            |
| STA         | MLS      |            |
+-------------+----------+------------+

如果我输入SIX,则最后输入将是SDS->仅用于解释目的。

1 个答案:

答案 0 :(得分:1)

如果要将类型化的输入更改为从查找中检索到的值,则需要VBA和Worksheet_Change事件驱动的子过程。

打开工作表的专用代码表(右键单击工作表名称标签和查看代码),然后粘贴此代码。

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("C:C")) Is Nothing Then
        On Error GoTo sub_exit
        Application.EnableEvents = False
        Dim t As Range, m As Variant
        For Each t In Intersect(Target, Range("C:C"))
            m = Application.Match(t.Value2, Range("A:A"), 0)
            If Not IsError(m) Then
                t = Cells(m, "B").Value2
            End If
        Next t

    End If

sub_exit:
    Application.EnableEvents = True
End Sub

enter image description here