Home » excel » vba – Unlocking range of cells in excel selected by cellname Office 2007

vba – Unlocking range of cells in excel selected by cellname Office 2007

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have 3 cells which are merged with each other and are referenced to with a given cellname (for example “foo”).

I now want to unlock these cells with the locked attribute.

The lock in the following code will not work, but the value will be successfully assigned to the cell:

Workbooks(loadedSheetName).Worksheets("foo").Range("bar").Locked = False
Workbooks(loadedSheetName).Worksheets("foo").Range("bar") = "foo value"

What will work is referencing the cells by “coordinates” but is not really an option for me:

Workbooks(loadedSheetName).Worksheets("foo").Range("B3:E3").Locked = False

Is there any possibility to select some merged cells by name and set the locked attribute to false?

How to&Answers:

The following code works OK in my Excel 2007


Sub aa()
    Dim ce As Range
    Application.ScreenUpdating = False ''# screen flicker off
    ActiveSheet.Unprotect Password:=""
    For Each ce In Range("rng")
        ce.MergeArea.Locked = "False"
    Next ce
    ActiveSheet.Protect Password:=""
End Sub

HTH!

Answer:

You don’t need to go through every cell in a Range.

Simply…

 Range("myRangeName").Select
 Selection.Locked = False