+ Reply to Thread
Results 1 to 10 of 10

Excel 2013 wrong window activated from VBA

  1. #1
    Registered User
    Join Date
    06-28-2011
    Location
    Bristol, UK
    MS-Off Ver
    Excel 2010, 2013, 2016, 365
    Posts
    79

    Question Excel 2013 wrong window activated from VBA

    Dear alll,

    I have a curious problem - I have a complex VBA project, part of which allows users to export 5 worksheets to a new workbook, after which the new workbook is activated. This works fine in Excel 2010 but when I run the code in 2013, 2 strange things happen:

    1. the nb.Activate line in the code seems to activate the originating workbook and vice versa, ThisWorkbook.Activate shifts focus to the new workbook;

    2. when clicking on the X to close the window of the new workbook, the originating workbook closes instead.

    It's very frustrating and seems to be a problem only in Excel 2013 - it'd as if the Application processes are getting confused about the identity of the two workbooks but even when I use the code Workbooks("NewBook.xlsx").Activate instead, it still activates the wrong workbook. Has anyone else encountered this?

    It is worth noting that this is happening while the originating workbook has two Userforms open in it - this could be a factor.

    Any thoughts?

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,694

    Re: Excel 2013 wrong window activated from VBA

    It will be difficult to diagnose without seeing the code.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

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

    Re: Excel 2013 wrong window activated from VBA

    Normally when you create a new workbook it becomes the active workbook, so there should be no need to activate it.

    How are you creating the new workbook?
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    06-28-2011
    Location
    Bristol, UK
    MS-Off Ver
    Excel 2010, 2013, 2016, 365
    Posts
    79

    Re: Excel 2013 wrong window activated from VBA

    Ok, I have removed some of the code as it is proprietary, but here is the essence of it:

    Please Login or Register  to view this content.

    As may be apparent, there is a lot of jumping back and forth between the workbooks (hence not relying on the new book being active as a result of being created) but I have clearly set the new book back to being active at all necessary exit points...

    So yeah, puzzled. As I say, it works in 2010...

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

    Re: Excel 2013 wrong window activated from VBA

    Why do you need to jump around the workbooks when you have references to them?

    By the way, where in the code is the nb.Activate you mention in the original post?

  6. #6
    Registered User
    Join Date
    06-28-2011
    Location
    Bristol, UK
    MS-Off Ver
    Excel 2010, 2013, 2016, 365
    Posts
    79

    Re: Excel 2013 wrong window activated from VBA

    I was trying a variety of different approaches, so in the code pasted here it actually uses "Application.Windows(nb.Name).Activate" instead - but the outcome is the same.

    As for why jumping between workbooks, it is because changes are being made to both in the course of the code that has been omitted - it takes about 5-10secs to process everything, during which time the original workbook is displayed. Only at the end, once the export has completed, should the new workbook be visible and activated.

    As I say, the code works perfectly in 2010.

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

    Re: Excel 2013 wrong window activated from VBA

    Have you stepped through the code to see what nb is referring to as it executes?

    By the way, there is usually no need to activate/select workbooks/worksheets etc to work with them.

  8. #8
    Registered User
    Join Date
    06-28-2011
    Location
    Bristol, UK
    MS-Off Ver
    Excel 2010, 2013, 2016, 365
    Posts
    79

    Re: Excel 2013 wrong window activated from VBA

    I have and the frustrating thing is that nb is being referenced correctly so far as I can tell (using a debug.print to check the name)...

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

    Re: Excel 2013 wrong window activated from VBA

    What happens if you try closing the new workbook without activating it?
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    06-28-2011
    Location
    Bristol, UK
    MS-Off Ver
    Excel 2010, 2013, 2016, 365
    Posts
    79

    Re: Excel 2013 wrong window activated from VBA

    I think I have solved the problem - it seems to be related to the fact that the Excel application windowstate is Maximized.

    When both the originating workbook and the exported workbook are maximised and I click on the X to close the exported file, it actually closes the original workbook, however, if I set the windowstate to normal, it closes the correct workbook.

    What I have done, then, is to set the Application.Windowstate = xlNormal, activate the new workbook, then reset Application.Windowstate = xlMaximized. This forces the applciation to activate the correct window.

    I'm fairly sure that this is an issue with Excel 2013 - do you have any thoughts?

+ 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. [SOLVED] Stuck using the Offset in Excel 2013, hi guys and gals, what is wrong with this code?
    By Hurricanefly in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-09-2013, 01:01 PM
  2. Link Excel 2013 to Word 2013
    By drosen99 in forum Word Formatting & General
    Replies: 20
    Last Post: 02-26-2013, 08:20 PM
  3. [SOLVED] How to get a pop-up window to tell you when you type ind the wrong password?
    By Siaach in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-14-2012, 05:35 AM
  4. Event when Excel window is activated
    By hojkoff in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-10-2011, 08:30 AM
  5. Returning the wrong values...but not in the Immediate window?
    By johnhildreth@citynet.net in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-24-2006, 05:35 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