+ Reply to Thread
Results 1 to 6 of 6

VBA to check for active sheet then execute following code

  1. #1
    Forum Contributor Pierce Quality's Avatar
    Join Date
    07-29-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    221

    VBA to check for active sheet then execute following code

    Hi All,

    Im having a little trouble with getting this code to work correctly and was wondering if someone could point me in the right direction.
    Here is the code that I am using:

    Please Login or Register  to view this content.
    Which works great as is. Basically it sorts my data, then selects the first empty cell when the user saves the workbook.
    However, now I have more than one sheet in the book, so if the user is on another sheet then saves the book he starts getting errors.
    So I wanted to get the sort to only execute if Sheet1 is active, and heres what I added:

    Please Login or Register  to view this content.
    Which will save the book but doesnt perform the sort or cell selection after.
    Any help with this would be greatly appreciated, thanks!
    Last edited by Pierce Quality; 11-12-2013 at 11:19 AM.

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

    Re: VBA to check for active sheet then execute following code

    Try this.

    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: VBA to check for active sheet then execute following code

    The event handler is set up for before save, it won't ever peform anything after save.

    Are you saying the sort does not happen on Sheet1 when it is the activesheet? Do you want the sort to happen on Sheet1 no matter what sheet is active?
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA to check for active sheet then execute following code

    Maybe:

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor Pierce Quality's Avatar
    Join Date
    07-29-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    221

    Re: VBA to check for active sheet then execute following code

    Quote Originally Posted by Norie View Post
    Try this.

    Please Login or Register  to view this content.
    Hello Norie, thanks for the reply!
    This code works great, thank you!

    Quote Originally Posted by Solus Rankin View Post
    The event handler is set up for before save, it won't ever peform anything after save.

    Are you saying the sort does not happen on Sheet1 when it is the activesheet? Do you want the sort to happen on Sheet1 no matter what sheet is active?
    Hello Solus, thanks for the reply!
    The sort happens when sheet1 is active, but with multiple sheets it tries to sort the data on whatever sheet is active. I just want the sort to happen on sheet1 on sheet 1 regardless of what sheet is active.

    Quote Originally Posted by JOHN H. DAVIS View Post
    Maybe:

    Please Login or Register  to view this content.
    Hello John, thanks for the reply!
    Your code worked great, thank you!

    Ill be marking this as solved, thanks for the help everyone!

  6. #6
    Forum Contributor Pierce Quality's Avatar
    Join Date
    07-29-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    221

    Re: VBA to check for active sheet then execute following code, follow up

    All, just as a follow up, I was running the existing code through some testing an realized that it would be better to have the code execute only if Sheet1 was active, like Solus had asked, and if any other sheets were active instead, the workbook would just save and the sort would not be performed.
    Here is the result that I ended up with:

    Please Login or Register  to view this content.
    Thanks again!
    Last edited by Pierce Quality; 11-12-2013 at 12:54 PM.

+ 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. Code to clear check box fails to execute
    By brharrii in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-18-2013, 03:51 PM
  2. Need to check if worksheet exists and execute code
    By chiidzzz in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-25-2012, 08:55 PM
  3. Need code to check for duplicates of active cell
    By davekippen in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-21-2011, 09:38 AM
  4. Code will only execute from one sheet
    By kirsty in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-12-2011, 07:07 PM
  5. Need VBA sheet code to execute when a row is delete
    By mikeburg in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-11-2006, 12:46 PM

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