Solved

Worksheet Selection Change - however slowing down spreadsheet

Posted on 2010-11-19
6
542 Views
Last Modified: 2013-11-25
I currently have a excel form which users input date
1.  Users have to fill in mamdatory data in first sheet.
2.  At the abocve sheet users choose "systems", and this will open unhide extra sheets to be filled in.
3.  When choosing system (more than 1) some combinations are immendiatelty defaulted if 2 systems go together,
2&3 use Worksheet Selection Change  macro,  This runs the macro everytime you move cells anywhere in the sheet.    This is slowing down the sopreadsheet, and using a curser to move down can be very slow.

1.  is there a way of slectio change cabn apply to only cells moved within a region.
2.  What else do uyou recommend?

Start of my code eg


If Range("J20").Value = "Summit XXX" Then
Sheets("Summit 3.83").Visible = False
Sheets("Summit 3.75").Visible = True
Sheets("Murex").Visible = False
Sheets("Martini").Visible = False
Sheets("TOMS").Visible = True
'Sheets("Paris (Hierarchy)").Visible = True
Range("F20").Value = "X"
Range("F34").Value = "X"
Range("J34").Value = "Yes"

ElseIf Range("J20").Value = "Summit XXY" Then
Sheets("Summit 3.83").Visible = False
Sheets("Summit 3.75").Visible = True
Sheets("Murex").Visible = False
Sheets("Martini").Visible = False

etc....... many lines.


Thanks
0
Comment
Question by:yasanthax
6 Comments
 
LVL 24

Assisted Solution

by:broomee9
broomee9 earned 100 total points
Comment Utility
Add this to the beginning of your code:
Application.ScreenUpdating = False
Application.EnableEvents = False

Then add this to the end of your code:
Application.ScreenUpdating = True
Application.EnableEvents = True
0
 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
1.  is there a way of slectio change cabn apply to only cells moved within a region.
2.  What else do uyou recommend?

Can I see your workbook?
0
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
To address the other part of the question do
If Not Intersect(Target, Range("A1:D7")) Is Nothing Then
...
End If

Open in new window


To only run if the selected cell is in Range("A1:D7")
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
So something like this
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1:D7")) Is Nothing Then
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  'All the code goes here
  Application.ScreenUpdating = True
  Application.EnableEvents = True
End If
End Sub

Open in new window

0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 400 total points
Comment Utility
Any time you turn off Events, ScreenUpdating etc, you should consider handling errors so that if the code crashes, the stuff doesn't stay off (if it gets an error, it will stop executing before it gets to where it turns it all back on) So I do this
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1:D7")) Is Nothing Then
  Application.ScreenUpdating = False 'Make it so the screen doesn't change while the code runs
  Application.EnableEvents = False 'Make it so other events don't fire
  On Error GoTo crash 'If it has an error move to the crash label so stuff gets turned back on
  'All the code goes here
  
crash:
  If Err.Number <> 0 Then 'If an error happened
    MsgBox "Error: " & Err.Description
  End If
  On Error GoTo 0 'Fix error handling
  Application.ScreenUpdating = True 'Turn it all back on
  Application.EnableEvents = True
End If
End Sub

Open in new window


But if using intersect speeds it up enough, you don't need to bother with all that.Good info though.
0
 

Author Closing Comment

by:yasanthax
Comment Utility
Exactly answered question as stated and in addition added error handling
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Indirect formula 9 28
Excel 6 16
vb6 - Transfer from MSHFlexgrid1 to xls issue 8 9
VBA in SharePoint 3 16
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now