+ Reply to Thread
Results 1 to 4 of 4

Intermittent error.... help me make my script more stable.

  1. #1
    Registered User
    Join Date
    01-05-2014
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    13

    Intermittent error.... help me make my script more stable.

    Hi below is my subroutine. What it does is to unlock a range in a column based on a cell being "empty" or "filled" with data.
    So for example If F3 is empty F10 to F54 is locked, Else IF F3 has data that range is unlocked.

    The problem is that the subroutine is unstable. Sometimes I get runtime error 13 and the debugger highlights the line

    If Target.Value = "" Then


    Here's the subroutine....

    Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("F3:O3")) Is Nothing Then
    If Target.Value = "" Then
    Sheets(ThisWorkbook.ActiveSheet.Name).Unprotect Password:="1234" 'change to your actual password
    Range(Cells(10, Target.Column), Cells(54, Target.Column)).ClearContents
    Range(Cells(10, Target.Column), Cells(54, Target.Column)).Locked = True
    Sheets(ThisWorkbook.ActiveSheet.Name).Protect Password:="1234"
    Else
    Sheets(ThisWorkbook.ActiveSheet.Name).Unprotect Password:="1234" 'change to your actual password
    Range(Cells(10, Target.Column), Cells(54, Target.Column)).Locked = False
    Sheets(ThisWorkbook.ActiveSheet.Name).Protect Password:="1234"
    End If
    End If

    End Sub

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Intermittent error.... help me make my script more stable.

    Okay - first off, can you please wrap your code in CODE tags - makes it much easier to read, and to copy.

    Now, to your problem - runtime error 13 shows a type mismatch, most likely caused when you have an error value in your target cell. Handle this by checking if the target cell is an error value.

    I've also changed your code a little to deal with range changes of more than one cell at a time (multiple cell selection and delete, for example).

    Please Login or Register  to view this content.
    Last edited by Olly; 01-21-2014 at 05:31 AM.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: Intermittent error.... help me make my script more stable.

    The problem happens probably when the target is not a single cell

    so may be you shall check every cell in possibly multi-cell Target one by one.

    Please Login or Register  to view this content.
    PS. Please note how indenting (just kindness to those reading) and using code tags (required by forum rules) improve readability of the code

  4. #4
    Registered User
    Join Date
    01-05-2014
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Intermittent error.... help me make my script more stable.

    Hi All,

    Firstly, let me apologise about the CODE TAGS... I couldn't figure out how to get them earlier.... I have been schooled since.
    Thank for both the solutions above... both worked but I'll be using OllyXLS solution...

    Thanks a bunch.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Intermittent Error 1004 on Selection.PasteSpecial
    By BeachRock in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 10-21-2012, 01:23 AM
  2. How to make the Header stable
    By simi in forum Excel General
    Replies: 1
    Last Post: 12-03-2009, 09:25 AM
  3. Intermittent Runtime Error when inserting picture
    By vamosj in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-20-2009, 04:36 AM
  4. Insert Pic Intermittent runtime error
    By vamosj in forum Access Programming / VBA / Macros
    Replies: 0
    Last Post: 04-19-2009, 07:01 AM
  5. [SOLVED] Run-time error '1004'; Document not saved - intermittent
    By Kerry in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-15-2005, 07:15 PM

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