Consulting

Results 1 to 8 of 8

Thread: Staying on Current Sheet

  1. #1
    VBAX Regular
    Joined
    Oct 2012
    Posts
    35
    Location

    Cool Staying on Current Sheet

    Hello Experts,

    I have a Workbook with multiple Sheets. I have a good amount of code in Module 1 which applies to just one sheet. I am working on say Sheet 2 now and when I run a Macro, it runs the code in Module 1 which results in Sheet 1 being selected. I wanted to know if there is a way to capture my Active view, so that when the macro is run, I can ensure it stays on the active Sheet. I am thinking of a some kind of variable to store the view, then at the end of the code I could select the variable. Any advice is appreciated. Thanks

  2. #2
    VBAX Mentor
    Joined
    Jul 2012
    Posts
    398
    Location
    paste here your code with comments

  3. #3
    VBAX Regular
    Joined
    Oct 2012
    Posts
    35
    Location
    Do you want all the code? It is very long and pulls data from a BW BPC database.

  4. #4
    Quote Originally Posted by snowdyce
    Hello Experts,

    I have a Workbook with multiple Sheets. I have a good amount of code in Module 1 which applies to just one sheet. I am working on say Sheet 2 now and when I run a Macro, it runs the code in Module 1 which results in Sheet 1 being selected. I wanted to know if there is a way to capture my Active view, so that when the macro is run, I can ensure it stays on the active Sheet. I am thinking of a some kind of variable to store the view, then at the end of the code I could select the variable. Any advice is appreciated. Thanks
    try using the following at the beginning of sure code application.screenupdating = false and at the end of your code application.screen updating = true

  5. #5
    VBAX Regular
    Joined
    Oct 2012
    Posts
    35
    Location
    I do have that in the sheet already, thanks though!

  6. #6
    VBAX Regular
    Joined
    Oct 2012
    Posts
    35
    Location
    Hey Guys, I was able to solve it by storing the active sheet as a String.
    [VBA]''This is at the beginning of the code
    Dim strSheetName As String
    strSheetName = ActiveSheet.Name
    ''Then use this at the end to go back to the orginal sheet
    Sheets(strSheetName).Select[/VBA]

  7. #7
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Instead of using a string you can set a variable to the sheet itself (a bit more robust since even if the sheet has its name changed it will still refer to the correct sheet):
    Set OrigSheet = Activesheet
    and then later:
    OrigSheet.activate

    but… it's rare that you need to select the sheet to operate on it; can you eliminate the selections
    eg.
    [VBA]Sheets("Sheet2").select
    Range("A1:A33").Select
    Selection.interior.colorindex = 3
    [/VBA]
    can be condensed to:
    [VBA]Sheets("Sheet2").Range("A1:A33").interior.colorindex = 3[/VBA]
    and the active sheet, whichever that might be, remains the active sheet, so no need to select it at the end of operations.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I'll bet that somewhere in the code applying to sheet 1, you are using [vba]XXX.Select
    Selection.YYY[/VBa]
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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