want to use a Spin button (ActiveX Control) to show a previous or next sheet. When the button is clicked the event is successfully triggered and the desired sheet is activated but it holds some elements (commands, graphs, etc.,) from the original sheet and shows these as an appending picture.
Sample code for Down button event :
Private Sub SpinButton_JumpToWeek_SpinDown()
Dim sh_num As String
Dim tmp_num As Integer
Application.ScreenUpdating = False
Application.EnableEvents = False
SpinButton_JumpToWeek.Value = Range("B27").Value - 1
tmp_num = SpinButton_JumpToWeek.Value
' Activate desired KTx sheet
sh_num = "KT" & tmp_num
Range("F27").Value = "" 'reset to blank
Sheets(sh_num).Activate
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
To override this effect I have to manually select (activate) another sheet and then again select the desired sheet. I tried also to automatize this workaround with a macro, but unfortunately it does not work.
It is interesting that this problem do not occur if I execute code in Debug mode (using breakpoint and the stepping line by line).
Surprisingly, I do not have such problem if I try to show the previous/next sheet by writing a value (sheet name index) into a defined cell (i.e. using the Worksheet_Change event). The desired page is correctly shown. See photos.
Sample code for this evententer image description here:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim sh_num As String
Application.ScreenUpdating = False
Application.EnableEvents = False
If Range("F27").Value > 0 Then
' Activate desired KTx sheet
sh_num = "KT" & Range("F27").Value
Range("F27").Value = "" 'reset to blank
Sheets(sh_num).Activate
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
I need to use the Spin button because it is faster and allow me to skip some sheets (for example if these do not have data).
The problem occurs in both 32 and 64-bit Excel.
Do somebody an idea why this problem is occurring and a workaround? Do I have to change some settings or system properties in Excel? Any help would be highly appreciated.
#mehow
I append my commenst here due to large number of words.
I followed your suggestion and tried the example of a UserForm with inserted active control “Microsoft Office Spreadsheet”. I found out that this would not be a good solution for my case, because the response of such sheet is relatively slow (even on a fast PC like mine) when user inserts values into cells. Furthermore, this would greatly complicate my fairly simple *.xlsm workbook, which has more than 50 sheets (1 sheet for each week, the contents of these sheets are then linked to a main sheet), and completely meets my basic necessities (with the exception of this spin button of course).
In my opinion there is probably necessary to manipulate some kind of system property (like for e.g. Application.ScreenUpdating trick), but I do not have enough VBA knowledge to find it.
To clearly explain my question I would need to share my sample workbook, but I do not know how to upload it to this forum. Unfortunately, I am not able upload/show images successfully on this forum (due to my low rating), which would also help a lot.
BTW, I cannot see images on other questions of this forum, too. . Could this problem occur due to missing plugins in a web browser?
You can use a simple trick... before the "Application.screenupdating = true" you can insert the two lines:
ActiveWindow.SmallScroll Down:=-100
ActiveWindow.SmallScroll Up:=100
Related
I've found a lot of questions and answers about issues that feel very close to what I'm working on, but not quite. I have an Excel workbook with a large Data Model connected to two slicers. I need to cycle through every entry in the slicer, allow the workbook to catch up on loading a large number of cube formulas, then copy one particular worksheet over into another.
I've written VBA which does all of this, but I can't for the life of me get the VBA to wait for the workbook to finish uploading before it continues with the rest of the script. I can rule out background refresh-based solutions, which don't apply to OLAP. Various solutions I've found online which recommend waiting for calculations to be complete don't seem to work, the script just barrels right through those lines. The only solution I've seen which seems to apply here involved identifying every cell which would be updated as a result of the slicer change and looping through them until they no longer say #GETTING DATA. For my workbook, this would be hundreds of cells to identify and check and feels very unsustainable.
Even telling the script to Applcation.Wait seems to wait for the selected amount of time during which the workbook pauses getting data.
Setting different values of a slicer connected to a Data Model and automating some output feels like it should be such a common task that we have a solution for it. Any help would be much appreciated.
Running Office 365
Sub generate_all_forecasts()
'Cycle through all products and push forecast values to fcst_output'
Application.ScreenUpdating = True
Dim SC_products As SlicerCache
Dim selection, product_array As Variant
Dim push As Boolean
Set SC_products = ThisWorkbook.SlicerCaches("Slicer_PRODUCT_GROUPING_WRITTEN") 'The product slicer on the Inputs worksheet'
product_array = Range("product_array") 'Named range product_array on Tbl_Codes worksheet'
For Each p In product_array 'For each product'
push = WorksheetFunction.Index(Range("fcst_push_array"), WorksheetFunction.Match(p, product_array, 0)) 'Check if the product has been selected for this run'
If push = True Then
If p = "Major Medical Plan" Then 'If "Major Medical" '
selection = Array("[Query1 1].[PRODUCT_GROUPING_WRITTEN].&[Major Medical Plan - CMM]", _
"[Query1 1].[PRODUCT_GROUPING_WRITTEN].&[Major Medical Plan - GMM]") 'selection will be both CMM and GMM'
Else
selection = Array("[Query1 1].[PRODUCT_GROUPING_WRITTEN].&[" & p & "]") 'Otherwse selection is the single product'
End If
SC_products.VisibleSlicerItemsList = selection 'Change slicer to current selection'
'This is where the script needs to pause until #GETTING DATA is complete'
Application.Run "push_to_output" 'Run the forecast update macro'
End If
Next p
Worksheets("Fcst_Output").Range("B2:B1381").Value = "" 'Clear prior month's comments'
Application.ScreenUpdating = True
End Sub
Solutions which have not worked: Wait time after change slicer in
power pivot, Getting vba to wait before proceeding, Wait
until Application.Calculate has finished
The "solution" I really don't want to use: Force VBA to wait until
power pivot finishes refreshing
Thanks to Tragamor for linking to a thread where they already had a working answer. I included the following immediately after the slicer selection in my VBA and it appears to properly wait until all data fetching is complete:
Dim CalculationState As Long
With Application
CalculationState = .Calculation
.Calculation = xlCalculationAutomatic
.CalculateUntilAsyncQueriesDone
Do Until .CalculationState = xlDone
DoEvents
Loop
.Calculation = CalculationState
End With
Find your Query Properties, and set 'Enable background refresh' to False (Microsoft use True as default).
Then in your code you need to call for RefreshAll and wait for the data to load with the DoEvents. If you want your update of data to run on open you can use this in 'ThisWorkbook' Object.
Private Sub Workbook_Open()
For Each q In ThisWorkbook.Connections
q.Refresh
DoEvents
Next
End Sub
I ran across a question in a facebook group which asked how to expand/enlarge/zoom the active cell in an Excel worksheet.
Normally, I don't like to mess with the Excel UI visually but I guess that the person must have some form of visual impairment or something which requires a clear and bigger view of the contents of the active cell.
I searched in stackoverflow and googled and also in the similar questions box which doesn't show the exact same answer I was searching.
I believe that there are multiple possible approaches to this question.
1.to change the rowHeight and columnWidth of the activecell.
Application.ActiveCell.RowHeight=50
Application.ActiveCell.ColumnWidth=50
2.to change the autofit of the column containing activecell.
Application.ActiveCell.EntireColumn.AutoFit
3.to change the zoom level of activewindow.
ActiveWindow.Zoom 50
4.to assign the activecell contents into a textbox.text property on a modeless userform.
I think methods 1 & 4 are most likely to work and personally, I prefer method4 because it seems less likely to visually disturb the user.
Workbook_SheetSelectionChange "event will be used
Allow me to answer to my own question with the following VBA code.
Please feel free to improve upon this.
'copy paste into ThisWorkbook module
'explicit error checking was not performed - use at users' own risk
Option Explicit
Const increasedColumnWidth = 50'change how large as per requirement
Const increasedRowHeight = 50
'saved properties to restore later
Private saved_ActiveCell_ColumnWidth As Integer
Private saved_ActiveCell_RowHeight As Integer
Private saved_ActiveCell As Range
'can also be placed into individual worksheet modules but used ThisWorkbook to cover newly inserted sheets
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
'very important that sh must be a worksheet<-not checked here
' If Sh.Name = "Sheet1" Then 'set sheet name here to limit to Sheet1 only or any particular sheet
If Target.Cells.CountLarge = 1 Then 'if .Count, there may be overflow
Application.ScreenUpdating = False 'to reduce flashing
If Target.Value <> "" Then 'or isempty(target.value)
'restoring previous activecell if there's one
If Not saved_ActiveCell Is Nothing Then
saved_ActiveCell.EntireColumn.ColumnWidth = saved_ActiveCell_ColumnWidth
saved_ActiveCell.EntireRow.RowHeight = saved_ActiveCell_RowHeight
End If
'backup
Set saved_ActiveCell = Target 'Application.ActiveCell
saved_ActiveCell_ColumnWidth = Target.ColumnWidth
saved_ActiveCell_RowHeight = Target.RowHeight
'expanding
Target.EntireRow.RowHeight = increasedRowHeight
Target.EntireColumn.ColumnWidth = increasedColumnWidth
Else'if activecell doesn't contain any value, restore previous activecell to previous size
If Not saved_ActiveCell Is Nothing Then
saved_ActiveCell.EntireColumn.ColumnWidth = saved_ActiveCell_ColumnWidth
saved_ActiveCell.EntireRow.RowHeight = saved_ActiveCell_RowHeight
End If
End If
Application.ScreenUpdating = True
End If
' End If
End Sub
Sharing this code doesn't mean that I support messing around with Excel UI manipulation especially those that would disturb the user.
The provided code is just for changing activecell's rowHeight and columnWidth to give the user the visual effect that the activecell is expanded/enlarged.
I've seen other code that would create an image of activecell on-the-fly and gives the user the impression of a zoomed cell here.
The code shared herewith is only for manipulating the rowHeight and columnWidth of the ActiveCell.
The method dealing with a textbox on a modeless userform is still getting improvements and will eventually be available(probably) on my GitHub as a .xlsm file.
Also uploaded a .gif below to clearly display the requirements and the results.
This video .gif contains features not available in the code shared here.
this project is certainly testing my limits in VBA. I have yet another question. In this workbook, I have a button called "Add Projects" that takes a different sheet "Project Templates" and uses it to create a new sheet called "Add Projects". I'm trying to add some specific cell locking when this sheet is created. The idea is that users can alter certain areas, but not ones that have formulas or are auto-populated by an entry in a different cell.
The problem is, when I add the code, certain ones lock appropriately, one particular cell does a popup asking for a password (when it should just be locked like the others), and some let you still edit them despite them being included in the locked code.
I've checked the cell format, and the box for locked is selected. I've also gone to the Allow Users to Edit Ranges and removed everything from there (although those ranges weren't in there). I've moved the order of the ranges around, thinking maybe they were somehow overwriting each other even though I have a distinct cell set as the end range.
Public Sub Open_AddProjectSheet()
Application.ScreenUpdating = False
ActiveWorkbook.Unprotect Password:="Password"
Worksheets("ProjTemplate").Visible = True
If WorksheetExists("AddProject") = False Then
GetTSResourceInfo
GetTSProjectID
ActiveWorkbook.Unprotect Password:="Password"
Worksheets("ProjTemplate").Copy After:=Worksheets("ProjTemplate")
ActiveSheet.Name = "AddProject"
Worksheets("ProjTemplate").Visible = xlSheetVeryHidden
Sheets("AddProject").Activate
ActiveSheet.Unprotect Password:="Password"
ActiveSheet.Cells.Locked = False
ActiveSheet.Range("E5,C6,L2,L6").Locked = True
ActiveSheet.Range("E11:E190").Locked = True
ActiveSheet.Range("G11:H190").Locked = True
ActiveSheet.Range("K2:K6").Locked = True
ActiveSheet.Range("L11:R190").Locked = True
ActiveSheet.Range("L3:L5").Locked = False
ActiveSheet.Protect Password:="Password",UserInterFaceOnly:=True,Contents:=True
Else
answer = MsgBox("'AddProject' sheet already active. Would you like to pick up where you left? WARNING: Selecting 'No' will close the existing sheet and open a new one, any progress will not be saved.", vbYesNo)
If answer = 6 Then
Worksheets("AddProject").Activate
Else
*Same code as above*
End If
End If
Worksheets("ProjTemplate").Visible = xlSheetVeryHidden
ActiveWorkbook.Protect Password:="Password"
Application.ScreenUpdating = True
End Sub
When running this code, E5,C6,K2-K6,L2,L11-R190 are all locked correctly. L6 brings up a popup asking for a password to change this cell. All other ranges, E11-E190, G11-H190 are editable even though they should be locked.
Any ideas I'm greatly appreciative of. Thanks!
So, even though it didn't work before, we went back and tried to alter the Allow Users to Edit Ranges. Wiped all of them out again and the cell locking started working. I'm not going to question why it worked vs not working the last time it was tried, just glad it's working! That being said, the speed being different is still an issue.
I have a worksheet_change macro embedded in two sheets within my workbook. They are there to prevent anyone making changes to the sheets. However, I still want the data within the sheets to be refreshed every so often. This does not work.
Two sheets within the workbook are connected via a query to another workbook. Essentially those sheets are a copy of the sheets within the other workbook.
I have embedded Code1 into the two worksheets. This is to prevent anyone making changes to the worksheet but still allow them to view the sheet and copy data from it. It brings up an message box and then undoes the change made by the user. This works fine and I am happy with it.
At the same time I want to be able to refresh the workbook so that the connected sheets are up to date with respect to the other workbook that they are connected to.
To do this I have added a button into the workbook called "Refresh". This button calls Code2. This was done with the intention of disabling events so that the worksheet_change macro is paused to allow for the data to be refreshed.
However, this does not work as the worksheet_change macro still works. I.e after clicking the button, the workbook is refreshed and then any update is undone and the message box is displayed - which isn't what I need.
CODE1
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("A1:Z1000")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
With Application
.EnableEvents = False
.Undo
.EnableEvents = True
End With
' Display a message when one of the designated cells has been
' changed.
' Place your code here.
MsgBox "DO NOT MODIFY THIS SHEET - Any necessary modifications should be made in 'Master Invoice Template' and this sheet will automatically be updated!"
End If
End Sub
CODE2
Sub refresh()
On Error GoTo ErrorHandler
Application.EnableEvents = False
ThisWorkbook.RefreshAll
ErrorHandler:
Application.EnableEvents = True
End Sub
I have scoured the internet for a solution and pretty much everything that I find points me in the direction of enableevents=false, but as described in my post this does not work. Do I need to change the method of solving my problem or am I doing something wrong within my code?
I suspect the undo line of code is causing the problem, but I am not sure!
Any help would be greatly appreciated!
I think I have figured out what was wrong with the code; correct me if I am wrong. The data was taking too long to refresh when Code2 was ran. This meant that the Application.EnableEvents = Ture in Code2 took effect before the data could be fully refreshed and when it finally did complete its update, the Worksheet_Change event was triggered.
I tried using DoEvents after the RefreshAll command but this didn't work either. I have used what I found in this post to work around the problem and the refresh button now works!
Specifically the code that helped is below: I replaced Code2 with this:
Sub Refresh_All_Data_Connections()
For Each objConnection In ThisWorkbook.Connections
'Get current background-refresh value
bBackground = objConnection.OLEDBConnection.BackgroundQuery
'Temporarily disable background-refresh
objConnection.OLEDBConnection.BackgroundQuery = False
'Refresh this connection
objConnection.Refresh
'Set background-refresh value back to original value
objConnection.OLEDBConnection.BackgroundQuery = bBackground
Next
MsgBox "Finished refreshing all data connections"
End Sub
Please let me know if my logic in explaining why the code didn't work is correct - I am still new to VBA and would like to understand the problem fully!
I have an excel sheet that became very slow.
For some reason, wherever I am in the document, if I set Calculation to manual and refresh the current sheet after any change, it is fast enough and serves my purpose.
This is not very comfortable however.
I would like the current sheet (and not the whole document) to be refreshed whenever a cell is changed. This should be done whatever sheet I'm on. How can I do that?
EDIT: let it be clear that I'm not asking for clues on how to make my workbook faster, it was just contextual info. I'm interested in autorefresh only.
Charles Williams has extensive information, techniques and code on calculation on his website http://decisionmodels.com. Quoting from this page:
Another method uses the worksheet.enablecalculation property. When
this property is changed all the formulae on the worksheet are flagged
as uncalculated, so toggling the property to false and then back to
true for all sheets in all open workbooks will cause the next
calculation to be a full calculation.
> Dim oSht as worksheet Application.Calculation=xlCalculationManual
>
> for each oSht in Worksheets oSht.enablecalculation=false
> osht.enablecalculation=true next osht
>
> Application.calculate
You can also use this method for a single worksheet to do a full sheet
calculate.
You can easily work this technique into the Worsheet_Change event, like this:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.Calculation = xlCalculationManual
Me.EnableCalculation = False
Me.EnableCalculation = True
Me.Calculate
End Sub
This will do a full calculation of the current sheet only and leave all other sheets uncalculated.
I believe the Calculation event can happen at the Application, Worksheet or Range level (but not the Workbook level.)
If you have calculation mode set to manual but want the current worksheet (only) updated when you enter something, you could add a worksheet change event (putting the code in the relevant sheet.)
Private Sub Worksheet_Change(ByVal ChangedRng As Range)
ChangedRng.Worksheet.Calculate
End Sub
This will recalculate just the worksheet and not the rest of the Workbook or any other workbooks.
As the provided answers don't fully satisfy me here is my own, borrowing from their appreciated help. It's possible that I've missed some nuance from teylyn's answer. I'll gladly edit if it's relevant.
There doesn't seem to be a way to handle change events at the APPLICATION level.
So all changes must be handled at the SHEET level.
This can be done by copying and pasting this bit of code in EVERY Sheet object of the workbook (and remember to copy and paste it it whenever you add a new sheet):
Private Sub worksheet_change(ByVal Target As Range)
Me.Calculate
End Sub
However because of the tedious copy pastes we had to do, we may want to turn this feature off easily without going back to each sheet object.
We may also want to avoid entering a loop of hell if we do mass change on one sheet (say through a macro, a drag & drop, or a ctrl+H).
To do this we add a new sheet called WS_refresh where we set 3 values :
in A2: Yes/No to enable/disable our new feature
in A4: time of latest refresh
in A6: min interval to allow refresh, in sec (I've set it to 1)
Now when we change values in several cells in a short amount on time, autorefresh will only work on the first change, avoiding previously mentioned loops of hell. If you like to live dangerously set A6 to 0.
This is what should be copied and pasted in every sheet object:
Private Sub worksheet_change(ByVal Target As Range)
auto_refresh = ThisWorkbook.Worksheets("WS_refresh").Range("A2")
If auto_refresh = "Yes" Then
last_refresh = ThisWorkbook.Worksheets("WS_refresh").Range("A4")
refresh_interval_sec = ThisWorkbook.Worksheets("WS_refresh").Range("A6")
refresh_interval_tv = TimeValue("0:00:" & refresh_interval_sec)
If Application.Calculation = xlCalculationManual And Now() > last_refresh + refresh_interval_tv Then
ThisWorkbook.Worksheets("WS_refresh").Range("A4") = Now()
Me.Calculate
End If
End If
End Sub