+ Reply to Thread
Results 1 to 21 of 21

Active sheet shifts back to original sheet when executing code

  1. #1
    Registered User
    Join Date
    10-13-2012
    Location
    Plano, Texas
    MS-Off Ver
    Excel 2007
    Posts
    10

    Active sheet shifts back to original sheet when executing code

    I developed a spreadsheet form for grading student's work for an entire quarter on one sheet. I use the ChangeSelection function to allow a teacher to click a cell that put a particular grade value into the grade total. This works fine on the template sheet where I developed and tested it. I then replicate that sheet to create several student pages based on a roll list in Sheet1. When I click the desired cell in the copied sheet, the ActiveSheet jumps back to the template sheet and posts the grade there. I want the active sheet to remain the sheet for the selected student and execute the the correct grade storage operation on that sheet.

    How can I keep the student sheet as the Active Sheet or force the Active Sheet back to the selected student sheet before executing the grade placement function?

    I've tried creating a global variable (Dim CurrentSheet As Sheet) and setting it when a SheetActivate event occurs, this, however, causes CurrentSheet to get set to the template when a new cell is selected in the student sheet. Is there a way to move the code from the template sheet to the workbook so that it applies to each sheet as it is selected?

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Can you post the code?
    If posting code please use code tags, see here.

  3. #3
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Active sheet shifts back to original sheet when executing code

    Post your code or share your workbook.
    Gary's Student

  4. #4
    Registered User
    Join Date
    10-13-2012
    Location
    Plano, Texas
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Active sheet shifts back to original sheet when executing code

    Quote Originally Posted by Jakobshavn View Post
    Post your code or share your workbook.
    Sorry, but I don't know how to share a workbook on this forum or how to post the entire image of code window (other than including a screen
    capture image)

    Here is the code that processes the SelectionChange event (the top of the VME window shows "Sheet2(Code)" (Sheet2 is the template sheet)
    if I set a breakpoint in this code, when I click on a cell in the copied sheet, the selected sheet changes to Sheet2 but this code does not
    execute until I select a cell in Sheet2.

    ===========================================

    Please Login or Register  to view this content.
    ======================================

    Evidently, this code is only associated with the template sheet and does not get associated with the copy of the template sheet that I create
    for each student. Somehow selecting a cell in the copied sheet causes the template sheet to be activated. Here is the code that I use to create
    the new student work sheets: (sheet "StuMaster" contains the student name list; each student record is tagged with the label "student" in column 2:

    ======================================

    Please Login or Register  to view this content.
    ==============================================
    Last edited by Cutter; 10-21-2012 at 01:05 PM. Reason: Added code tags

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Active sheet shifts back to original sheet when executing code

    What's the name of the Template sheet?

  6. #6
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Active sheet shifts back to original sheet when executing code

    If the event macro is in each of the copied sheets and you want it to operate only on that active, copied sheet, then remove all references to:

    Worksheets("Values")

  7. #7
    Registered User
    Join Date
    10-13-2012
    Location
    Plano, Texas
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Active sheet shifts back to original sheet when executing code

    Quote Originally Posted by Jakobshavn View Post
    If the event macro is in each of the copied sheets and you want it to operate only on that active, copied sheet, then remove all references to:

    Worksheets("Values")
    I'm not sure if the event macro is in each of the copied sheets - it doesn't appear to be so because when I select a new cell in a copied sheet, the template sheet ("StuMaster")
    is immediately selected but the macro for SelectionChange is not executed until I click on a cell in the StuMaster sheet.

    How would I make sure that the macro is copied into each new sheet?

    If I don't reference WorkSheet(Values) how would I get the cell range numbers? Put them in global variables? that seems a bit clumsy in that I would have to anticipate every
    control value that I would normally communicate through the "Values" worksheet.
    Last edited by pcdog; 10-21-2012 at 11:59 AM. Reason: missing detail in original reply

  8. #8
    Registered User
    Join Date
    10-13-2012
    Location
    Plano, Texas
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Active sheet shifts back to original sheet when executing code

    Template sheet name : "StuMaster"

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Active sheet shifts back to original sheet when executing code

    Can you attach a sample workbook?

  10. #10
    Registered User
    Join Date
    10-13-2012
    Location
    Plano, Texas
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Active sheet shifts back to original sheet when executing code

    I tried removing all references to Worksheets("Values") by hardcoding the minCol, maxCol and sumCol values - this did not change the behavior of the WorkBook - still jumps to the
    sheet "stuMaster" (the original template sheet) when I click a cell in one of the copied work sheets.

  11. #11
    Registered User
    Join Date
    10-13-2012
    Location
    Plano, Texas
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Active sheet shifts back to original sheet when executing code

    Quote Originally Posted by Norie View Post
    Can you attach a sample workbook?
    Would love to - how do I do attach a sample workbook?

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Active sheet shifts back to original sheet when executing code

    Click Go Advanced and then goto Manage Attachments.

  13. #13
    Registered User
    Join Date
    10-13-2012
    Location
    Plano, Texas
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Active sheet shifts back to original sheet when executing code

    Quote Originally Posted by Norie View Post
    Click Go Advanced and then goto Manage Attachments.
    Okay - here goes:

    The first three sheets are reserved for control parameters (none used yet), student data, and values to be used to define the rubric
    forms and values for grading. The remaining sheets are student data sheets where the grades will be posted and calculated.

    If the workbook works correctly, you should be able to select a student sheet, then click on a cell across from a graded characteristic and
    under the desired grade. The cell should be marked with an "X" and the grade value entered in the Point Total column. The grade is calculated
    as a percentage of the maximum possible (all "A" column checks).
    Attached Files Attached Files
    Last edited by pcdog; 10-21-2012 at 01:08 PM. Reason: added usage details

  14. #14
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Active sheet shifts back to original sheet when executing code

    @ pcdog

    Welcome to the forum.

    Please notice that [CODE] tags have been added to your post(s). The forum rules require them so please keep that in mind and add them yourself whenever showing code in any of your future posts. To see instructions for applying them, click on the Forum Rules button at the top of the page and read Rule #3.
    Thanks.

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Active sheet shifts back to original sheet when executing code

    pcdog

    In workbook code you have this in each sub, which is activating the template.
    Please Login or Register  to view this content.
    This could be the problem as any code following that line that doesn't have a worksheet reference will reference the template.

    For example, a few lines down you have this.
    Please Login or Register  to view this content.
    That code is going to refer to the template sheet.

    Is it supposed to?

  16. #16
    Registered User
    Join Date
    10-13-2012
    Location
    Plano, Texas
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Active sheet shifts back to original sheet when executing code

    Duh! - Light slowly dawning - Thanks for the VBA lesson

    This looks like it is probably the problem. I did not realize that VBA had done what I wanted in that it copied the code for each sheet so I did not open each sheet to see what might be happening. I was modifying code that only applied to the template sheet ("StuMaster") thinking that it was the one copy of the code that was controlling everything. This means that I will need to regenerate the entire gradebook every time I fix a bug or make a mod to the way that the grading modules work. Is there a way to have a master code that handles the control of every sheet? Could I place that code in the WorkBook code area and adjust a pointer to the active worksheet for each function? That was sort of the way that I thought things were working?

    I'll experiment and research some more.

    Thanks again.

  17. #17
    Registered User
    Join Date
    10-13-2012
    Location
    Plano, Texas
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Active sheet shifts back to original sheet when executing code

    Duh! - Light slowly dawning - Thanks for the VBA lesson

    This looks like it is probably the problem. I did not realize that VBA had done what I wanted in that it copied the code for each sheet so I did not open each sheet to see what might be happening. I was modifying code that only applied to the template sheet ("StuMaster") thinking that it was the one copy of the code that was controlling everything. This means that I will need to regenerate the entire gradebook every time I fix a bug or make a mod to the way that the grading modules work. Is there a way to have a master code that handles the control of every sheet? Could I place that code in the WorkBook code area and adjust a pointer to the active worksheet for each function? That was sort of the way that I thought things were working?

    I'll experiment and research some more.

    Thanks again.

  18. #18
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Active sheet shifts back to original sheet when executing code

    If you have programming that is common to all sheets, rather than put a copy in each sheet, put the common code in a module and have the event code call the module-based sub.

    This way you will only have to maintain a single copy.

  19. #19
    Registered User
    Join Date
    10-13-2012
    Location
    Plano, Texas
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Active sheet shifts back to original sheet when executing code

    That is what I have done - looks like it is working a charm!

    Thanks for the help.

  20. #20
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Active sheet shifts back to original sheet when executing code

    @ pcdog

    Welcome to the forum.

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

    Also, as a new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

  21. #21
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    You can also use the workbook level event SheetSelectionChange.

    If you use that you don't need code in the worksheets.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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