excel 根据多个条件将值拉入一个单元格

oaxa6hgo  于 2023-10-21  发布在  其他
关注(0)|答案(1)|浏览(127)

尝试运行一个返回不同的单元格值从一个工作表到另一个工作表单单元格基于多个条件匹配(smth接近textjoin/filter函数)
这是我想运行的代码

Sub FindValues()

Dim lookUpSheet As Worksheet, updateSheet As Worksheet
Dim valueToSearch As String
Dim i As Long

Set lookUpSheet = Worksheets("Test")
Set updateSheet = Worksheets("result")

Worksheets("result").Cells(22, 5).Value = result
    
For i = 2 To lastRow
        If lookUpSheet.Cells(i, 5).Value & lookUpSheet.Cells(i, 2).Value = updateSheet.Range("C22").Value & updateSheet.Range("E21").Value Then
            Worksheets("result").Cells(22, 5).Value = lookUpSheet.Cells(i, 1).Value & vbNewLine & lookUpSheet.Cells(i, 3).Value & vbNewLine & lookUpSheet.Cells(i, 4).Value & vbNewLine
        End If
    Next i
    
   
End Sub
blpfk2vs

blpfk2vs1#

试试看吧

Option Explicit
Sub FindValues()
    Dim lookUpSheet As Worksheet, updateSheet As Worksheet
    Dim i As Long, lastRow as Long
    Dim sResult As String, sKey As String
    Set lookUpSheet = Worksheets("Test")
    Set updateSheet = Worksheets("Result")
    sKey = updateSheet.Range("C22").Value & updateSheet.Range("E21").Value
    with lookUpSheet
        lastRow = .Cells(.Rows.Count, 2).End(xlUp).Row ' modify column # as needed
        For i = 2 To lastRow
            If .Cells(i, 5).Value & .Cells(i, 2).Value = sKey Then
                sResult = sResult & vbNewLine &  .Cells(i, 1).Value & vbNewLine _
                    & .Cells(i, 3).Value & vbNewLine & .Cells(i, 4).Value
            End If
        Next i
    end with
    updateSheet.Cells(22, 5).Value = mid(sResult,2)
End Sub

相关问题