Solved

Worksheet Selection Change - however slowing down spreadsheet

Posted on 2010-11-19
6
544 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
ID: 34174917
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
ID: 34174973
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
ID: 34174975
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34174985
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
ID: 34175025
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
ID: 34186708
Exactly answered question as stated and in addition added error handling
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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