Nhập dữ liệu và tự khóa (1 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

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Ế
Yêu cầu mình đã viết trong file, xin chân thành cảm ơn
 

File đính kèm

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é
PHP:
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
 
Lần chỉnh sửa cuối:
Upvote 0
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é
PHP:
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
[/QUOTE
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
 
Upvote 0
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
 
Upvote 0
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
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
 
Upvote 0
Cùng một bảng tính mà thay code của bác Ba tê vào thì chạy OK nhưng của bác quanghai vẫn bị báo lỗi 1004 khi mình thay đổi code thanh H7:AV650 , hiz mù tịt VBA nên cũng k bít vì sao luôn. Rất Cảm ơn và chúc 2 bác ngủ ngon nha ^^
 
Upvote 0
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 ^^
 
Upvote 0
Chèn thêm code sau dưới mỗi câu lệnh: ActiveSheet.Protect "123"

ThisWorkBook.Save
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"
ThisWorkBook.Save
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"
ThisWorkBook.Save
End If
End If
End If
End Sub

Như thế này đúng chưa bạn

 
Upvote 0
Minh nghĩ chỉ cần save một lần trước câu End Sub là đủ rồi
 
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom