在这张工作表1中有一个值列表,
我需要根据ID和课程列表获取状态“进行中”和“已完成”
工作表1的状态必须在VBA的帮助下粘贴工作表2中ID和主题匹配的内容,有人可以指导该主题以实现结果吗?
答案 0 :(得分: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