run time error 9, subscript out of range

N

Nasir.Munir

Someone please help me with this. When I run this script, it works
fine with some of the sheets but then gives the error for some. It
stops at the For loop and says "subscript out of range".
Sub lock_protect()
Dim pass
Dim sheet_name
pass = "hiacsc"
sheet_name = InputBox("Enter the sheet name")
Dim cell As Range
For Each cell In Sheets(sheet_name).UsedRange.Cells
If cell.HasFormula = True Then
If cell.MergeCells = True Then
With cell.MergeArea
.Locked = True
End With
Else
cell.Locked = True
End If
End If
Next cell
Sheets(sheet_name).Protect (pass)
MsgBox (sheet_name + " is protected now")
End Sub
 
N

Norman Jones

Hi Nasir,

You error would suggest that an incorrect sheetname is enterd in response to
the input box. Providing that a correctly spelled sheet name is entered , I
would not antipate your error.
 
N

Nasir.Munir

Thanks Norman and colofnature !!!

I still have the same error message. Yes it is the existing sheet
colof. Norm I have tried with an array too with all the names(carefully
entered), but it is the same error message. Quite strangely it is
working fine with the first 10 sheets , gives me error for the next 3
sheets and works fine with the last one. Here is my second version of
code, for your review:(gives me error for ENG sheet names)

Sub Hyper2()

Dim name As Variant
name = Array("Colour Page", "CH149", "SLL", "FRI", "CMR", "COS &
Other", _
"DTS", "Maintenance Checks", "LUBRIF", "ENG insp", _
"ENGLCF Calculations", "ENG life", "SB", "MFS Update
Log")
Dim pass

pass = "hiacsc"

Dim cell As Range
For i = 0 To 13
For Each cell In Sheets(name(i)).UsedRange.Cells
Sheets(name(i)).Unprotect (pass)

If cell.HasFormula = True Then
If cell.MergeCells = True Then
With cell.MergeArea
.Locked = True
End With
Else
cell.Locked = True
End If

End If
Next cell
Sheets(name(i)).Protect (pass)
MsgBox (name(i) + " is protected now")
Next i
End Sub
 
N

Norman Jones

Hi Nasir,
I have tried with an array too with all the names(carefully entered),

Despite your care, I would examine the three contentious sheets and ensure
that their names were as expected and, particularly, that no initial,
trailing, or other, spaces were included in the sheet names themselves.

BTW your code lines:
For Each cell In Sheets(name(i)).UsedRange.Cells
Sheets(name(i)).Unprotect (pass)


would appear to repeat the unprotection of each sheet for every cell in the
used range rather than once if you were to invert these two lines. i.e.:

Sheets(name(i)).Unprotect (pass)
For Each cell In Sheets(name(i)).UsedRange.Cells
 
N

Nasir.Munir

Norman, thanks a million. Yes you were right, this time I copied and
pasted the name of the sheets and it is working perfect.
God bless you,
Nasir.
 
N

Nasir.Munir

Norman, I really appreciate your time and much needed help. You are
awesome !!!
Nasir.( I didnt include the trailing spaces :), its working now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top