keke355992
Thành viên thường trực




- Tham gia
- 19/1/08
- Bài viết
- 310
- Được thích
- 20
- Nghề nghiệp
- KẾ TOÁN THUẾ, TƯ VẪN THUẾ
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell
If Not Intersect(Target, [E7:F700]) Is Nothing Then
If Target.Rows.Count = 1 And Target.Columns.Count = 1 Then
If Target.Value = "x" Then
ActiveSheet.Unprotect "123"
Target.Offset(, -2).Value = Now()
Target.Locked = True
Target.Offset(, -2).Locked = True
ActiveSheet.Protect "123", DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True
ActiveSheet.EnableSelection = xlUnlockedCells
End If
End If
End If
If Not Intersect(Target, [H7:AV12]) Is Nothing Then
ActiveSheet.Unprotect "123"
For Each cell In [H7:AV12]
If cell.Value <> "" Then cell.Locked = True
Next cell
ActiveSheet.Protect "123"
End If
End Sub
Bạn thay code này vào nghen, chỉ khóa tới ô AV12 , nếu muốn thì thay AV12 theo thực tế nhé
[/QUOTEPHP:Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) Dim cell If Not Intersect(Target, [E7:F700]) Is Nothing Then If Target.Rows.Count = 1 And Target.Columns.Count = 1 Then If Target.Value = "x" Then ActiveSheet.Unprotect "123" Target.Offset(, -2).Value = Now() Target.Locked = True Target.Offset(, -2).Locked = True ActiveSheet.Protect "123", DrawingObjects:=True, Contents:=True, Scenarios:=True _ , AllowFormattingCells:=True, AllowFormattingColumns:=True, _ AllowFormattingRows:=True ActiveSheet.EnableSelection = xlUnlockedCells End If End If End If If Not Intersect(Target, [H7:AV12]) Is Nothing Then ActiveSheet.Unprotect "123" For Each cell In [H7:AV12] If cell.Value <> "" Then cell.Locked = True Next cell ActiveSheet.Protect "123" End If End Sub
Bạn ơi dữ liệu của mình đến dòng 650, mình thay thành [H7:AV650] thì báo lỗi "run time error 1004" bạn ah, bạn sửa giùm mình với
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell
If Not Intersect(Target, [E7:F650]) Is Nothing Then
If Target.Rows.Count = 1 And Target.Columns.Count = 1 Then
If Target.Value = "x" Then
ActiveSheet.Unprotect "123"
Target.Offset(, -2).Value = Now()
Target.Locked = True
Target.Offset(, -2).Locked = True
ActiveSheet.Protect "123", DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True
ActiveSheet.EnableSelection = xlUnlockedCells
End If
End If
End If
If Not Intersect(Target, [H7:AV650]) Is Nothing Then
ActiveSheet.Unprotect "123"
For Each cell In [H7:AV650]
If cell.Value <> "" Then cell.Locked = True
Next cell
ActiveSheet.Protect "123"
End If
End Sub
Mỗi lần nhập liệu vào vùng H7:AV650 lại duyệt hết từng Cell sao phí vậy.Mình đã thay code và kiểm tra thấy code không có lỗi, không biết là lỗi nằm chỗ nào. Nếu vẫn chưa ổ thì up file bị lỗi lên nha.
PHP:Private Sub Worksheet_Change(ByVal Target As Range) Dim cell If Not Intersect(Target, [E7:F650]) Is Nothing Then If Target.Rows.Count = 1 And Target.Columns.Count = 1 Then If Target.Value = "x" Then ActiveSheet.Unprotect "123" Target.Offset(, -2).Value = Now() Target.Locked = True Target.Offset(, -2).Locked = True ActiveSheet.Protect "123", DrawingObjects:=True, Contents:=True, Scenarios:=True _ , AllowFormattingCells:=True, AllowFormattingColumns:=True, _ AllowFormattingRows:=True ActiveSheet.EnableSelection = xlUnlockedCells End If End If End If If Not Intersect(Target, [H7:AV650]) Is Nothing Then ActiveSheet.Unprotect "123" For Each cell In [H7:AV650] If cell.Value <> "" Then cell.Locked = True Next cell ActiveSheet.Protect "123" End If End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, [E7:F700]) Is Nothing Then
If Target.Rows.Count = 1 And Target.Columns.Count = 1 Then
If Target.Value = "x" Then
ActiveSheet.Unprotect "123"
Target.Offset(, -2).Value = Now()
Target.Locked = True
Target.Offset(, -2).Locked = True
ActiveSheet.Protect "123"
End If
End If
ElseIf Not Intersect(Target, [H7:AV700]) Is Nothing Then
If Target.Rows.Count = 1 And Target.Columns.Count = 1 Then
If Target.Value <> "" Then
ActiveSheet.Unprotect "123"
Target.Locked = True
ActiveSheet.Protect "123"
End If
End If
End If
End Sub
Nhờ bác giúp mình chèn đoan code tự động save bảng tỉnh sau khi thực hiện một trong những thao tác này với ^^Mỗi lần nhập liệu vào vùng H7:AV650 lại duyệt hết từng Cell sao phí vậy.
Thử như vầy xem:
PHP:Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, [E7:F700]) Is Nothing Then If Target.Rows.Count = 1 And Target.Columns.Count = 1 Then If Target.Value = "x" Then ActiveSheet.Unprotect "123" Target.Offset(, -2).Value = Now() Target.Locked = True Target.Offset(, -2).Locked = True ActiveSheet.Protect "123" End If End If ElseIf Not Intersect(Target, [H7:AV700]) Is Nothing Then If Target.Rows.Count = 1 And Target.Columns.Count = 1 Then If Target.Value <> "" Then ActiveSheet.Unprotect "123" Target.Locked = True ActiveSheet.Protect "123" End If End If End If End Sub
Nhờ bác giúp mình chèn đoan code tự động save bảng tỉnh sau khi thực hiện một trong những thao tác này với ^^
Chèn thêm code sau dưới mỗi câu lệnh: ActiveSheet.Protect "123"
ThisWorkBook.Save