My pages load slowly. Code needs to be faster any suggestions? - excel

Background: This project is gets loaded into full screen maximized.To make it look good I took out the tabs, formula bar, headers and status bar. I created my own buttons customized from shapes. These do have some very attractive colors and 3D views and I am wondering it is my code or the shapes that cause it to load slowly. When I Click a button to change pages is when this happens. Initially I had the button macros simply changing the pages but I thought it was slow because i was using select code. worksheet.Activate and worksheets.range.select. And the code was in the worksheet private sub under the Activate event. Then I took the code from there removed the select criteria and it almost feels slower. Is there anything I can do to pick up speed? Thank you.
This Example shows how the buttons are positioned up top of each worksheet. There are 10 sheets with these buttons which are shapes.
Example of 1 worksheet with Navigation Buttons on top
Here is the code im using now. This code is in public subs for a general module.
Sub StartPg()
'ChangePagesmacro
With wsStart.Application 'initiate page loading
.Calculation = xlCalculationManual 'turn off calculations
.ScreenUpdating = False 'turn off screen updates
.EnableEvents = False 'disable events
.Goto Reference:=wsStart.Range("A4"), Scroll:=True 'page starting point
With .ActiveWindow
If .FreezePanes Then .FreezePanes = False 'unfreeze panes to remove any jam up`enter code here`
.SplitColumn = 0 'unsplit column to free up screen
.SplitRow = 0 'unsplit row to free up screen
.WindowState = xlMaximized 'maximize window
.DisplayHeadings = False 'turn of page headings
.DisplayFormulas = False ' hide formula bar
.DisplayWorkbookTabs = False 'hide page tabs
.DisplayGridlines = False 'hide gridlines
.DisplayHorizontalScrollBar = True 'show scrollbar
.DisplayVerticalScrollBar = True 'show scrollbar
.Zoom = 81 'zoom and area
.ScrollColumn = 1 'scroll to top
.ScrollRow = 1 'scroll to left
.SplitColumn = 0 'split colum to be frozen
.SplitRow = 3 'split row to be frozen
.FreezePanes = True 'freeze panes
End With
End With
With wsStart
.ScrollArea = "SAStart" 'set user scroll area
With .Application
.Calculation = xlCalculationAutomatic 'turn on calculations
.ScreenUpdating = True ' turn on screen updates
.EnableEvents = True ' turn on events
.DisplayFormulaBar = False 'No formula bar
.DisplayStatusBar = False 'No status bar
.DisplayFullScreen = True ' show full screen
End With
End With
End Sub

Related

Rerun a macro when a combobox value in a userform is changed

I have a userform with a combobox to select the training type for various employee classes.
When the user selects one of the options from the dropdown menu it runs the macro below.
Private Sub TrainingType_Selection_Change()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Dim TrainingType_Selection As String
TrainingType_Selection = Hiring_Validation_Form.TrainingType_Selection.Value
If TrainingType_Selection = "Sustain - 30" Or TrainingType_Selection = "Sustain - 60" Then
Hiring_Validation_Form.LinkingECPID_Selection.Visible = True
Hiring_Validation_Form.LinkingECP_Label.Visible = True
End If
If TrainingType_Selection <> "New Hire" Then
Hiring_Validation_Form.ReqReasonLv1_Selection.Visible = False
Hiring_Validation_Form.Label6.Visible = False
Hiring_Validation_Form.ReqReasonLv2_Selection.Visible = False
Hiring_Validation_Form.Label11.Visible = False
End If
End Sub
The problem I'm running into is that if someone makes a selection from the drop-down menu and then changes their mind and selects another value from the drop-down menu it isn't re-running the macro. For example they change it from the above "New Hire" to "Sustain - 30". I have a clear button on the userform, but that clears the entire form which would not be ideal in a situation where the user only wants to change one input, not completely start over.
How do I get the TrainingType_Selection_Change() macro to re-run again when the combobox selection is changed
This line:
Application.ScreenUpdating = False
disables screen updates even after the macro has finished running.
Add
Application.ScreenUpdating = True
as the last line before End Sub to re-enable them.
I was able to figure out a way to address this issue by using AfterUpdate()
I added the below macro and it now updates the entire form when selections are changed.
Private Sub TrainingType_Selection_AfterUpdate()
TrainingType_Selection = Hiring_Validation_Form.TrainingType_Selection.Value
Hiring_Validation_Form.LinkingECPID_Selection.Visible = False
Hiring_Validation_Form.LinkingECP_Label.Visible = False
Hiring_Validation_Form.ReqReasonLv1_Selection.Visible = True
Hiring_Validation_Form.Label6.Visible = True
Hiring_Validation_Form.ReqReasonLv2_Selection.Visible = True
Hiring_Validation_Form.Label11.Visible = True
Hiring_Validation_Form.Label21.Visible = True
Hiring_Validation_Form.NewHireSup_Selection.Visible = True
Call TrainingType_Selection_Change
End Sub

Userform button stays pressed after command

I have Userform with buttons that are connected to codes. Why buttons keeps being pressed after code has been completed? Is there any way to release them?
Button looks like this after clicking on it:
Button code:
Private Sub ToggleButton2_Click()
ThisWorkbook.Worksheets("Price calculation").Activate
Application.ScreenUpdating = False
ThisWorkbook.Sheets("Price calculation").Unprotect Password:="123"
Range("1867:1979").EntireRow.Hidden = False
ActiveSheet.Shapes("Rectangle: Rounded Corners 76").Visible = False
ActiveSheet.Shapes("Rectangle: Rounded Corners 244").Visible = True
ActiveWindow.ScrollRow = 1867
ThisWorkbook.Sheets("Price calculation").Protect Password:="123"
Application.ScreenUpdating = True
End Sub
You're using a toggle button. These toggle, unsurprisingly. I reckon you are looking to use a CommandButton instead.
In the toolbox browse for the CommandButton control Replace the ToggleButton with the newly added CommandButton. Then, edit the code for your togglebutton and replace it with:
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
With ThisWorkbook.Sheets("Price calculation")
.Activate
.Unprotect Password:="123"
.Range("1867:1979").EntireRow.Hidden = False
.Shapes("Rectangle: Rounded Corners 76").Visible = False
.Shapes("Rectangle: Rounded Corners 244").Visible = True
ActiveWindow.ScrollRow = 1867 'Is this necessary? It scrolls to a specific hard-coded row
.Protect Password:="123"
End With
Application.ScreenUpdating = True
End Sub

How can I make this code more efficient so that it runs quicker?

I am attempting to hide rows so that only a certain retailers data is shown, the data is not filterable due to the layout of the report. I start by just unhiding all rows as a reset and then manually hide rows that aren't relevant to a retailer until only the clicked retailers info remains.
However this is a slow way of doing this, and I need a quicker way I can understand. There is no criteria to filter the data. Just the retailer name on a click button.
My code shows the manual slow way of doing this.
Sub SummaryRetailer1Only()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'Resets hidden rows by showing everything.
ActiveSheet.Rows("2:480").EntireRow.Hidden = False
'Hides all rows that don't show data for Retailer1.
ActiveSheet.Rows("18:21").EntireRow.Hidden = True
ActiveSheet.Rows("37:48").EntireRow.Hidden = True
ActiveSheet.Rows("54:57").EntireRow.Hidden = True
ActiveSheet.Rows("73:84").EntireRow.Hidden = True
ActiveSheet.Rows("88:129").EntireRow.Hidden = True
ActiveSheet.Rows("261:376").EntireRow.Hidden = True
ActiveSheet.Rows("390:393").EntireRow.Hidden = True
ActiveSheet.Rows("409:420").EntireRow.Hidden = True
ActiveSheet.Rows("424:427").EntireRow.Hidden = True
ActiveSheet.Rows("443:454").EntireRow.Hidden = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
The code works fine I just want a way that I assume uses some variables so that it runs quicker.
Another way:
Option Explicit
Sub SummaryRetailer1Only()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With ThisWorkbook.Worksheets("Sheet1") '<- It s better to create a with statement with the sheet you want to use insead of activesheet
'Resets hidden rows by showing everything.
.Rows("2:480").EntireRow.Hidden = False
'Hides all rows that don't show data for Retailer1.
Union(.Rows("18:21"), .Rows("37:48"), .Rows("54:57"), .Rows("73:84"), .Rows("88:129"), .Rows("261:376"), _
.Rows("390:393"), .Rows("409:420"), .Rows("424:427"), .Rows("443:454")).EntireRow.Hidden = True
End With
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

Macro to Freeze Panes (Top Row) Not Working

I recorded a macro to freeze panes, but it is not working as intended. It freezes at the 2nd row and makes the top row hidden. Help. I am using Excel 2007. The code is below:
' Freeze Pains - Top Row
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
ActiveWindow.FreezePanes = True
I found this code works perfectly:
Sub Freeze_Top_Panes()
Application.ScreenUpdating = False
Rows("2:2").Select
ActiveWindow.FreezePanes = True
Application.ScreenUpdating = True
End Sub
To achieve this for all sheets in your workbook, try this:
Sub Freeze_All()
Dim Ws As Worksheet
Application.ScreenUpdating = False
For Each Ws In Application.ActiveWorkbook.Worksheets
Ws.Activate
With Application.ActiveWindow
.FreezePanes = True
End With
Next
Application.ScreenUpdating = True
End Sub
Ii am not sure if this only affects Excel 2007 (as I have not tested it on other versions yet), but it appears that you must turn off screen updating for the freeze panes function to work with VBA. Not sure as to why that is.
If you want to freeze the first row/column, but the first row/column is not displayed on the screen, the macro will not freeze the first row/column as expected.
To solve this issue you have to use the ScrollRow (or Scrollcolumn) property.
Worksheets(1).Cells(1, 1).Select
With ActiveWindow
If .FreezePanes Then .FreezePanes = False
.ScrollRow = 1
.ScrollColumn = 1
.SplitColumn = 5
.SplitRow = 5
.FreezePanes = True
End With
I propose you the snippet enclosed (to freeze after row 5 and column 5)

how to disable zoom slider through excel vba?

I am looking to disable zoom slider in excel worksheet. I have looked around but couldn't find a solution to it. There are some solutions pointing to setting the zoom to a defined value but none of them caters to disabling the zoom option itself.
Any help would be much appreciated.
I am adding the code I have put in ThisWorkbook to disable most of the visible option on worksheet.
Private Sub Workbook_Activate()
Application.ScreenUpdating = False
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
Application.DisplayFormulaBar = False
Application.DisplayStatusBar = Not Application.DisplayStatusBar
ActiveWindow.DisplayWorkbookTabs = False
Application.ScreenUpdating = True
End Sub
Private Sub Workbook_Deactivate()
Application.ScreenUpdating = False
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
Application.DisplayFormulaBar = True
Application.DisplayStatusBar = True
Application.ScreenUpdating = True
ActiveWindow.DisplayWorkbookTabs = True
End Sub
Private Sub Workbook_Open()
Application.ScreenUpdating = False
'Hide list of sheets
Call hide_sheets
Windows(1).WindowState = xlMaximized
ActiveWindow.DisplayGridlines = False
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
Application.DisplayFormulaBar = False
Application.DisplayStatusBar = Not Application.DisplayStatusBar
ActiveWindow.DisplayWorkbookTabs = False
ActiveWindow.Zoom = 100
'Lock cells in the UI sheet
ThisWorkbook.Sheets("UI").ScrollArea = "A1:t46"
'Hide scroll bar
With ActiveWindow
.DisplayHorizontalScrollBar = False
.DisplayVerticalScrollBar = False
End With
Application.ScreenUpdating = True
welcomeScreen.Show 0
End Sub
Some screen shots of the problem I am facing. I am currently using some makeshift arrangement by loading the maximized version userform on workbook opening. So the slider is hidden.
There doesn't appear to be a way to disable zoom completely or specifically the slider after looking around. If you're main mission is to avoid someone clicking on the zoom slider I would probably go with hiding the statusbar all together.
Application.DisplayStatusBar = False
To hide the zoom slider alone you can do so by editing the registry key
HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Excel\StatusBar
by setting ZoomSlider value to 0
Anyway, i don't think this can be achieved using VBA even with SaveSetting.
You can try write a .reg file to change the target key and VBA-load it. I'm not sure if this can be done but even if it works the user will still to have click Yes in a system prompt to allow the key to be loaded into registry.
And even when the user click Yes to allow the .reg file to load and change the registry key, Excel status bar doesn't refresh to show/hide the ZoomSlider, until Excel is restarted.
In short, hiding zoom slider alone using VBA doesn't seem to be achievable.

Resources