重复处理

时间:2016-01-26 16:25:09

标签: excel vba

有没有办法编辑我的代码,以便如果“Sheet2”中的值出现多次,它将跳到下一次出现并复制相应的单元格?我的数据有重复的ID号,但每个都有一个唯一的发票号......

提前致谢

Sub CopyBasedonSheet1()
Dim i As Long
Dim j As Long
Sheet1LastRow = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
Sheet2LastRow = Worksheets("Sheet2").Range("D" & Rows.Count).End(xlUp).Row

   For j = 1 To Sheet1LastRow
        For i = 1 To Sheet2LastRow
            If Worksheets("Sheet1").Cells(j, 1).Value = Worksheets("Sheet2").Cells(i, 9).Value Then
                'Worksheets("Sheet1").Cells(j, 4).Value = Worksheets("Sheet2").Cells(i, 12).Value
                Worksheets("Sheet1").Cells(j, 6).Value = Worksheets("Sheet2").Cells(i, 1).Value
                Worksheets("Sheet1").Cells(j, 7).Value = Worksheets("Sheet2").Cells(i, 2).Value
                Worksheets("Sheet1").Cells(j, 8).Value = Worksheets("Sheet2").Cells(i, 6).Value
                Worksheets("Sheet1").Cells(j, 14).Value = Worksheets("Sheet2").Cells(i, 3).Value

           Else
            End If
    Next i
Next j
End Sub

0 个答案:

没有答案