+ Reply to Thread
Results 1 to 3 of 3

Stop Worksheet change event from interfering with macro code

Hybrid View

  1. #1
    Forum Contributor BeachRock's Avatar
    Join Date
    11-01-2011
    Location
    Oregon, United States
    MS-Off Ver
    Excel 2016
    Posts
    403

    Stop Worksheet change event from interfering with macro code

    Hi everyone,

    I have a worksheet event that seems to be interfering with the functionality of a macro.

    The macro is used for adding a specific number of new records based on the number value of a cell on the worksheet. Without the worksheet event code in place, the add record macro functions flawlessly. With it, no matter how many records are selected to be created, only one is created.

    The worksheet even watches a specific cell on each row and locks and unlocks cells in the same row when the value of the watched cell changes based on 5 different possible settings. Other than it interefering with the macro functionality, it too works flawlessly otherwise.

    I've been trying everything I can think of but I can't get the two to play nice together. The worksheet event code and the macro code are below. Can anyone tell me why this is happening and what I can do to fix it?

    Thanks, very much!

    Worksheet Event Code
    Private Sub Worksheet_Change(ByVal Target As Range)
    '
        If Not Intersect(Target, Range("Case_Entry")) Is Nothing Then
            Dim cell As Range
            Unprotect Password:=""
            For Each cell In Intersect(Target, Range("Case_Entry"))
                Select Case cell.Value
                    Case "EOH Case"
                        cell.Offset(0, 5).Locked = False   'Column Z
                        cell.Offset(0, 6).Locked = False   'Column AA
                        cell.Offset(0, 7).Locked = True    'Column AB
                        cell.Offset(0, 8).Locked = True    'Column AC
                        cell.Offset(0, 9).Locked = True    'Column AD
                    Case "EOH Enq"
                        cell.Offset(0, 5).Locked = False   'Column Z
                        cell.Offset(0, 6).Locked = False   'Column AA
                        cell.Offset(0, 7).Locked = True    'Column AB
                        cell.Offset(0, 8).Locked = True    'Column AC
                        cell.Offset(0, 9).Locked = True    'Column AD
                    Case "Brochure"
                        cell.Offset(0, 5).Locked = True    'Column Z
                        cell.Offset(0, 6).Locked = True    'Column AA
                        cell.Offset(0, 7).Locked = False   'Column AB
                        cell.Offset(0, 8).Locked = False   'Column AC
                        cell.Offset(0, 9).Locked = False   'Column AD
                    Case ""
                        cell.Offset(0, 5).Locked = False   'Column Z
                        cell.Offset(0, 6).Locked = False   'Column AA
                        cell.Offset(0, 7).Locked = False   'Column AB
                        cell.Offset(0, 8).Locked = False   'Column AC
                        cell.Offset(0, 9).Locked = False   'Column AD
                    Case "Case"
                        cell.Offset(0, 5).Locked = True   'Column Z
                        cell.Offset(0, 6).Locked = True   'Column AA
                        cell.Offset(0, 7).Locked = True   'Column AB
                        cell.Offset(0, 8).Locked = True   'Column AC
                        cell.Offset(0, 9).Locked = True   'Column AD
                    Case Else
                        cell.Offset(0, 5).Locked = False   'Column Z
                        cell.Offset(0, 6).Locked = False   'Column AA
                        cell.Offset(0, 7).Locked = False   'Column AB
                        cell.Offset(0, 8).Locked = False   'Column AC
                        cell.Offset(0, 9).Locked = False   'Column AD
                    End Select
            Next cell
    '    
            Protect Password:=""
        End If
    '    
    End Sub

    Macro Code
    Sub Insert_New_Case_Record_Using_Filter_Row_1()
    '
    ' Insert_New_Case_Record_Using_Filter_Row_1
    '
    Dim wsh As Worksheet
    Dim rng As Range
    Dim i As Long
    Dim iRet1 As Integer
    Dim strPrompt1 As String
    Dim strTitle1 As String
    '
    '
    '
        Application.ScreenUpdating = False
    '
        Set wsh = Worksheets("Cases")
         On Error Resume Next
        wsh.Unprotect Password:=""
    '   
        Range("Q14:X19").Select
        Selection.ClearContents
    '    
        Range("14:19").EntireRow.Hidden = True
        ActiveSheet.Range("$T$13:$Z$13").Name = "Criteria"
    '
        strPrompt1 = "Please select the number of new records to be created from Q4."
        strTitle1 = "Record Creation Error"
    '
        If Range("Q4") < 1 Then
        iRet1 = MsgBox(strPrompt1, vbOKOnly, strTitle1)
            If iRet1 = vbOKOnly Then
           Exit Sub
            End If
        End If
    '
        If ActiveSheet.AutoFilterMode Then
            If ActiveSheet.FilterMode Then
                ActiveSheet.ShowAllData
            End If
        ElseIf ActiveSheet.FilterMode Then
            ActiveSheet.ShowAllData
        End If
    '
        For i = 1 To Range("Q4")
        Range("21:21").Copy
        Range("25:25").Select
        Selection.Insert Shift:=xlDown
        Range("Q13:S13").Copy
        Range("Q25:S25").PasteSpecial xlPasteValues
        Range("U13:X13").Copy
        Range("U25:X25").PasteSpecial xlPasteValues
            Next i
    '    
    '
        Range("DataRows").EntireRow.Hidden = False
    '
        Range("24:24").EntireRow.Hidden = True
    '
        Range("Q4").Value = 1
    '
        wsh.Protect Password:=""
    '
        Range("Q25").Select
    '
        Application.ScreenUpdating = True
    '
    End Sub
    -------------
    Tony

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Stop Worksheet change event from interfering with macro code

    Much as you do with ScreenUpdating, try setting the Application.EnableEvents property to False. This will stop changes made by code from firing other events - for example, the Worksheet_Change event.

  3. #3
    Forum Contributor BeachRock's Avatar
    Join Date
    11-01-2011
    Location
    Oregon, United States
    MS-Off Ver
    Excel 2016
    Posts
    403

    Re: Stop Worksheet change event from interfering with macro code

    Cytop, thanks very much! That did the trick. Still learning so didn't know about the Application.EnableEvents property. Great tip!

    It works beautifully now.

+ 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. Code for Worksheet Change Event Needed
    By ARGK in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-17-2013, 11:54 PM
  2. [SOLVED] Using Worksheet Deactivate event to run macro and stop deactivation if required
    By Bonnister in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-09-2013, 11:00 AM
  3. Worksheet Change Event code help
    By kev_33 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-12-2013, 01:51 AM
  4. Stop code from triggering worksheet event
    By mashley in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-17-2010, 11:47 AM
  5. Replies: 5
    Last Post: 06-23-2005, 06:05 PM

Tags for this Thread

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