I'm trying to get this code to work, I'm stuck on the first hurdle, I'm trying to get the below to lock each cell after a user enters a value, but I can't get it to work.
The only ranges a user would need to fill in once are:
C8 - F12
C23 - F27
C38 - F42
C53 - F57
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
On Error GoTo enditall
Application.EnableEvents = False
If Target.Cells.Column = 2 Then
ActiveSheet.Unprotect Password:="justme"
N = Target.Row
If Excel.Range("B" & N).Value <> "" Then
Excel.Range("B" & N).Locked = True
End If
End If
enditall:
ActiveSheet.Protect Password:="justme"
Application.EnableEvents = True
End Sub
Okay, Well i have an Excel sheet where I enter my daily working hours, IE: 07:30 - 12:30 - 13:30 - 18:30
Management in their infinite wisdom think this is a great idea and want everyone to do it, but are worried the peoples will use this to fiddle the numbers (somehow) so I tried to get a code that locks each cell after editing, I tried to upload the excel sheet to attach to the post but, due to format and size it can't be attached, it would explain things alot easier, sorry.
I know, daft an't it! I think they would then appoint an administrator (not liable to bribes, counts me out!) that would be able to amend the typos/mistakes and reprotect.
Yeah, I think it could be a good idea, and could save alot of paper,
Techincally, the sheet consists of 4 weeks worth of daily times, and is only meant to be filled in by one user each day, the above code was good because it wouldn't lock the cell unless a value has been entered.
Ideally the sheet would be a template for which users would open and then save for themselves each month.
Ok I was busy creating a separate sample for you but it's good that you gave a sample of your own for me to work on...
So now what you want (correct if I am wrong) is
The moment user fills up Cell C8 and D8 then those cells should be locked. Similarly when the user fills up C9 and D9 then those cells should get locked... Am I right in my understanding?
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Something like this? I have just worked for the range C8 to F12
The moment you enter any value in the above range, it will get locked...
Before you paste this code ensure that
1) The locked property of C8 to F12 is False.
2) The sheet is protected using the password "justme"
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim r As Long, c As Long
'~~> Required as it will give error if it encounters a protected cell
On Error Resume Next
r = Target.Previous.Row
c = Target.Column
On Error GoTo 0
Select Case c
Case 3, 4, 5, 6 '<~~ Column C to F
Select Case r
Case 8, 9, 10, 12 '<~~ Row 8 to 12
If Len(Trim(Cells(r, c))) = 0 Then Exit Sub
ActiveSheet.Unprotect Password:="justme"
Cells(r, c).Locked = True
ActiveSheet.Protect Password:="justme"
End Select
End Select
End Sub
Edit:
I am leaving for the day (I am in the office and don't have internet at home for the time being). Will check on this thread tom
Last edited by Siddharth Rout; Jun 11th, 2010 at 11:11 AM.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Thanks again Coolsid! the Code works really well, I'm just putting the final touches to the template before submitting to the 'heavy's'. I'm having slight issue with a userform I've created for it, I'm used to Word and userforms "Selection.Typetext...blah!) but it's different in Excel, I'm trying to get the below to work, but it's not liking it. any help?
Code:
Private Sub cmdOkay_Click()
Dim ws As Worksheet
Set ws = Worksheets("FlexiHours")
ws.Cells(B3, 2).Value = Me.txtname.Value
ws.Cells(E3, 5).Value = Me.txtdepartment.Value
ws.Cells(J3, 10).Value = Me.txtbalance.Value
ws.Cells(B8, 2).Value = Me.txtdate.Value
End Sub
Thanks again, "Range("") worked in the end! your a star, if any royalities come out of this (Government department... doubt it!) 50-50? lol more like 70 - 30! thanks again!