Help with Existing VBA Code

Mista_sav

Board Regular
Joined
Aug 18, 2019
Messages
69
Office Version
  1. 2016
Platform
  1. Windows
  2. Web
Hi Team,

I have the following VBA Code that auto sorts and works great! The only issue is it wont work once i protect the sheet. I have one column locked and when i protect the sheet i allow sort but it wont work once i protect the sheet

Is there anything i can add to the code that will work?

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("K:K")) Is Nothing Then
Range("K5").Sort Key1:=Range("K300"), _
Order1:=xlAscending, Header:=xlDisbursed, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
maybe
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next

If Not Intersect(Target, Range("K:K")) Is Nothing Then
   ActiveSheet.Unprotect
   Range("K5").Sort Key1:=Range("K300"), _
   Order1:=xlAscending, Header:=xlDisbursed, _
   OrderCustom:=1, MatchCase:=False, _
   Orientation:=xlTopToBottom
   ActiveSheet.Protect
End If

End Sub
although I would not use Resume Next here. I'd use an error handler instead in case protection was left off. If sheet needs to be password protected you can add that. Google Excel vba unprotect sheet. Please use code tags (vba button on posting toolbar) to maintain code indentation and readability.
 
Upvote 0
Solution
maybe
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next

If Not Intersect(Target, Range("K:K")) Is Nothing Then
   ActiveSheet.Unprotect
   Range("K5").Sort Key1:=Range("K300"), _
   Order1:=xlAscending, Header:=xlDisbursed, _
   OrderCustom:=1, MatchCase:=False, _
   Orientation:=xlTopToBottom
   ActiveSheet.Protect
End If

End Sub
although I would not use Resume Next here. I'd use an error handler instead in case protection was left off. If sheet needs to be password protected you can add that. Google Excel vba unprotect sheet. Please use code tags (vba button on posting toolbar) to maintain code indentation and readability.
Perfect thank you so much mate. Will take that on board too. Appreciate the advice
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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