簡體   English   中英

如果單元格包含列中的特定值,請顯示第三個單元格值

[英]If cell contains a specific value from a Column, show a third cell value

這是我想做的事情:

我在A:A中有2000個訂單號,例如:

125787
358946
358961

我有2000個字符串,其中包括B:B中的這些訂單號,例如:

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列中。 我希望這有道理...

或只是一個公式。 在D1中,輸入

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

這是一個按鈕單擊事件,它將執行您想要的操作。 您將需要為adodb記錄集添加參考。 在VBA IDE中,轉到工具下拉菜單-參考。 選擇“ Microsoft ActiveX數據對象2.8庫”。

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

根據張貼者對所需內容的評論。 讓我們看看這是否有效。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM