Too many ELSEIF statements

gord9b

Board Regular
Joined
Jun 12, 2002
Messages
247
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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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,196,487
Messages
6,015,498
Members
441,898
Latest member
kofafa

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