Solved

Worksheet Selection Change - however slowing down spreadsheet

Posted on 2010-11-19
6
543 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

911 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

21 Experts available now in Live!

Get 1:1 Help Now