?
Solved

Worksheet Selection Change - however slowing down spreadsheet

Posted on 2010-11-19
6
Medium Priority
?
547 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 24

Assisted Solution

by:broomee9
broomee9 earned 400 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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 1600 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

801 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