Reputation: 51
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 reproduce the issue:
Have a workbook with 2 worksheets in it, called Sheet1
and Sheet2
let's say, and on Sheet1
2 buttons:
On click of Button1
activates Sheet2
worksheet using a Macro1
with 1 line in it:
ThisWorkbook.Sheets("Sheet2").Select
and I can edit data from it fine.
On click of Button2
a UserForm1
pops up and on click of CommandButton1
call same Macro1 like this:
Unload Me
Macro1
the Sheet2
worksheet is activated, sort of, but if I edit data in it, it actually updates corresponding cells in Sheet1, if I click on Sheet1
I can see data entered in there!
Clicking back to Sheet2
worksheet activates the Sheet2
sheet properly.
Has anyone seen such behaviour? If yes, are there any coding workarounds to properly activate Sheet2
?
Upvotes: 5
Views: 12240
Reputation: 1728
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.
Upvotes: 0
Reputation: 1
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
Upvotes: -1
Reputation: 29
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:-)
Upvotes: 2
Reputation: 589
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.
Upvotes: 1
Reputation: 13
I have the same problem, but my code is actually running in a Excel COM addin, which is slightly different from VBA.
My code worked in 2003,2010... using Sheets("Sheet2").Activate
, but not 2013
But I fixed the problem by starting a timer, 100 ms after my event code (Button
event).
Then this trimmer opens the file and uses Sheets("Sheet2").Activate
. It is then select, just the same as it used to be in old versions.
Upvotes: -1
Reputation: 21
I was able to reproduce the error in Excel 2013 as per Profex's answer. A workaround that solved the issue for me was to make my userform modeless.
userform1.show vbModeless
I understand that this workaround won't be helpful if you require a modal form, but hopefully it will save the next person some time.
Upvotes: 2
Reputation: 1390
Wow, I was able to reproduce this error using 2013. This is a fantastic failure of Excel's new SDI Interface. What is also interestingly odd is that when you follow the following steps
A1
on Sheet1
B2
on Sheet2
Button2
which opens the formCommandButton1
on the form (which hides the form and activates Sheet2
)it looks like cell B2
on Sheet2
is selected, but when you start typing, the text is going into cell A1
; Once you hit Enter the text disappears to cell B2
on Sheet1
You almost have to see it to believe it.
The only thing that I've found that works is using the Ontime
function.
Private Sub CommandButton1_Click()
Unload Me
'Call Macro1
Application.OnTime Now(), "Macro1"
End Sub
...but I have a feeling that this isn't going to help everyone
Upvotes: 3
Reputation: 149295
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
Upvotes: 0
Reputation: 1048
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
Upvotes: 0