|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Private Sub Worksheet_Change(ByVal Target As Range)
-
- Dim rngs As Range, rng As Range, valRng As Range
- Set rngs = Range("B3:J1000000") '这里改你要监视的区域
- If Intersect(rngs, Target) Is Nothing Then Exit Sub
- Set valRng = Union(ActiveSheet.UsedRange, Intersect(rngs, Target))
- Dim arr
- arr = valRng.Value
- Dim x As Long, y As Long, str As String
- For Each rng In Intersect(rngs, Target)
- If IsEmpty(rng) Then GoTo continue
- x = rng.Row - valRng.Row + 1
- y = rng.Column - valRng.Column + 1
- arr(x, y) = rng.Value
- str = rng.Text
- Dim i As Long, j As Long
- For i = LBound(arr) To UBound(arr)
- For j = LBound(arr, 2) To UBound(arr, 2)
- If i <> x Or j <> y Then
- If arr(i, j) = str Then
- arr(i, j) = Empty
- End If
- End If
- Next j
- Next i
- continue:
- Next rng
- Set rng = Nothing
- For i = LBound(arr) To UBound(arr)
- For j = LBound(arr, 2) To UBound(arr, 2)
- If IsEmpty(arr(i, j)) Then
- x = i
- For y = i + 1 To UBound(arr)
- If Not IsEmpty(arr(y, j)) Then
- arr(x, j) = arr(y, j)
- arr(y, j) = Empty
- x = x + 1
- End If
- Next y
- End If
- Next j
- Next i
- Application.EnableEvents = False
- valRng.Value = arr
- Application.EnableEvents = True
- Erase arr
- Set valRng = Nothing
- Set rngs = Nothing
-
- End Sub
复制代码 |
|