|
大家好,做了一个表格事件的代码,经常出错,速度也慢,各位老师帮忙看看有什么问题?谢谢!
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, Jrng As Range, s As Range, r As Integer
On Error Resume Next
Application.EnableEvents = False
If Target.Row >= 3 Then
r = Target.Row
Range("a" & r, "s" & r).Borders.LineStyle = xlContinuous
Set rng = Sheet1.Range("c:c")
If Not Application.Intersect(Target, rng) Is Nothing Then '被改变单元格与a列存在交集
'取出交集区域
Set Jrng = Application.Intersect(Target, rng)
For Each s In Jrng
With Sheet3.Range("a1:a" & Sheet3.Range("a65536").End(xlUp).Row)
Set c = .Find(s.Value, LookIn:=xlValues)
If s.Value <> "" Then
s.Offset(0, -2) = Date
s.Offset(0, 3) = c.Offset(0, 2).Value
s.Offset(0, 5) = c.Offset(0, 3).Value
s.Offset(0, 15) = c.Offset(0, 4).Value
Else
s.Offset(0, -2) = ""
s.Offset(0, 3) = ""
s.Offset(0, 5) = ""
s.Offset(0, 15) = ""
End If
End With
Next
End If
End If
Application.EnableEvents = True
Set rng = Sheet1.Range("e:e")
If Not Application.Intersect(Target, rng) Is Nothing Then '被改变单元格与a列存在交集
'取出交集区域
Set Jrng = Application.Intersect(Target, rng)
For Each s In Jrng
If s.Value <> "" Then
s.Offset(0, 2) = s.Offset(0, 0) * s.Offset(0, 1)
Else
s.Offset(0, 2) = ""
End If
Next
End If
Set rng = Sheet1.Range("k:k")
If Not Application.Intersect(Target, rng) Is Nothing Then '被改变单元格与a列存在交集
'取出交集区域
Set Jrng = Application.Intersect(Target, rng)
For Each s In Jrng
With Sheet2.Range("a1:a" & Sheet2.Range("a65536").End(xlUp).Row)
Set c = .Find(s.Value, LookIn:=xlValues)
If s.Value <> "" Then
s.Offset(0, -1) = c.Offset(0, 11).Value
s.Offset(0, 1) = c.Offset(0, 1).Value '公司代码
s.Offset(0, 2) = c.Offset(0, 4).Value '目地库位
If s.Offset(0, 7) = "乙类" Or s.Offset(0, 7) = "甲类" Then
s.Offset(0, 2) = c.Offset(0, 5).Value
s.Offset(0, 3) = c.Offset(0, 8).Value
s.Offset(0, 8) = c.Offset(0, 9).Value
Else
s.Offset(0, 2) = c.Offset(0, 4).Value
s.Offset(0, 3) = c.Offset(0, 7).Value
s.Offset(0, 8) = c.Offset(0, 10).Value
End If
s.Offset(0, 5) = c.Offset(0, 3).Value '时限
s.Offset(0, 6) = Date + s.Offset(0, 5) '到货日期
s.Offset(0, 8) = s.Offset(0, 1) & "-" & s.Offset(0, 2) & "-" & s.Offset(0, 3).Value
Else
s.Offset(0, -1) = ""
s.Offset(0, 1) = ""
s.Offset(0, 2) = ""
s.Offset(0, 3) = ""
s.Offset(0, 5) = ""
s.Offset(0, 6) = ""
s.Offset(0, 8) = ""
End If
End With
Next
End If
Application.EnableEvents = True
End Sub
|
|