+ Reply to Thread
Results 1 to 4 of 4

Need Help With VBA code Run-Time error 91

  1. #1
    Registered User
    Join Date
    10-25-2012
    Location
    Tennessee
    MS-Off Ver
    Excel 2007
    Posts
    2

    Need Help With VBA code Run-Time error 91

    I am very new to VBA coding on Excel, and I have run into a problem, that I'm not sure what I've done wrong.

    I have a button designed on a worksheet to clear all the unprotected cells. This is the code:

    Please Login or Register  to view this content.
    When I try to run this, I get Run-Time Error 91, and the debugging points me back to the "If Cell.Locked = False" part.

    Any suggestions would be greatly appreciated.
    Last edited by jeffreybrown; 10-25-2012 at 11:54 AM. Reason: Please use code tags...Thanks.

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Need Help With VBA code Run-Time error 91

    whitners_98,

    Welcome to the forum!
    The problem is that you have a For/Next loop, but nothing inside it. Move the For Next to be after the If response = vbYes, and make sure that inside the for/next is the If Cell.Locked line
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    10-25-2012
    Location
    Tennessee
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Need Help With VBA code Run-Time error 91

    That worked - Thank you!!!

    However, now I've run into another problem. I have some cells that are merged, and I'm getting an error "Cannot Change Part of a Merged Cell"

    The Debugging points me to "Cell.ClearContents"

    Any other advice?

    Here's the updated code:

    Sub ClearUnlockedCells()
    Dim WorkRange As Range
    Dim Cell As Range
    Dim intresponse As Integer
    Set WorkRange = ActiveSheet.UsedRange



    intresponse = MsgBox("Are you sure you want to Clear this Worksheet?", vbYesNo + vbQuestion, "Quit")

    If intresponse = vbNo Then End

    For Each Cell In WorkRange

    If intresponse = vbYes Then
    If Cell.Locked = False Then Cell.ClearContents

    End If
    Next

    End Sub

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Need Help With VBA code Run-Time error 91

    whitners_98,

    Please wrap your code in code tags. See link in my sig for how.
    As for your question, the simple answer is don't use merged cells. They're unnecessary and a nightmare to work with. If you absolutely must work with merged cells, then try:
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1