Speed up code

rlink_23

Board Regular
Joined
Oct 30, 2015
Messages
149
Oh my goodness I have been posting a lot!!! But I am so close to getting my project done and these few last details are making my brain hurt lol... So once I insert this code on my Sheet it goes super slow inputting data from userform to sheet1. Is there a way I can modify it to speed up the process? Or a different way to code it? The reason I have it this way is because I am trying to still change the color of the cell on right click or double click... Not sure how to proceed.



Code:
Private Sub Worksheet_Change(ByVal Target As Range)Set Rng = Range("O4:O10003")
If Sheets("sheet1").ProtectContents = True Then Sheets("sheet1").Unprotect Password:="hello"
For Each cell In Rng
 If cell = "" Then
  cell.Locked = False
  Else
  cell.Locked = True
 End If
Next cell
Sheets("sheet1).Protect Password:="hello", UserInterfaceOnly:=True
End Sub


Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Target.Interior.ColorIndex = 3
End Sub


Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Target.Interior.ColorIndex = 0
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Set Rng = Range("O4:O10003")
    If Sheets("sheet1").ProtectContents = True Then Sheets("sheet1").Unprotect Password:="hello"
    On Error Resume Next
    ActiveSheet.Range("O4:O10003").SpecialCells(xlCellTypeBlanks).Locked = False
    On Error GoTo 0
    Sheets("sheet1").Protect Password:="hello", UserInterfaceOnly:=True
End Sub
 
Upvote 0
The Worksheet_Change event is triggered by a change in any cell. That is why the range is being locked after input.
 
Upvote 0
Try adding this line
Code:
If Sheets("sheet1").ProtectContents = True Then Sheets("sheet1").Unprotect Password:="hello"
[COLOR=#0000ff]Rng.Locked = True[/COLOR]
On Error Resume Next
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
You're quite welcome!! I am sure I will be back lol, as I have a little bit left to do on my project!! This is a wonderful resource for a beginner like me :)
 
Upvote 0

Forum statistics

Threads
1,222,753
Messages
6,168,011
Members
452,160
Latest member
Bekerinik

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