View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
[email protected] Nasir.Munir@dal.ca is offline
external usenet poster
 
Posts: 37
Default run time error 9, subscript out of range

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