Excel 2013 worksheet activate

前端 未结 9 1741
情话喂你
情话喂你 2021-01-03 03:28

I have an issue with activating a sheet from a user form, the same code works fine in Excel 2003 to Excel 2010, doesn\'t work with Excel 2013.

This is how to simply

相关标签:
9条回答
  • 2021-01-03 03:33

    The workaround for the above Excel 2013 worksheet activation through automation bug is below (c#):

    public static void ActivateSheetAndWorkaroundExcel2013VBASheetActivationBug( Worksheet oSheet2Activate )
    {
        if( oSheet2Activate.IsNull() )
            return;
    
        oSheet2Activate.Activate();
    
        // Excel 2013 has problems activating worksheet through automation
        // https://www.google.com/webhp?ie=utf-8&oe=utf-8#q=excel+2013+worksheet+activate+problem+
        // http://stackoverflow.com/questions/18726141/excel-2013-worksheet-activate
        // 
        // The only way to reset the Excel 2013 state is to hide/show the active window
        if( Application.Version == "15.0" )
        {
            Window oActiveWnd = Application.ActiveWindow;
            oActiveWnd.Visible = false;
            oActiveWnd.Visible = true;
            oActiveWnd.Activate();
        }
    }
    

    Call that helper method instead of directly calling oSheet.Activate(). You're welcome:-)

    0 讨论(0)
  • 2021-01-03 03:33

    I think Dima Reznikov has the right answer. I used his C# code and did it in VBA and it seems to fix the problem. My personal code also included the use of a Modal UserForm.

    Example:

    Sub Macro1()
        Dim ws As Worksheet
        Dim win As Window
    
        '... bunch of code here...
    
        ws.Activate
        Set win = Application.ActiveWindow
        win.Visible = False
        win.Visible = True
    End Sub
    

    Hopefully this helps someone else who is also confused by the C# code.

    0 讨论(0)
  • 2021-01-03 03:38

    Please avoid using .SELECT. You may want to see THIS?

    Having said that, if you really want to activate the sheet then use .ACTIVATE

    For example

    Sheets("Sheet2").Activate
    

    So using that in your code, it will look like this.

    Sub Macro1()
        Sheets("Sheet2").Activate
    End Sub
    
    Sub Macro2()
        Macro1
        Unload Me
    End Sub
    
    0 讨论(0)
  • 2021-01-03 03:38

    In the worksheet code on worksheet_selectionchange event just put in me.activate.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Me.Select
    End Sub
    
    0 讨论(0)
  • 2021-01-03 03:48

    I found a possible workaround for a similar situation. We have a complex Excel addin that uses both COM and XLL code. (No VBA) We encountered similar issues as above. (When activating a sheet from a modal dialog, the new cell data appeared on the previous sheet).

    In our case, we discovered that the issue appeared only when our code evaluated a cell Range's ".HasFormula" property. There's no logical explanation. If our compiled .Net code evaluated that property, we would observe the bug. If the code ignored that property, we did not observe the bug. We also found that if we manually clicked back and forth between worksheets (tabs), the bug went away.

    0 讨论(0)
  • 2021-01-03 03:52
    Sub Macro1()
    Sheets("Sheet2").Select
    End Sub
    
    Sub Macro2()
    Unload Me
    Call Macro1
    End Sub
    

    This works. I could reproduce your error to an extent, but it may be the location of your macros? (modules vs sheets).

    EDIT: I didn't see your comment at first, I have Excel2010 (which is why I couldn't really reproduce it). -Sorry

    0 讨论(0)
提交回复
热议问题