VBA Dynamic Row Range

DavidG007

Board Regular
Joined
Jul 6, 2018
Messages
79
Office Version
  1. 365
Platform
  1. Windows
Hi, Can anyone please help?

I need to amend the code so that the Row Range selection is dynamic, as per below.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

ActiveSheet.Unprotect "Password"

For Each cell In Range("B4:B18")

If cell.Value = "Expansion" Then

'NEED TO AMEND THIS LINE SO THAT THE ROW RANGE SELECTION IS DYNAMIC BASED ON THE ROW NUMBER.  RANGE SAY D:F, ROW = VARIABLE.
cell.EntireRow.Locked = True

End If

Next cell

ActiveSheet.Protect "Password"

End Sub


Big thanks
 
just checking;
* code is copied to Module1
* worksheet - all cells locked apart from column B
* worksheet protected.

Then, User makes selections?
 
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.
It is not for me. In my sheet below if I select B7 then I can enter/edit values in C7:E7 but not F7:H7.
If I select B9 I an enter in F9:H9 but not C9:E9
Is that what should be happening? If not please explain what should be happening when I select each of those two cells.

22 02 22.xlsm
BCDEFGH
1
2
3
4
5
6
7Expansion
8
9SIB
selection change lock
for some reason it doesn't work for me!?
 
Upvote 0
* code is copied to Module1
No, the code should go in the particular worksheet's module. Since you already had Worksheet_SelectionChange code I assumed you had it in the correct place.
 
Upvote 0
The worksheet I am testing on is called 'selection change lock' so my code goes here:

1645531315101.png
 
Upvote 0
very strange, it is still allowing me to enter in the cells that should be locked?
 
Upvote 0
Can you upload a copy of the file with any sensitive data disguised or removed to DropBox, OneDrive, Google Drive etc and post a shared link here?
Also give some details of exactly what cell you selected and what cell(s) you were able to enter into that should have been disallowed.
 
Upvote 0
I am not seeing what appears to be your main problem**.

If I first select B5 then I can move to C5, D5 and/or E5 and choose a drop-down value. I am unable to make a selection in F5, G5 or H5
If I first select B11 then I can move to C5, D5 and/or E5 and am prevented from choosing a drop-down value. I am able to make selections in F5, G5 and H5

What happens to you in that file if you follow step-by-step what I described above?

** The one area where my code may be doing something that you don't want is that if I select a 'B' cell with no value (say B6) then my code is unlocking that row so entries could be made in any of C6:H6. That will be easy enough to correct if you don't want that, but not much point yet if the main objective of the code is not working.
 
Upvote 0

Forum statistics

Threads
1,215,660
Messages
6,126,088
Members
449,287
Latest member
lulu840

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