|
本帖最后由 gxp1986 于 2019-10-4 17:23 编辑
写在sheet2里面,注意,如果表1里有多个相同的值,只会返回第一个结果
- Private Sub Worksheet_Change(ByVal Target As Range)
- Dim tr As Long, tl As Long, i As Long, k As Long, n As Long, ss As Variant
- n = Target.Count
- If n > 1000 Then Exit Sub '当一次性操作操作1000个单元格时,不触发本程序
- If Target.Cells(1).Column = 12 Then
- For i = 1 To n
- tr = Target.Cells(i).Row
- tl = Target.Cells(i).Column
- If tl = 12 Then
- ss = Target.Cells(i)
- If ss = "" Then
- Rows(tr).ClearContents '当L列数据为空时,清空本行
- Else
- For k = 1 To 10000
- With Sheet1
- If .Cells(k, "L") = ss Then
- Application.EnableEvents = False
- Rows(tr) = .Rows(k).Value '当数值相等时,返回匹配到的数据,并退出程序
- .Rows(k).Delete '删除表1里的数据
- Application.EnableEvents = True
- Exit Sub
- End If
- End With
- Next
- Application.EnableEvents = False
- Rows(tr).ClearContents '当匹配不到数据时,清空本行,并还原数值
- Target.Cells(i) = ss
- Application.EnableEvents = True
- End If
- End If
- Next
- End If
- End Sub
复制代码
|
|