Locking/unlocking non contigous cells

joey peanuts

New Member
Joined
Mar 20, 2011
Messages
18
I know there has to be a way to clean this up into 1 command instead of selection each cell and locking/unlocking. Any help would be appreciated.

For Each cell In Range("RBARECT_LINE").Cells
If cell.Value <> "" Then
cell.Offset(0, 3).Select
Selection.Locked = False
cell.Offset(0, 4).Select
Selection.Locked = False
cell.Offset(0, 5).Select
Selection.Locked = False
cell.Offset(0, 6).Select
Selection.Locked = False
cell.Offset(0, 11).Select
Selection.Locked = False
cell.Offset(0, 13).Select
Selection.Locked = False
Else
cell.Offset(0, 3).Select
Selection.Locked = True
cell.Offset(0, 4).Select
Selection.Locked = True
cell.Offset(0, 5).Select
Selection.Locked = True
cell.Offset(0, 6).Select
Selection.Locked = True
cell.Offset(0, 11).Select
Selection.Locked = True
cell.Offset(0, 13).Select
Selection.Locked = True
End If
Next cell
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You don't need to select the cells:

Code:
Sub T()
    For Each cell In Range("RBARECT_LINE").Cells
        cell.Offset(0, 3).Locked = cell.Value = ""
        cell.Offset(0, 4).Locked = cell.Value = ""
        cell.Offset(0, 5).Locked = cell.Value = ""
        cell.Offset(0, 6).Locked = cell.Value = ""
        cell.Offset(0, 11).Locked = cell.Value = ""
        cell.Offset(0, 13).Locked = cell.Value = ""
    Next cell
End Sub
 
Upvote 0
Or maybe
Code:
    For Each cell In Range("RBARECT_LINE").Cells
        cell.Range("D1:G1,L1,N1").Locked = IsEmpty(cell.Value)
    Next cell
 
Upvote 0
You don't need to select the cells:

Code:
Sub T()
    For Each cell In Range("RBARECT_LINE").Cells
        cell.Offset(0, 3).Locked = cell.Value = ""
        cell.Offset(0, 4).Locked = cell.Value = ""
        cell.Offset(0, 5).Locked = cell.Value = ""
        cell.Offset(0, 6).Locked = cell.Value = ""
        cell.Offset(0, 11).Locked = cell.Value = ""
        cell.Offset(0, 13).Locked = cell.Value = ""
    Next cell
End Sub

If the cell value of RBARECT_LINE is not blank, this doesn't seem to be unlocking the offset cell. Is there a way to tweak this. I tried duplicating these lines and substituting .locked with .unlocked, but no change in behavior.
 
Upvote 0
If the cell isn't blank then:

cell.Value = ""

will be False, so the cell won't be locked.

This code is in the OnActivate area. I have all cells set by default to be locked until it runs through this loop. Would that explain why some of the offset cells that should be unlocked aren't?
 
Upvote 0
This code is in the OnActivate area. I have all cells set by default to be locked until it runs through this loop. Would that explain why some of the offset cells that should be unlocked aren't?


I was using the wrong offset columns. Works perfect - thank you.
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,879
Members
452,948
Latest member
Dupuhini

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