【问题标题】:If cell contains a specific value from a Column, show a third cell value如果单元格包含列中的特定值,则显示第三个单元格值
【发布时间】:2015-07-13 13:40:45
【问题描述】:

这是我想要做的:

我在 A:A 中有 2000 个订单号,示例:

125787
358946
358961

我在 B:B 中有 2000 个包含这些订单号的字符串,示例:

12542-MARLBORO-125787
19009-BRYN ATHYN-358946
21037-EDGEWATER-358961

我在 C:C 中有 3000 个与 B 列中列出的城市相关联的人名,示例:

Frank Smith - MARLBORO
John Park - BRYN ATHYN
Kevin Decker - EDGEWATER

我想在 B:B 中匹配/查找来自 A:A 的订单号,并返回与该城市相关联的人的姓名(在 C:C 中)并将该姓名放入新列 D:D。我希望这是有道理的......

【问题讨论】:

  • 所以你想要 D 中的名称作为 A 中的订单号?
  • 是的!基于 A:A 和 B:B 的匹配......
  • 你试过什么?它必须是VBA吗?您应该可以使用FindA->B->C 获取。您也可以使用公式来执行此操作,同样使用 FIND 和数组公式。真正的问题是……从 City 到 Person 的映射是独一无二的吗?如果没有,那将是难以解决的部分。
  • 拜伦-我试过了:
  • =INDEX(C:C,MATCH(A1,B:B,0)) 但这不起作用,例如:正在尝试匹配订单:125787 与 12542-MARLBORO-125787,是实际上并未搜索 12542-MARLBORO-125787 上是否存在 125787

标签: excel vba


【解决方案1】:

或者只是一个公式。在 D1 中,输入

=IF(A1<>"",INDEX(C:C,MATCH(CONCATENATE("*",A1),B:B,0)),"")

【讨论】:

  • 一点解释和几个换行符会改善这个答案。
  • 您可以在此处添加它们只是为了提供帮助。如果您从其他地方粘贴它,Excel 允许返回字符。您也可以按 ALT+ENTER,IIRC。
  • Byron 该代码 (=LEFT) 无效。我得到“#VALUE!”在 D:D
  • 我提供的公式中没有 D:D。除非您将其拖到另一列。如果您需要在另一列中,请从上面粘贴公式,然后您可以向下拖动到所有行。
  • 我将公式粘贴到 D1 并向下拖动到所有行
【解决方案2】:

这是一个按钮单击事件,它将执行您正在寻找的操作。您将需要为 adodb 记录集添加引用。在 VBA IDE 中,转到工具下拉菜单 - 参考。选择“Microsoft ActiveX Data Objects 2.8 Library”。

Private Sub CommandButton5_Click()
Dim rs As New ADODB.Recordset
Dim ws As Excel.Worksheet
Dim lRow As Long
Dim strCity As String
Dim strName As String
Dim iIndex As Integer

    Set ws = Application.ActiveSheet

    'Add fields to your recordset for storing data.  You can store sums here.
    With rs
        .Fields.Append "Row", adChar, 20
        .Fields.Append "ColumnB", adChar, 70
        .Fields.Append "ColumnC", adChar, 70
        .Open
    End With

    lRow = 1

    'Loop through and record what is in the columns we want to look at
    Do While lRow <= ws.UsedRange.Rows.Count

        rs.AddNew
        rs.Fields("Row").Value = lRow
        rs.Fields("ColumnB").Value = ws.Range("B" & lRow).Value
        rs.Fields("ColumnC").Value = ws.Range("C" & lRow).Value
        rs.Update

        lRow = lRow + 1
        ws.Range("A" & lRow).Activate
    Loop

    If rs.EOF = False Then
        rs.MoveFirst
    End If

    'Now go through and check the values of the second column against what we recorded from the first
    lRow = 1
    Do While lRow <= ws.UsedRange.Rows.Count

        'Find the record with this order number.
        rs.Filter = ""
        rs.Filter = "ColumnB Like '%" & ws.Range("A" & lRow).Value & "%'"
        If rs.RecordCount > 0 Then
            strCity = rs.Fields("ColumnC").Value
            iIndex = 0
            iIndex = InStr(strCity, "-")
            If iIndex <> 0 Then
                strCity = Right(strCity, Len(strCity) - iIndex)
            End If
            iIndex = 0
            iIndex = InStr(strCity, "-")
            If iIndex <> 0 Then
                strCity = Left(strCity, iIndex)
            End If

            'Now find the record with that name
            rs.Filter = ""
            rs.Filter = "ColumnC Like '%" & Trim(strCity) & "%'"
            If rs.RecordCount > 0 Then
                strName = ws.Range("C" & lRow).Value
                iIndex = 0
                iIndex = InStr(strName, "-")
                if iIndex > 0 then
                    ws.Range("D" & lRow).Value = Trim(Left(strName, iIndex - 1))
                else
                    ws.Range("D" & lRow).Value = "not found"
                end if
            End If
        End If

        lRow = lRow + 1
        ws.Range("A" & lRow).Activate
    Loop
End Sub

根据海报 cmet 的要求。让我们看看这是否有效。

Private Sub CommandButton5_Click()
Dim rs As New ADODB.Recordset
Dim ws As Excel.Worksheet
Dim lRow As Long
Dim strCity As String
Dim strName As String
Dim iIndex As Integer

Set ws = Application.ActiveSheet

'Add fields to your recordset for storing data.  You can store sums here.
With rs
    .Fields.Append "Row", adChar, 20
    .Fields.Append "ColumnB", adChar, 70
    .Fields.Append "ColumnC", adChar, 70
    .Open
End With

lRow = 1

'Loop through and record what is in the columns we want to look at
Do While lRow <= ws.UsedRange.Rows.Count

    rs.AddNew
    rs.Fields("Row").Value = lRow
    rs.Fields("ColumnB").Value = ws.Range("B" & lRow).Value
    rs.Fields("ColumnC").Value = ws.Range("C" & lRow).Value
    rs.Update

    lRow = lRow + 1
    ws.Range("A" & lRow).Activate
Loop

If rs.EOF = False Then
    rs.MoveFirst
End If

'Now go through and check the values of the second column against what we recorded from the first
lRow = 1
Do While lRow <= ws.UsedRange.Rows.Count

    'Find the record with this order number.
    rs.Filter = ""
    rs.Filter = "ColumnB Like '%" & ws.Range("A" & lRow).Value & "%'"
    If rs.RecordCount > 0 Then
        ws.Range("D" & lRow).Value = rs.Fields("ColumnC").Value
    End If

    lRow = lRow + 1
    ws.Range("A" & lRow).Activate
Loop
End Sub

【讨论】:

  • 行:ws.Range("D" & lRow).Value = Trim(Left(strName, iIndex - 1))
  • 运行时错误 5:无效的过程调用或参数。
  • 失败时 iIndex 的值是多少?我对该部分进行了更改,以说明未找到该城市的记录。
  • 我进入了代码,但是我不确定如何找到您想要的值...抱歉!您是否使用我上面指定的值创建了一个包含 3 行和列的工作簿/工作表并测试了您的代码?你得到同样的结果吗?
  • 我确实创建了它,但没有收到错误消息。您是否尝试过使用上面的修复程序?如果 iIndex > 0 那么和接下来的 5 行。
猜你喜欢
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多