Xin code bảo vệ vùng dữ liệu sau khi nhập liệu ở cột B

Liên hệ QC

marcosheath479

Thành viên chính thức
Tham gia
23/2/22
Bài viết
53
Được thích
5
Chào cả nhà,

Nhờ mọi người giúp mình đoạn code để mỗi khi nhập liệu vào cột B thì sẽ bảo về cả vùng (protect range). VD: nếu nhập liệu vào ô B4, bảo vệ vùng dữ liệu A2:Z4, nếu nhập liệu vào ô B7, bảo vệ vùng A2:Z7 (nếu cần thì mật khẩu sẽ là 9798).
Cám ơn mọi người rất nhiều.
 

File đính kèm

  • KHOA VUNG DU LIEU SAU KHI NHAP LIEU.xlsx
    9.9 KB · Đọc: 10
Chào cả nhà,

Nhờ mọi người giúp mình đoạn code để mỗi khi nhập liệu vào cột B thì sẽ bảo về cả vùng (protect range). VD: nếu nhập liệu vào ô B4, bảo vệ vùng dữ liệu A2:Z4, nếu nhập liệu vào ô B7, bảo vệ vùng A2:Z7 (nếu cần thì mật khẩu sẽ là 9798).
Cám ơn mọi người rất nhiều.
Thử code.Trước hết cần bỏ hết Locked của cột B đi.
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Application.EnableEvents = False
      If Not Intersect(Target, Range("B2:B1000")) Is Nothing Then
         If Target.Count = 1 Then
            If Len(Target.Value) > 0 Then
            Sheet1.Unprotect "9798"
                  Target.Locked = True
            Sheets("nhaplieu").Protect "9798"
            End If
         End If
     End If
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
 

File đính kèm

  • KHOA VUNG DU LIEU SAU KHI NHAP LIEU.xlsm
    14.7 KB · Đọc: 10
Upvote 0
Thử code.Trước hết cần bỏ hết Locked của cột B đi.
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Application.EnableEvents = False
      If Not Intersect(Target, Range("B2:B1000")) Is Nothing Then
         If Target.Count = 1 Then
            If Len(Target.Value) > 0 Then
            Sheet1.Unprotect "9798"
                  Target.Locked = True
            Sheets("nhaplieu").Protect "9798"
            End If
         End If
     End If
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
Đầu tiên, mình chọn cả cột B, sau đó vào format cell, bỏ chọn Locked Cell. Sau đó mình thử đoạn code của bạn thì hiện lỗi Compile error: Expected Function or Variable, dòng code này "Private Sub Worksheet_SelectionChange(ByVal Target As Range)" bị tô vàng
 
Upvote 0
Thử code này trong sheet nhâp liệu
Mã:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Unprotect "9798"
    If Target.Column = 2 Then
        If Target <> Empty Then
            Cells.Locked = False
            If Target.Row > 1 Then
                Range("A2:Z" & Target.Row).Locked = True
            Else
                Range("A2:Z" & Cells(Rows.Count, 2).End(3).Row).Locked = True
            End If
        End If
    End If
    Application.EnableEvents = True
    Protect "9798", 1, 1, 1
    Application.ScreenUpdating = True
End Sub
 
Lần chỉnh sửa cuối:
Upvote 1
Thử code này trong sheet nhâp liệu
Mã:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Unprotect "9798"
    If Target.Column = 2 Then
        If Target <> Empty Then
            Cells.Locked = False
            If Target.Row > 1 Then
                Range("A2:Z" & Target.Row).Locked = True
            Else
                Range("A2:Z" & Cells(Rows.Count, 2).End(3).Row).Locked = True
            End If
        End If
    End If
    Application.EnableEvents = True
    Protect "9798", 1, 1, 1
    Application.ScreenUpdating = True
End Sub
Tuyệt vời. Cám ơn bạn rất nhiều :heart:
 
Upvote 0
Hình như khúc này bị dư hay sao ấy nhỉ?
Hihi. Cám ơn anh đã phản hồi. Nếu không có cái khúc đó. Target đang ở dòng đầu. Thì sẽ Unprotect các cái đã khóa trước đó anh ạ. Chẳng biết đúng không. Ngại test nên kệ ạ
 
Upvote 0
Nếu không có cái khúc đó. Target đang ở dòng đầu. Thì sẽ Unprotect các cái đã khóa trước đó anh ạ
Bạn đưa khúc unprotect vào trong target.row > 1, tức thỏa mãn điều kiện mới xử lý là được mà
Mã:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    If Target.Column = 2 Then
        If Target.Row > 1 Then
            Unprotect "9798"
            Cells.Locked = False
            Range("A2:Z" & Target.Row + Target.Rows.Count - 1).Locked = True
            Protect "9798", 1, 1, 1
        End If
    End If
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Bạn đưa khúc unprotect vào trong target.row > 1, tức thỏa mãn điều kiện mới xử lý là được mà
Mã:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    If Target.Column = 2 Then
        If Target.Row > 1 Then
            Unprotect "9798"
            Cells.Locked = False
            Range("A2:Z" & Target.Row + Target.Rows.Count - 1).Locked = True
            Protect "9798", 1, 1, 1
        End If
    End If
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
Giờ em mới để ý. Code này có vẻ gọn gàng hơn ý
 
Upvote 0
Web KT
Back
Top Bottom