Sheet.Activate changes sheet but continues to edit data on previous sheet - vba

I have a dialog box with a couple of buttons that launch macros to activate and change to different sheets.
The problem I am having is after I click the button, the macro activates the new sheet and I see it. But when I go to delete data, add data or try to delete a row "Nothing happens" the data on the screen is still there. If I go back to the previous sheet, the cells and rows that I had intended to delete were deleted in that sheet. It is very wierd and never seen anything like that. It appears that my macro code is note doing enough to actually change to the new sheet. I do not have this problem if I click a different sheet tab to change to it. Or if I click the dialog button to go to the new sheet and quickly do a ctrl-pgDown and Ctrl-PgUp to change from another tab and back that seems to fix the problem.
This is the code in my macro I am using to try to change to the desired tab.
Private Sub Report1Button_Click()
On Error GoTo Handler
Sheets("Report1").Activate
If StayOpenBox.Value = False Then
Unload MainMenu
End If
Exit Sub
Handler:
MsgBox "Sheet 'Report1' not found or renamed"
End Sub
Thanks for any help or suggestions
UPDATE:
Here is code that I use to call the dialog box. I have a shape on the other sheet that is assigned to this macro to open dialog box
Sub ShowMainMenu()
With UserForm1
.Show
End With
End Sub
Also there is no further code to make edits to the new sheet. My Button click simply switches to the other sheet and when I attempt to make edits manually, they are actually done on the previous sheet which is not the one I am currently looking at. So anything I do, Bold text, delete text, delete row, etc, is not done on the current sheet I am looking at, but when I return to the previous sheet the changes where made there. Im on Excel 2013, I have reproduced this problem in 2 separate files, but I will try on a different computer and older version of excel. Screenshot of my situation is below.
UPDATE 2:
I ran this xlsm file on a 2nd computer with Xls 2007 and was not having the problem. So I ran the macro on a 3rd computer that also has Excel 2013 and it is experiencing the same problem. So it is not computer specific and seems to be a problem in XLS 2013 but not in XLS 2007. I will try to find a computer with Excel 2010 to test as well, but something about this code is causing a problem in 2013 but not in older versions of excel.

When you run VBA code, it will default to using the ActiveSheet if you don't define the Sheet. When you have objects/methods that you want performed on a specific sheet, you should always specify! You can do that one of two ways:
Sheets("Report1").[Object].[Method]
'or
Sheets("Report1").[Method]
or you can pass the Sheet name to a variable and use that for shorter code
Dim Report1 As Worksheet
Set Report1 = Sheets("Report1")
Report1.[Object].[Method]
'or
Report1.[Method]

Try changing the sequence of lines in your code. I had a similar situation and it turned out that I inserted the "delete sheet" code in between the commands that were copying data from sheet1 to sheet2. When I put the deletion lines (commands) after I finished copying sheets everything started to work correctly. Many commands activate one sheet while performing and this immediately disactivates another sheet. So if you used "ActiveSheet" sommand somewhere it may be incorrectly understood - the command may be executed not on the sheet you meant.

Just use the full address for the range you are trying to manipulate, for example instead of:
Sheets("mySheet").Activate
Range("A1:B10").Cut
Sheets("myOtherSheet").Activate
Range("A1:B10").Paste
use:
Sheets("mySheet").Range("A1:A10").Cut Destination:=Sheets("myOtherSheet").Range("A1:B10")

I just had the same problem, also with Excel 2013. So even if the thread is over 9 month inactive, I want to share my solution in case somebody gets here through a Google search.
The solution was really simple. Call the userform with:
UserForm1.show vbModeless

Related

Excel stops scrolling after a Copy command

I have a set of data entries in the 2nd Worksheet of my Workbook, which are part of a list using Filters. Using VBA I have created a button that when clicked, opens a Userform which lets the user enter their desired data selection to be copied from the 2nd Worksheet to the 1st Worksheet. This is done by having the user checking 1 out of 8 possible OptionButtons and then having the user select 1 of 5 possible options from a ComboBox. All is well and every possible combination in the Userform inputs leads to the correct data being copied from the 2nd Worksheet in to the 1st Worksheet. However, after a successful copy, I am unable to scroll using my scroll wheel in my 1st Worksheet. The "square" moves in the scrollbar when scrolling with the scroll wheel, however the worksheet does not move.
The main differences between the two Worksheets are that in the 1st Worksheet, no filters are present and that the copied selection is always less data then the source data, however this isn't to big of a difference (source data in 2nd worksheet is about 300 rows, smallest copy possibility is around 12 rows). I've searched and tried the following solutions, with the accompanying results.
Unfreeze Panes; One solution suggested was to unfreeze panes as Excel would possibly freeze all rows after the copy. However, when checked there was no "Unfreeze" option, indicating that I had no frozen panes to begin with. Also, when I clicked any of the three "Freeze" options, thinking I would try to manually "Freeze" and then "Unfreeze" to resolve the issue, Excel would stop working, giving me the "Not Responding" in the title bar and needed to be shut down/restarted.
Select Objects; Another suggestion was that Excel would have the "Select Objects" checked in the "Find & Select" portion of the "Home" tab. When un-checking this, the issue should be resolved. However, it wasn't checked to begin with. Checking it and then un-checking it did not resolve the problem.
Switch Worksheets; When I manually click the 2nd Worksheet after a copy command and then click back to the 1st Worksheet, the problem is resolved and I can use the scroll wheel again. However, I intend to use this Workbook a lot and having to constantly click back and forth is going to be a pain. So this isn't really a solution, more of a work around.
Dragging; Another work around I have found is that clicking and then dragging the "square" in the scrollbar does move the worksheet around. However, this is again just a temporarily fix.
One more thing that may be useful to mention. In both Worksheet 1 & 2 I am using collapsible columns. Worksheet 2 also contains collapsible rows, where Worksheet 1 does not.
Okay, so on a whim I tried to enter the following 2 lines of code to the end of the sub
ThisWorkbook.Sheets("Overview").Activate 'Activates the 2nd Worksheet
ThisWorkbook.Sheets("Selection").Activate 'Activates the 1st Worksheet
This mimics the manually triggering of switching Worksheets. And it resolved the problem. Still don't understand why the problem occurred but it's solved anyway.
I had a similar issue and my initial workaround was to make the userForm modeless:
Sub addButton()
builderForm.Show (0)
End Sub
I wasn't a huge fan of leaving it modeless, so I ended up using your sheet toggling method like this:
Sub addButton()
builderForm.Show
Sheets("Cover").Activate
Sheets(Worksheets.count - 10).Activate
End Sub
I still don't fully understand why it is happening; it's almost like windows/excel loses track of which sheet is the active one while the userForm is running.
Thanks for your submission/answer!
This also happens when a worksheet has too many copied Conditional Formatting rules. Once I cleaned up my extra/duplicated rules, I could scroll again after copying a cell.
Found good info about copying/pasting with or without the Conditional Formats here https://superuser.com/questions/419287/how-to-copy-paste-without-conditional-formatting

Excel 2016 Conundrum: "Activate method of range class failed" error when trying to set focus to a cell in another workbook

I have a form containing a treeview control listing errors found in another excel workbook. When an error is clicked in the treeview, I want to move the cursor to the appropriate cell in the other workbook, so that the user can correct the value of that cell. I have the following code:
Set wsNavigate = Worksheets(TargetWorksheet)
' Activate workbook
TargetWorkbook.Activate
' Activate worksheet
wsNavigate.Activate
' Activate and select cell
wsNavigate.Cells(cDestination.row, cDestination.column).Activate
wsNavigate.Cells(cDestination.row, cDestination.column).Select
This code works fine, if all Excel windows are maximized and on the same screen. However, if I move the form window to my other screen or simply restore down my Excel windows, then it only works the first time a link is clicked, after that I get an "Activate method of range class failed" error. The weird thing is, if I then go into debug mode, it works just fine.
This problem started occurring, when we started using Excel 2016 with the Single Document Interface. In older versions, the code worked fine.
I've searched and tried many different things, however, so far I have not been able to come up with a solution yet. It would be great, if someone could help.
After much research, the only solution to the problem that I could find, was to move the cursor to another sheet briefly before moving it to the desired cell. It's not pretty, but at least it works. Here is the code (new parts in bold):
Set wsNavigate = Worksheets(TargetWorksheet)
**Set wsTemp = Worksheets(SomeOtherWorksheet)**
' First activate other sheet
**wsTemp .Activate**
' Activate destination worksheet
wsNavigate.Activate
' Activate and select cell
wsNavigate.Cells(cDestination.row, cDestination.column).Activate
wsNavigate.Cells(cDestination.row, cDestination.column).Select

vba , Autofill userform from another workbook cell selection

I have a workbook that used to be one with many sheets, I have now split the sheet to different workbooks.
The problem I now have is userform population from cell selection. When all the sheets were together. This code worked great.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not (UFMJobSelectForm.ActiveControl Is Nothing) Then
Call UpdateJobSelectForm
End If
End Sub
However now the Userform is in one workbook and this code is in another. I don't want to reference library as I need it to open and close so it can be accessed for other people to use.
Thanks for any help in advance.
Edit:
What i have is 4 different workbooks with jobs on and i want to select the job and the userform populates with job details.
The code I have detects the userform is open and then calls then populates the userform using updatejobselectform. Which worked when all the sheets were in the same workbook. however no longer works now i have separated them.
When i run this code now the sheets are in there own work books i get Error: runtime erroe 424 object required.
So what i am asking is dose anyone know how i can check a userform is loaded from in a different workbook and how i can get the useform to interact with cell selection from a different workbook.
thanks again.
This looks like a code that primafacie must fire when selection change event occurs, and call a function that must update the said form.
If the code and the said form are dependent, while splitting up should they not be together.
In case there are some other constraints that do not let you do so, and are not defined in the question do feel free to update the question / comment below.
Happy to help!

Hyperlink doesn't activates macro in VBA

I'm trying to get a hyperlink to activate a macro. I can't use a fixed target address, because I've several links; The goal is to create a clickable history of taken step. So I need to create a back button.
I already tried this:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If Target.Range.Address = "$A$4" Then
MsgBox "This isn't what I had in mind"
Else
MsgBox "MACRO!"
Exit Sub
End If
End Sub
Anybody an idea?
If I click the hyperlink it only show the code for that cell.
The code is in the Worksheet module.
The general idea:
I'm making a workbook that calculates based on several steps (40 in total). For example: if sum is larger than 9 go to step 4, if not, go to step 21.
But because we're all human and mistakes can be made, i want excel to make a list of all the steps that have been taken and their answer. I want to make the steps in this list clickable, so when they click on the step, the can go back to that step and continue from there. So a lot of hyperlinks :)
Why not use Worksheet_SelectionChange with the same setup?
This seems to work for me, I've copy and pasted your code into the "Sheet1" module of a new Excel workbook, and put a hyperlink on the 'Sheet1' tab to click on. The MsgBox pops up as expected.
I believe the macro will only work if the code is in the Sheet module for the Worksheet that the users is clicking the link on.
If you need a Hyperlink Back button, you can always add one to the Quick Access Toolbar:
This Macro doesn't work if you use =HYPERLINK(), you have to convert it via 'rightclick', Hyperlink.

Click to toggle sheet tab

I am trying to use VBA to create the same action you would do if you had an Excel spreadsheet open with 3 sheet and you want to toggle between sheets exactly like when you click on the sheet tab at the bottom of the workbook (i.e. Toggle between Sheet1, Sheet2, Sheet3 by clicking on the sheet tabs)
I've tried some thing:
book.Worksheets(1).Activate = True
book.Worksheets(1).Activate
sheet.Visible = True
But nothing seems to work. I'm not sure Activate is what I want. I think that just makes the sheet active, but not visible. Visible seems to do nothing. How can this be accomplished?
book.Worksheets(1).Activate
Is designed to perform the action you want "clicking on a tab"
You can also try
book.Worksheets(1).Select
Which can be used to select multiple tabs (see Have a look at What is the difference between Sheets.Select and Sheets.Activate?)
I am guessing something is not right either with your workbook reference or something else in your code. If you make a new excel file and just try Sheets("Sheet2").Activate it will select that tab.
thanks for the feedback. I have been attempting to use com objects in python. they usually interop with each other quite well, but this is one thing that doesn't seem to work. I've tried all of your suggestions with no results.
I managed to find a workaround by just moving the sheets around using:
sheet.Move(Before=book.Worksheets(1))
For some reason, when the sheet is moved, it also activates it which is my desired end result. It works for the scenario I am scripting, but may not for other people trying to do something similar.