Excel stops scrolling after a Copy command - vba

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

Related

ActiveX Combobox value not setting to linkedcell on another sheet on workbook_open

We have created a workbook which use ActiveX objects and macros to perform multiple actions (calculations, changing values in cells, forcing mandatory fields before save, colour changes, lock and unlock, etc). There are 3 sheets:
Lists - where we set the values for comboboxes, run mandatory checks against cells and comboboxes, etc. this is VeryHidden to the user
Form - this is where the user will enter their data. it contains all of the ActiveX objects
Import - this is a formatted sheet which we can import to our database with expected formatting on values (i.e. "inches" instead of "in.H2O#60F", days converted to hours). this sheet is VeryHidden.
This workbook can be downloaded blank from our website and imported back into the database successfully.
What we want to do is download a pre-populated version where "Form" shows data from the database. To do this we are populating the Import sheet with the values (as they are already linked to the comboboxes we need to populate). i.e. ComboA on "Form" is linked to cell A4 on "Import".
We expected the comboboxes with linked cells to the update when we downloaded and opened the pre-populated workbook. When it did not we tried:
Sheets("Import").EnableCalculation = False
Sheets("Import").EnableCalculation = True
Application.Calculate
to force the row to "recalculate" and therefore trigger the linked cell to work as when we manually went into one of the linked cells and pressed enter (or F9) the associated combobox updated.
This was partially successful in that is populated around 1/3 of the comboboxes but not all of them. There does not seem to be an order or data type causing the issue (some Y/N update, others do not. comboboxes A-D will update, then E-G will not, then H will).
We tried Application.CalculateFull as well as:
setting the linkedcells to Dirty
only applying .Calculate to the range on the worksheet
only applying .Calculate to the row on the worksheet (as it is all in one row)
We can manually force each combobox to populate with it's linked cell however given the number of comboboxes and the fact there will be more workbooks like this to build this is not an ideal solution.
We know it is hitting the workbook_Open() event as we put in some MsgBox items and the couple custom populations we do (converting degF to F then pushing to combobox) and that is working fine.
If we open the download, save it (skipping the mandatory fields before save), then reopen it everything populates/selects correctly (which is all the more frustrating).
Any thoughts on why the downloaded version is not acting as expected would be much appreciated.
given the other posts I have seen and the follow up information requested I though more was better. there is a code snippet there - and we found a solution to the issue. needed to add Application.Calculation = xlCalculationAutomatic to the open event before the code provided above. thanks anyways

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

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

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

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.

Hiding Page Breaks in Excel

Following on from my previous questions:
I have implemented a series of checkboxes with which users can hide/unhide ranges of data. This is great - it works. The problem is when the users go to print - the hidden data is hidden but the pages are still there but blank.
If the page breaks are left to their own devices then everything is fine - there are no blank sections. When manual page breaks are used then you can see where the data was.
I've tried everything to get rid of the blank areas.
Copying the ranges and recalculating the page breaks is a no-go as the page breaks take upwards of 2 minutes for a fairly small report.
So, how can I print only the ranges that aren't hidden?
Thanks, G.
Haven't tested it, but you could change the print range to only the visible cells:
Public Sub SetPrintRangeToVisible(ByRef ws As Excel.Worksheet)
ws.PageSetup.PrintArea = ws.UsedRange.SpecialCells(xlCellTypeVisible).Address
End Sub
Excel, by default prints only visible cells. You wouldnt have to set the Print area to visible cells as previously suggested.
I suppose you have un-checked the Print Object option in all of the Check-boxes. And that area appears blank, when you print the sheet. If that is the case, read on:
I suggest writing a simple macro that hides the rows that contain the check boxes. Set the visible property of the check boxes to false. Print the sheet, and then return everything back to the original state.
Alternatively, you could just select the rows above the check boxes, then the entire area below the check boxes, and then set that as the PrintArea. That should solve the problem you are having.
Also, I may have wildly misunderstood the situation, in which case, I am sorry I wasted your time.