+ Reply to Thread
Results 1 to 9 of 9

Getting Worksheet_Activate to only run when user clicks on the sheet (not at opening file)

Hybrid View

  1. #1
    Registered User
    Join Date
    02-01-2011
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    9

    Getting Worksheet_Activate to only run when user clicks on the sheet (not at opening file)

    [SOLVED]

    Hi,

    I have a file with several sheets. One of them uses the Worksheet_Activate event. This sub sets Application_EnableEvents to False in the beginning and True at the end. The code works fine. It runs when the sheet is selected by the user.

    Now to my problem: When the workbook is opened the Worksheet_Activate code runs. I want the code to only run when the user actually clicks on the sheet.

    I have tried to find a way so that the Worksheet_Activate code does not run at opening. I read somewhere that Workbook_Activate does not run until the workbook has finished opening. I don't know if that is true or not but anyway I tried to disable events in Workbook_Open and then enable them again in Workbook_Activate. But no... The disabling works but it is not enabled again when the file has finished opening.

    And also: I am worried that I, by messing with events at opening, will mess up something that I am not aware of.

    I hope that my description is clear and that someone has a suggestion for how I can solve this.

    Thanks in advance.

    /Zenita
    Last edited by zenita; 03-16-2015 at 08:43 AM. Reason: Cannot find any other place to write [SOLVED]

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Getting Worksheet_Activate to only run when user clicks on the sheet (not at opening f

    Can you post a workbook example because the test file I made with activate code on sheet1 does not fire when workbook is opened and sheet1 already is active.

    You might have some other code in place causing the activation
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    02-01-2011
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Getting Worksheet_Activate to only run when user clicks on the sheet (not at opening f

    Hi,

    I was hoping that I wouldn't have to do that because the file is used at work and has some company data.

    I will try to make a cleaned up file and post it. I'll do that as soon as I can (between meetings).

    Best regards,
    Zenita

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Getting Worksheet_Activate to only run when user clicks on the sheet (not at opening f

    The example file does not require any data, only the code relevant to events. But the file must have the problem in it so we can understand what's happening

  5. #5
    Registered User
    Join Date
    02-01-2011
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Getting Worksheet_Activate to only run when user clicks on the sheet (not at opening f

    Hi again,

    The code requires data in order to work so I have added fake data. So all sensitive data has been removed and passwords changed, but all the code is still there.

    I have also added additional information the file about how to recreate the error.

    Please let me know if anything else is needed.

    Thanks in advance.

    /Zenita
    Attached Files Attached Files

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Getting Worksheet_Activate to only run when user clicks on the sheet (not at opening f

    You can disable events in the BeforeSave event to prevent rerunning code when saving.

    But your problem is more about your code corrupting the workbook at some point.
    That corruption is cause by data validation list being over 255 characters long.

    Public Function addDropDownStr(rng As Range, dropDownFormula As String) As String
    
        Dim MyList As String
        
        MyList = dropDownFormula
        If Len(dropDownFormula) > 255 Then
            MyList = Left(dropDownFormula, 255)
        End If
        
        With rng.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertInformation, Operator:= _
            xlBetween, Formula1:=MyList
            .InCellDropdown = True
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = "You have entered a value that is not predefined"
            .ShowInput = True
            .ShowError = False
            colCounter = colCounter + 1
        End With
    
    End Function
    You might want to use a sheet to store the list in a range and avoid that problem completely.

  7. #7
    Registered User
    Join Date
    02-01-2011
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Getting Worksheet_Activate to only run when user clicks on the sheet (not at opening f

    Hi,

    Thank you for your response.

    I actually want to run the code before saving as well as when the user clicks on the sheet. So I cannot disable events in the BeforeSave.

    But you say that the problem is in the when the data validation list is longer than 255 characters. I will try to look into a way to solve that. It is not acceptable to only list the first 255 characters as the left function would do. I need all items in the validation list. But I will see if I can store it somewhere in a range.

    Kind regards,
    Zenita

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Getting Worksheet_Activate to only run when user clicks on the sheet (not at opening f

    I only suggested the change in the BeforeSave event passed on your request, "I want the code to only run when the user actually clicks on the sheet".

    I agree that simply truncating the list is not practical, hence the use of cells suggestion, but the code should demonstrate the problem error message does not then appear on re-openning the file.

  9. #9
    Registered User
    Join Date
    02-01-2011
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Getting Worksheet_Activate to only run when user clicks on the sheet (not at opening f

    Hi Andy Pope,

    Thank you so much for all your help. I have managed to solve my problem using your input.

    The issue was simply, just as you said, that the validation list became too long. The solution was to store the validation list items in a range in the sheet. I don't get the error anymore.

    By the way, sorry that I missed to mention that the BeforeSave should also run the Worksheet_Activate code. I completely forgot about that. My mistake.

    Have a nice day.

    /Zenita

+ 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. Replies: 0
    Last Post: 06-22-2014, 04:18 PM
  2. Msgbox appear when user clicks save?
    By buckhunt122 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-01-2011, 06:53 PM
  3. Replies: 12
    Last Post: 06-07-2011, 04:17 AM
  4. How to catch when the user clicks a cell?
    By WhiteEagle in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-26-2007, 10:17 AM
  5. Run a macro when a user clicks in a certain cell
    By NickySA in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-16-2007, 08:20 AM

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