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
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

Not certain what you are asking, but could it be this?
VBA Code:
Intersect(cell.EntireRow, Columns("D:F")).Locked = True
 
Upvote 0
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

Not certain what you are asking, but could it be this?
VBA Code:
Intersect(cell.EntireRow, Columns("D:F")).Locked = True
thanks Peter for your reply, really appreciated.

I've attached a screenshot to hopefully explain better, but, basically what i am trying to achieve is;

* user selects a value in column B
* based on that value the user needs to complete cells in column C:E or F:H with the non required cells being locked

* in each of the columns are named range drop down lists.

sounds easy (and probably is!) but been at this for quite a while and driving me barmy lol,

Really appreciate your help and thank you
 

Attachments

  • excel vba help.jpg
    excel vba help.jpg
    150.6 KB · Views: 8
Upvote 0
I'm still not sure that I have understood, but is this it?

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Selection.CountLarge = 1 And Not Intersect(Target, Range("B4:B18")) Is Nothing Then
    ActiveSheet.Unprotect "Password"
    With Intersect(Target.EntireRow, Columns("C:H"))
      .Locked = False
      Select Case Target.Value
        Case "Expansion"
          .Cells(, 4).Resize(, 3).Locked = True
        Case "SIB"
          .Cells(, 1).Resize(, 3).Locked = True
      End Select
    End With
    ActiveSheet.Protect "Password"
  End If
End Sub
 
Upvote 0
Hi Peter,

Still not quite right, I've attached another screenshot to hopefully help. Sorry to be a pain, thanks
 

Attachments

  • excel vba help V2.jpg
    excel vba help V2.jpg
    235 KB · Views: 10
Upvote 0
I can't tell much from an image nor can I copy from it. Consider XL2BB
In what way was it "not quite right"?

Also, please remember the first paragraph from post #2
 
Upvote 0
just sent you a PM Peter as struggling with the xl2bb add-in ,
Basically, the current code is protecting all the cells and not allowing the input / locking per row based on the selection in column B.

Thanks
 

Attachments

  • excel vba help V3.jpg
    excel vba help V3.jpg
    92 KB · Views: 4
Upvote 0
Basically, the current code is protecting all the cells and not allowing the input / locking per row based on the selection in column B.
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
 
Upvote 0
User Selection
ExtensionUser InputUser InputUser InputNO INPUT _ LOCKNO INPUT - LOCKNO INPUT - LOCK
SIBNO INPUT - LOCKNO INPUT - LOCKNO INPUT - LOCKUser InputUser InputUser Input
etc
etc

each row will have a different set of cells that will be locked etc based on what is selected in column B.
 
Upvote 0
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


Yes, that is exactly what should happen. How strange, let me just double check.
 
Upvote 0

Forum statistics

Threads
1,214,960
Messages
6,122,479
Members
449,088
Latest member
Melvetica

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