Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default very slow code

I can't figure out why this code takes 9 seconds to run. What it does is
switch the contents of two adjacent cells. It does have to unprotect the
worksheet and also turn off autocalculation, then restore both of those
settings. But the core code doesn't do much that should generate the delay I
am seeing. Any ideas from the experts? TIA

With Application
.Calculation = xlManual
.MaxChange = 0.001
End With
ActiveWorkbook.PrecisionAsDisplayed = False
ActiveSheet.Unprotect Password:="password"
Selection.Cut
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.Insert Shift:=xlToRight
ActiveSheet.Protect Password:="password", DrawingObjects:=True,
Contents:=True, Scenarios:=True _
, AllowFiltering:=True
With Application
.Calculation = xlAutomatic
.MaxChange = 0.001
End With
ActiveWorkbook.PrecisionAsDisplayed = False

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 43
Default very slow code

First off try to eliminate your select statements. In most cases they're
useless. You can also nest your application statements as one, and you can
do that with the active sheet reference.

You can also put your Cut statement on one line.

Here's an example (note I just randomly chose C1, so it probably won't do
what you want, it's more for the examples of what you can do to speed up your
code):

With Application
.Calculation = xlManual

With ActiveSheet
.Unprotect Password:="password"
With Range("C1")
.Cut .Offset(, -1)
.Offset(, -1).Insert Shift:=xlToRight
End With
.Protect Password:="password"
End With

.Calculation = xlAutomatic
End With

HTH,

Smitty

"andy62" wrote:

I can't figure out why this code takes 9 seconds to run. What it does is
switch the contents of two adjacent cells. It does have to unprotect the
worksheet and also turn off autocalculation, then restore both of those
settings. But the core code doesn't do much that should generate the delay I
am seeing. Any ideas from the experts? TIA

With Application
.Calculation = xlManual
.MaxChange = 0.001
End With
ActiveWorkbook.PrecisionAsDisplayed = False
ActiveSheet.Unprotect Password:="password"
Selection.Cut
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.Insert Shift:=xlToRight
ActiveSheet.Protect Password:="password", DrawingObjects:=True,
Contents:=True, Scenarios:=True _
, AllowFiltering:=True
With Application
.Calculation = xlAutomatic
.MaxChange = 0.001
End With
ActiveWorkbook.PrecisionAsDisplayed = False

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default very slow code

Thanks, Smitty. I tried some of the changes but I'm not any better off. The
cells I'm reversing are within a range of 9000 cells that gets celled by a
lot of formulas elsewhere in the workbook (many sumproducts). I have a
feeling that one of my VBA statements - possibly the "Insert
Shift:=xlToRight" - is causing all those formulas to recalculate. A regular
F9 manual recalc takes under 1 second, but I still seeing 9 seconds. Maybe I
need to revise my approach and avoid that statement?

"Smitty" wrote:

First off try to eliminate your select statements. In most cases they're
useless. You can also nest your application statements as one, and you can
do that with the active sheet reference.

You can also put your Cut statement on one line.

Here's an example (note I just randomly chose C1, so it probably won't do
what you want, it's more for the examples of what you can do to speed up your
code):

With Application
.Calculation = xlManual

With ActiveSheet
.Unprotect Password:="password"
With Range("C1")
.Cut .Offset(, -1)
.Offset(, -1).Insert Shift:=xlToRight
End With
.Protect Password:="password"
End With

.Calculation = xlAutomatic
End With

HTH,

Smitty

"andy62" wrote:

I can't figure out why this code takes 9 seconds to run. What it does is
switch the contents of two adjacent cells. It does have to unprotect the
worksheet and also turn off autocalculation, then restore both of those
settings. But the core code doesn't do much that should generate the delay I
am seeing. Any ideas from the experts? TIA

With Application
.Calculation = xlManual
.MaxChange = 0.001
End With
ActiveWorkbook.PrecisionAsDisplayed = False
ActiveSheet.Unprotect Password:="password"
Selection.Cut
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.Insert Shift:=xlToRight
ActiveSheet.Protect Password:="password", DrawingObjects:=True,
Contents:=True, Scenarios:=True _
, AllowFiltering:=True
With Application
.Calculation = xlAutomatic
.MaxChange = 0.001
End With
ActiveWorkbook.PrecisionAsDisplayed = False

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default very slow code

Thanks, Smitty. I tried some of the changes but I'm not any better off. The
cells I'm reversing are within a range of 9000 cells that gets celled by a
lot of formulas elsewhere in the workbook (many sumproducts). I have a
feeling that one of my VBA statements - possibly the "Insert
Shift:=xlToRight" - is causing all those formulas to recalculate. A regular
F9 manual recalc takes under 1 second, but I still seeing 9 seconds. Maybe I
need to revise my approach and avoid that statement?

"Smitty" wrote:

First off try to eliminate your select statements. In most cases they're
useless. You can also nest your application statements as one, and you can
do that with the active sheet reference.

You can also put your Cut statement on one line.

Here's an example (note I just randomly chose C1, so it probably won't do
what you want, it's more for the examples of what you can do to speed up your
code):

With Application
.Calculation = xlManual

With ActiveSheet
.Unprotect Password:="password"
With Range("C1")
.Cut .Offset(, -1)
.Offset(, -1).Insert Shift:=xlToRight
End With
.Protect Password:="password"
End With

.Calculation = xlAutomatic
End With

HTH,

Smitty

"andy62" wrote:

I can't figure out why this code takes 9 seconds to run. What it does is
switch the contents of two adjacent cells. It does have to unprotect the
worksheet and also turn off autocalculation, then restore both of those
settings. But the core code doesn't do much that should generate the delay I
am seeing. Any ideas from the experts? TIA

With Application
.Calculation = xlManual
.MaxChange = 0.001
End With
ActiveWorkbook.PrecisionAsDisplayed = False
ActiveSheet.Unprotect Password:="password"
Selection.Cut
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.Insert Shift:=xlToRight
ActiveSheet.Protect Password:="password", DrawingObjects:=True,
Contents:=True, Scenarios:=True _
, AllowFiltering:=True
With Application
.Calculation = xlAutomatic
.MaxChange = 0.001
End With
ActiveWorkbook.PrecisionAsDisplayed = False

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 43
Default very slow code

Maybe you need to give a deeper explanation of the process, like what else is
happening?

You might also want to look into disabling events if your calculating and
subsequent code changes are causing some recursion like you indicate.

What's calling your code?

Smitty

"andy62" wrote:

Thanks, Smitty. I tried some of the changes but I'm not any better off. The
cells I'm reversing are within a range of 9000 cells that gets celled by a
lot of formulas elsewhere in the workbook (many sumproducts). I have a
feeling that one of my VBA statements - possibly the "Insert
Shift:=xlToRight" - is causing all those formulas to recalculate. A regular
F9 manual recalc takes under 1 second, but I still seeing 9 seconds. Maybe I
need to revise my approach and avoid that statement?

"Smitty" wrote:

First off try to eliminate your select statements. In most cases they're
useless. You can also nest your application statements as one, and you can
do that with the active sheet reference.

You can also put your Cut statement on one line.

Here's an example (note I just randomly chose C1, so it probably won't do
what you want, it's more for the examples of what you can do to speed up your
code):

With Application
.Calculation = xlManual

With ActiveSheet
.Unprotect Password:="password"
With Range("C1")
.Cut .Offset(, -1)
.Offset(, -1).Insert Shift:=xlToRight
End With
.Protect Password:="password"
End With

.Calculation = xlAutomatic
End With

HTH,

Smitty

"andy62" wrote:

I can't figure out why this code takes 9 seconds to run. What it does is
switch the contents of two adjacent cells. It does have to unprotect the
worksheet and also turn off autocalculation, then restore both of those
settings. But the core code doesn't do much that should generate the delay I
am seeing. Any ideas from the experts? TIA

With Application
.Calculation = xlManual
.MaxChange = 0.001
End With
ActiveWorkbook.PrecisionAsDisplayed = False
ActiveSheet.Unprotect Password:="password"
Selection.Cut
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.Insert Shift:=xlToRight
ActiveSheet.Protect Password:="password", DrawingObjects:=True,
Contents:=True, Scenarios:=True _
, AllowFiltering:=True
With Application
.Calculation = xlAutomatic
.MaxChange = 0.001
End With
ActiveWorkbook.PrecisionAsDisplayed = False

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Slow code when used as VBA code instead of macro (copying visible columns) [email protected] Excel Programming 3 April 2nd 07 05:26 PM
slow code NickHK Excel Programming 0 December 21st 06 02:32 AM
Can someone help me with this slow code BillReese Excel Programming 7 September 3rd 05 12:34 AM
Slow Code Shawn Excel Programming 7 August 23rd 05 08:44 PM
Is this slow code? Tom Excel Programming 4 March 3rd 04 11:18 PM


All times are GMT +1. The time now is 01:55 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"