Too many ELSEIF statements

gord9b

Board Regular
Joined
Jun 12, 2002
Messages
249
I currently have a macro that unlocks a range of cells depending on the numeric value of an input cell. It looks like this:

[c4:c19].Locked = True
If [b2] > 15 Then
[c4:c19].Locked = False
ElseIf [b2] > 14 Then
[c4:c18].Locked = False
ElseIf [b2] > 13 Then
[c4:c17].Locked = False
ElseIf [b2] > 12 Then
[c4:c16].Locked = False
ElseIf [b2] > 11 Then

As you can see, this is unwieldy. If I want to expand the range of cells, then the number of ElseIf statements becomes unwieldy.
Is there a way to insert the cell value in the logic, such as:
[c4:"value in cell b2")].Locked = False
where the second parameter would be the numeric value of cell b2?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi Gordon,

You can generalize

[c4:"value in cell b2")].Locked = False

as follows:

Range([C4],Cells([B2].Value,3)).Locked = False

where B2 contains the row number and the 3 refers to column C. And of course you can offset the value in B2 to replace your set of If...ElseIf tests:

Range([C4],Cells([B2].Value+4,3)).Locked = False
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

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