code to lock a range of cells after completing

Cablek

Board Regular
Joined
Nov 22, 2017
Messages
51
I have a code to automatically enter the date if any data is entered into cells B14 to B100

Private Sub Worksheet_change(ByVal Target As Excel.Range)
If (Target.Count = 1) And _
(Not Intersect(Target, [B14:B10000]) Is Nothing) Then _
Target.Offset(0, -1) = Date
End Sub

but now I want the user to enter date into B14, C14 and E14 THEN once completed I want to LOCK those cells
then user can come back later and enter B15,C15 and E15 right down to B100, C100 and E100

A B C&D E

Date
Qty builtPO#built by

<colgroup><col span="3"><col span="2"></colgroup><tbody>
</tbody>
Does this make any sense?
 
Hey JB I am a bit confused as to how and where to insert the Sub UnlockCells() code goes...

With only the Private Sub Worksheet_change code it works but again everything is locked once data in B14 is entered
 
Upvote 0

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
JB would it be easier if the cell only locked when the user got to the end of the row???

A B C D E
"Date" "Shipped qty" "B/O (Qty)" "shipper name" "notes"....

I could set the range from B14 to D100 allowing notes to remain unlocked and when the Shipper puts in his name it locks that Ax to Dx (the row he is on.... does this simplify things?
 
Upvote 0
The unprotect suvb can go anywhere.

Replace your current code. I mean clear it completely. Replace it with the larger code from my previous post.

create a module and paste the Unprotect sub into it.

Put the Keyboard cursor in the sub and press F8. press it another 3 times and it should run through the sub.

Now your other sub should work. IT sounds like you still had the '....Cells.Unprotect' line in the main sub.
 
Upvote 0
JB it works like a charm for locking Ax and Bx but how do I get it to lock Cx, Dx also???

I had to add colums so now have

A B C D E F G
auto date Qty ordered Qty shipped B/O (auto) W/O # Shipper name Notes

I would like the row to lock from A-F when Shipper enteres his name...
 
Upvote 0
Evening,

Glad it works. Adding in more actions for other columns is easy enough.

I don't want to just give you the answer though, as that won't help you learn VBA.

Lets start with your original code:

Code:
Private Sub Worksheet_change(ByVal Target As Excel.Range)
	If (Target.Count = 1) And (Not Intersect(Target, [B14:B10000]) Is Nothing) Then
		Sheets("Shipping").Unprotect
		Target.Offset(0, -1) = Date
		Target.Offset(0, -1).Resize(1, 2).Locked = True
		Sheets("Shipping").Protect
	End If
End Sub

So line 2 is the if statement. If that results in FALSE, then it will skip straight to 'End If', ignoring any code in between. It will then action any code after 'End If', which in this case is just 'End Sub'.

So if we add in some space beneath 'End If' we can write more code for the same Sub.

In your current If statement, you are saying Target.count must be 1. This is just saying that the user must have edited only one cell. (Copying multiple cells and pasting them into Column B will edit multiple cells at once, so will be ignored). This code is normally in place because commands on a single cell are often not designed for a range of cells, so will fail.

We are also saying 'NOT Intersect(Target, [B14:B10000]) is Nothing'. The Intersect checks that the 'Target' cell in in the range B14:B10000. You then have an 'Is Nothing', and the 'Not', which makes it all very confusing as it is a double negative.

You may find this easier to understand:
Code:
If Target.Column = 2 AND Target.Row >=14 And Target.Row <=10000           'This says exactly the same thing. (Target.column is numeric, but Column A=1, B=2, etc)

So now you want to add in another scenario for column G (I think?), so we need a whole new If statement, in the blank space you have created beneath the original one.

Column G is Column number 7.

So your next IF statement is:

Code:
 If Target.Count = 1 AND Target.Column = 7 and Target.Row >=14 and Target.Row <=10000
'After this line hit Enter a few times and then put 'End If'.

This gives you a new If statement to work with, which is currently empty. so now we need to fill it.

Remember you cannot modify the sheet, (either manually or with code) if the sheet is protected, so the first line within the If statement needs to unprotect the sheet, which you now know how to do right? :)

There's no auto date or anything to enter for this scenario, so we just need to lock the cells, and re-protect the sheet.

A & B should be locked already, but it will do no harm to re-lock them, so we are going to lock Columns A - F. You now know that '.Locked = True' will lock the cells specified to the left of '.Locked', so let's specify those cells!

Target is always the cell you have edited. Let's say G14.
So to get to A we need to offset - Again, A = 1, G = 7, so to get back to A we need to offset by -6. Remember with offset the first number is always Rows, the second is Columns. We of course still want the same row, so zero offset there.

If you have written your Target.Offset correctly, then it will now be referring to cell A14.

So finally we need to resize that reference to produce A14:F14.

Resize, like offset has two parameters, and again like offset, they are in the order Rows,Columns. We are still just working with the single row of data, so you still want your range to be 1 row high. However, you need it to be 6 columns wide. You can copy the 'Locked = True' line from the first if statement and adjust it accordingly if that makes things easier.

Hopefully now you have completed your line of code to lock the row.

Finally, before the 'End If' you will want to re-protect the sheet, which again, you should now know how to do.

You should now be ready to go.

If it does not behave as expected, then put a breakpoint on line 1 (Private Sub...) and then enter a person's name in G. You should then be in the code, which you can step through with F8, to try to diagnose the issue.

Good luck.
Make sure you let me know how you get on ;)

Cheers
JB
 
Upvote 0
Hey JB... here is the currrent code as I understand (not hopefull) but now nothing is working.

How do I do the breakpoint?

Private Sub Worksheet_change(ByVal Target As Excel.Range)
If Target.Count = 1 And Target.Column = 7 And Target.Row >= 14 And Target.Row <= 10000 Then
Sheets("Shipping").Unprotect
Target.Offset(0, -1) = Date
Target.Offset(0, -1).Resize(1, 2).Locked = True
Sheets("Shipping").Protect
End If
If Target.Count = 1 And Target.Column = 7 And Target.Row >= 14 And Target.Row <= 10000 Then
Sheets("Shipping").Unprotect
Target.Offset(0, -6).Resize(1, 6).Locked = True
Sheets("Shipping").Protect
End If

End Sub
 
Upvote 0
Hi there,

You're very close, but you changed the first 'If' line when you didn't really need to.

It's fine to change it, but you used the logic for column G rather than B.

The first if statement is intended for use when a change is made to column B... column B is also known as column 2, not 7... You see the mistake?

Other than that, it is perfect! Good work!
 
Upvote 0
Hey JB,
it worked..... To be honest I really wanted you to give me the answer but now see why you didn't.... I don't know if I will be using Excel much but now have a much better understansing of probably .00001% of VBA. lol

I once again want to show you my appretiation... if ever you are in Montreal Canada let me know.
 
Upvote 0
Haha, Montreal, wow that's far! I went to France once. Twice actually, but other than that, 30+ years in the UK.

And you may be right on the 0.00001% in terms of code knowledge.... BUT, knowing how the code works counts for at least a third (in my opinion). Once you have a decent understanding of the structure of the code, you can start to use the 'Record Macro' function to extract certain lines that you need. It would record both protect/unprotect and locking of cells for example.
 
Upvote 0
Hey JB I guess I was too excited on Friday and didn't test it through... I have been playing around with the off sets ect and now it seems to be really flacky...
I'm sure it's logical but for example
when I enter the Qty ordered in column B row 14... it locks A, B, C, D, E and F then brings me to G... AND it also locks Row 15 A-F and row 16 A and B only.
FYI I studies basic back in the late 80's and some Visual C in the mid 90's but that might not be helping me !!!
 
Upvote 0

Forum statistics

Threads
1,215,097
Messages
6,123,077
Members
449,094
Latest member
mystic19

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