需要使用id和标头从两个不同的工作表中获取数据/

时间:2018-10-23 10:15:26

标签: excel vba

Sheet1

在这张工作表1中有一个值列表,

我需要根据ID和课程列表获取状态“进行中”和“已完成”

Sheet2

工作表1的状态必须在VBA的帮助下粘贴工作表2中ID和主题匹配的内容,有人可以指导该主题以实现结果吗?

2 个答案:

答案 0 :(得分:1)

修改正确的工作表和范围后,请在Sheet2中尝试此公式,然后向右和向下填充。

=INDEX($C:$C, AGGREGATE(15, 7, ROW(A:A)/(($A$1:$A$5=$E2)*($B$1:$B$5=F$1)), 1))

答案 1 :(得分:1)

只是可能解决方案的一个示例(或者可能会给您前进的方向):

Sub test()
    Dim dic As Object: Set dic = CreateObject("Scripting.Dictionary")
    dic.comparemode = vbTextCompare
    Dim src As Worksheet: Set src = Sheet1
    Dim dest As Worksheet: Set dest = Sheet2
    Dim id As Range, Topic As Range, Status As Range
    Dim searchStr1$, searchStr2$

    With src
        Set id = .Rows(1).Find("ID") 'assume that header in row 1
        Set Topic = .Rows(1).Find("Topic") 'assume that header in row 1
        Set Status = .Rows(1).Find("Status") 'assume that header in row 1
        Set rng = .Range(id.Offset(1), .Cells(.Rows.Count, id.Column).End(xlUp))

        For Each cl In rng
            dic.Add cl.Value2 & .Cells(cl.Row, Topic.Column).Value2, _
            .Cells(cl.Row, Status.Column).Value2
        Next cl
    End With
    With dest
        Set rng = .Range(.[A2], .Cells(.Rows.Count, id.Column).End(xlUp))
        For Each cl In rng
            searchStr1 = cl.Value2 & .[B1].Value2
            searchStr2 = cl.Value2 & .[C1].Value2
            If dic.exists(searchStr1) Then cl.Offset(, 1).Value2 = dic(searchStr1)
            If dic.exists(searchStr2) Then cl.Offset(, 2).Value2 = dic(searchStr2)
        Next cl
    End With
End Sub

测试 enter image description here