创建一个宏以在列B中查找值,如果有任何匹配的大小写,则复制相应的单元格值

时间:2016-05-13 13:17:34

标签: excel-vba vba excel

ReporteeID  Manager ID
R1001   M1001
R1002   M1002
R1003   M1003
R1004   M1001
R1005   M1002
R1006   M1003
R1007   M1001
R1008   M1002
R1009   M1003
R1010   M1001

Out Put应该如下所示

M1001   R1001   R1004   R1007   R1010
M1002   R1002   R1005   R1008   
M1003   R1003   R1009       

我的请求是创建一个宏,它可以查找所有单元格的ManagerID(列)循环,并且它匹配左侧列(ReporteeID)单元格值应该复制并粘贴到另一个工作表中的相应ManagerID(M1001)通过xltoright右侧行

1 个答案:

答案 0 :(得分:0)

这将为您提供一个开始:

Sub dural()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim N2 As Long, i As Long, K As Long
    Dim v As String, N1 As Long

    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")
    sh1.Range("B:B").Copy sh2.Range("A1")
    sh2.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
    N2 = sh2.Cells(Rows.Count, "A").End(xlUp).Row
    N1 = sh1.Cells(Rows.Count, "A").End(xlUp).Row

    For i = 2 To N2
        K = 2
        v = sh2.Cells(i, 1).Value
        For j = 2 To N1
            If v = sh1.Cells(j, 2).Value Then
                sh2.Cells(i, K).Value = sh1.Cells(j, 1).Value
                K = K + 1
            End If
        Next j
    Next i
End Sub