Consulting

Results 1 to 10 of 10

Thread: Autorun Macro/Select Tab

  1. #1
    VBAX Regular
    Joined
    Jul 2005
    Posts
    19
    Location

    Autorun Macro/Select Tab

    hi everyone
    quick question please. is it possible to have a macro run automatically, when selecting a tab in a workbook? each time i select a tab, before the sheet appears, a macro runs. thanks for any help. you folks are great.

  2. #2
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Sure:

    [VBA]Option Explicit

    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    MsgBox "Hey"
    End Sub
    [/VBA]
    But I'm not sure about the "before sheet appears" portion




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  3. #3
    VBAX Regular
    Joined
    Mar 2006
    Posts
    44
    Location

    "Before" is tricky

    The "before sheet appears" bit (if required) is a bit tricky. At first I thought using Workbook_SheetDeactivate instead would do the job, but appears to move to the new sheet before the macro call is made. You could do something like this:

    [VBA]Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Application.EnableEvents = False
    Worksheets("SomeSheet").Activate
    MsgBox "Hey"
    Sh.Activate
    Application.EnableEvents = True
    End Sub
    [/VBA]

    If you try this, you do get a brief flash of the destination sheet, but you then flick to another sheet (I've picked SomeSheet) to do your thing before returning to the selected sheet. Not perfect, but it's something.

    Sean.

  4. #4
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Similar to Sean's, but this keeps the previous sheet visible...although this too gets the flickering:

    [vba]Option Explicit
    Dim shName As String

    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Application.EnableEvents = False
    Sheets(shName).Activate
    MsgBox "Hey"
    Sh.Activate
    Application.EnableEvents = True
    End Sub

    Private Sub Workbook_SheetDeActivate(ByVal Sh As Object)
    shName = Sh.Name
    End Sub
    [/vba] Now how to get rid of the flickering???




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  5. #5
    VBAX Regular
    Joined
    Jul 2005
    Posts
    19
    Location
    good morning. you folks are great. ill give these suggestions a shot as soon as i get into work. not worried about the flickering, just that my macro runs when i select a tab. again thanks.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I assume that the reason for wanting to run the macro before the new sheet displays is to run the macro against data on the previous sheet? If so, by using the Deactivate event, the Sh object still referes to the sheet just exited, so your code can address data there like so

    [vba]
    Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    MsgBox "Previous sheet name is " & Sh.name
    MsgBox "New sheet name is " & ActiveSheet.name
    End Sub
    [/vba]

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by malik641
    Now how to get rid of the flickering???
    Application.ScreenUpdating = False/True?
    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'

  8. #8
    VBAX Regular
    Joined
    Jul 2005
    Posts
    19
    Location
    hi, i must be doing something wrong. i cant get any of the vba code to come back with the "hey", so i assume its not functioniing as intended. i put the code on the pane for the sheet. can someone nudge me in the right direction. thanks

  9. #9
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Quote Originally Posted by mdmackillop
    Application.ScreenUpdating = False/True?
    Can't get it to work...

    I'm going to have to get back on this later this afternoon..a little busy at my job right now.

    And phiore, make sure that Application.EnableEvents = True...if the Message Box still isn't coming up....then




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  10. #10
    VBAX Regular
    Joined
    Mar 2006
    Posts
    44
    Location
    I don't think that ScreenUpdating will do the trick: by the time you set it to False, the new sheet has already been activated. Initially I thought that SheetDeactivate might give you a chance to set ScreenUpdating to false before activating the new sheet, but it does not.

    Sean

Posting Permissions

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