如何将specfic员工的数据复制到其他工作表匹配的员工ID

时间:2018-03-26 13:11:05

标签: vba copy

我有1000名员工的数据,但我只需要400人,400个电话号码到另一张表。我在另一张表中有所有员工ID。我尝试了vlookup,但它没有用。

sub trandsfer ()
Dim a As Integer
Dim b As Integer

for a=2 To 1000
for b=2 To 400

if Worksheets(1).Range("A")=Worksheets(2).Range("A") Then
worksheets(1).Range("F").Copy
Worksheets(2).Activate
Worksheets(2).Range("C").select
Select.paste
worksheets(1).activate
End if
Next
Next
End Sub

1 个答案:

答案 0 :(得分:0)

试试这个:

  Sub trandsfer()

            'Go through the sheets(2) Ids
            For i = 2 To 400

                idToSearch = Worksheets(2).Cells(i, 1).Value 'put the id in a variable
                'search for the id in sheet(1)
                Set findID = Worksheets(1).Columns("A").Find(what:=idToSearch, LookIn:=xlValues, Lookat:=xlWhole)

                If findID Is Nothing Then
                    Debug.Print "Not Found !"
                'If found
                Else
                    firstAddress = findID.Address
                    foundRow = findID.Row
                    'copy the value of column F of sheet(1) and paste it in column C of sheet(2)
                    Worksheets(1).Cells(foundRow, 6).Copy Destination:=Worksheets(2).Cells(i, 3)

                End If

            Next i

   End Sub