Consulting

Results 1 to 6 of 6

Thread: Ignoring a subroutine

  1. #1
    VBAX Regular
    Joined
    Mar 2006
    Posts
    11
    Location

    Ignoring a subroutine

    I recently had a post asking for help with a bit of code and had such success I figured I would try one more time.

    I have created a the following sub routine

     
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
       'On Error GoTo Finished
            
        If Target.Row >= 8 And Target.Row <= 32 Then
            
            If Target.Address = ("$B$" & Target.Row) Then
            frmCalendar.Show
            End If
            
        Else
        End If
        
    Finished:
    
    End Sub
    This code is activated everytime a user selects a cell in the B column and brings up the calendar. However, there are other sub routines that also affect cells in column b and when they are run, it also runs the above subroutine. Is there a way I can code it in that when the other subroutines are run, it ignores the one above? I only want the above subroutine to run when I "click" in the cell, not when the focus is brought to a cell with another subroutine.

    I am thinking since there is a way to supress warning screens while executing code, there is a way to supress subroutines from running while another is being executed.

    Sean

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    In your other routines, start with

    [vba]
    Application.EnableEvents = False
    [/vba]

    and at the end, reset back to True

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You disable "events" using EnableEvents as follows
    [VBA]
    Sub MoveToCol()
    Application.EnableEvents = False
    [B12].Select
    Application.EnableEvents = True
    End Sub

    [/VBA]
    Be cautious with this. If there is an error in your code and execution halts, Events will not be reenabled and some functionality may "disappear". You need to add proper error handling to reenable events in the event of code failure.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Regular
    Joined
    Mar 2006
    Posts
    11
    Location
    Too Funny, that was the exact code I was talking about supressing errors, just didn't think it worked for subroutines. Useful little tidbit of information.

    Thanks again, you guys rock!!!

    Sean

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    One other line of code you might find very useful in sheet event macros, usually placed at the start of your code, it avoids error messages etc. when multiple cells are selected for deletion etc.
    [VBA]
    If Target.Cells.Count>1 then Exit Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by JG4life
    Too Funny, that was the exact code I was talking about supressing errors, just didn't think it worked for subroutines. Useful little tidbit of information.

    Thanks again, you guys rock!!!

    Sean
    It doesn't suppress procedures, it just turns off event enabling, so events do not work any longer. Any other procedures will work as normal.

Posting Permissions

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