VBA to lock Cell after Data Entry but still Allow Sort & Filter ?

Dazzybeeguy

Board Regular
Joined
Jan 6, 2022
Messages
86
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I have tried using this VBA below to lock cells after an entry in a cell, it works fine but it disables sort & filter, is there any way around this ??

Thanks

Dim mRg As Range
Dim mStr As String

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Range("f3:F1100"), Target) Is Nothing Then
Set mRg = Target.Item(1)
mStr = mRg.Value
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRg As Range
On Error Resume Next
Set xRg = Intersect(Range("f3:F1100"), Target)
If xRg Is Nothing Then Exit Sub
Target.Worksheet.Unprotect Password:="Password"
If xRg.Value <> mStr Then xRg.Locked = True
Target.Worksheet.Protect Password:="Password"
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Range("f3:F1100"), Target) Is Nothing Then
Set mRg = Target.Item(1)
mStr = mRg.Value
End If
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
This is the way I would do it. Using the UserInterFaceOnly parameter allows the macro to do anything to the sheet without having to unlock it. Making the sheet locked when sombody activates the sheet locks it all the time. The other parameters I added to the Protect command allows users to sort and autofilter.

VBA Code:
Private Sub Worksheet_Activate()
  Sht.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowSorting:=True, AllowFiltering:=True, UserInterfaceOnly:=True
End Sub


Private Sub Worksheet_Change(ByVal Target As Range)
  Dim xRg As Range
  Dim Sht As Worksheet
  On Error Resume Next
  Set xRg = Intersect(Range("f3:F1100"), Target)
  If xRg Is Nothing Then Exit Sub
  Set Sht = ActiveSheet
  Sht.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowSorting:=True, AllowFiltering:=True, UserInterfaceOnly:=True
  
  If xRg.Value <> mStr Then xRg.Locked = True
  
End Sub
 
Upvote 0
This is the way I would do it. Using the UserInterFaceOnly parameter allows the macro to do anything to the sheet without having to unlock it. Making the sheet locked when sombody activates the sheet locks it all the time. The other parameters I added to the Protect command allows users to sort and autofilter.

VBA Code:
Private Sub Worksheet_Activate()
  Sht.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowSorting:=True, AllowFiltering:=True, UserInterfaceOnly:=True
End Sub


Private Sub Worksheet_Change(ByVal Target As Range)
  Dim xRg As Range
  Dim Sht As Worksheet
  On Error Resume Next
  Set xRg = Intersect(Range("f3:F1100"), Target)
  If xRg Is Nothing Then Exit Sub
  Set Sht = ActiveSheet
  Sht.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowSorting:=True, AllowFiltering:=True, UserInterfaceOnly:=True
 
  If xRg.Value <> mStr Then xRg.Locked = True
 
End Sub
When I used that it didn't lock the cell after data entry, I want it so users cant accidentally or on purpose change a cell that already populated, but I still want to allow them to Sort & Filter
 
Upvote 0
All I did was add the code to lock the sheet differently. You have code that tests if the altered cell falls within Range F3:F1100 and
If xRg.Value <> mStr Then xRg.Locked = True

You should step through the code one line at a time to see which line is preventing the result you wanted.
 
Upvote 0

Forum statistics

Threads
1,221,417
Messages
6,159,789
Members
451,589
Latest member
Harold14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top