PC Review


Reply
Thread Tools Rate Thread

How can I optimize this code?

 
 
Gustaf
Guest
Posts: n/a
 
      9th Jan 2009
In my VBA app, all input data needs to be removed occasionally in a reset procedure. The problem is that this reset procedure, which I think is quite simple and straightforward, takes several seconds to complete. I'd like to hear some ideas on what it is in the code below that is so expensive performance-wise.

Many thanks,

Gustaf

--

' Resets a sheet by clearing data on rows with known control characters
Private Sub ResetSheet(wsh As Worksheet)

Dim iCol As Integer
Dim iRow As Integer
Dim iLastRow As Integer
Dim iFirstCol As Integer
Dim iLastCol As Integer
Dim iCtrlCol As Integer
Dim sArray() As String
Dim i As Integer

sArray = Split(conCtrlStrings, " ")

' Find delimiting cells
iLastRow = wsh.Cells(wsh.Rows.Count, "H").End(xlUp).Row
iFirstCol = 41
iLastCol = wsh.Cells(2, wsh.Columns.Count).End(xlToLeft).Column + 11
iCtrlCol = 8

' Loop through rows
For iRow = 6 To iLastRow
' Loop through control characters
For i = 0 To UBound(sArray)
' If the Ctrl column on the current row matches a known Ctrl character
If Cells(iRow, iCtrlCol) = sArray(i) Then
' Clear this row
For iCol = iFirstCol To iLastCol
Cells(iRow, iCol).ClearContents
Cells(iRow, iCol).ClearComments
Next iCol
' Skip to next row
Exit For
End If
Next i
Next iRow

End Sub
 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      9th Jan 2009
try this change
from
For iCol = iFirstCol To iLastCol
Cells(iRow, iCol).ClearContents
Cells(iRow, iCol).ClearComments
Next iCol
to
set ClearRange = Range(cells(iRow,iFirstCol),cells(irow,iLastCol)
ClearRange.ClearContents
ClearRange.ClearComments

I think the Clear comments is what slows the code down significantly.

"Gustaf" wrote:

> In my VBA app, all input data needs to be removed occasionally in a reset procedure. The problem is that this reset procedure, which I think is quite simple and straightforward, takes several seconds to complete. I'd like to hear some ideas on what it is in the code below that is so expensive performance-wise.
>
> Many thanks,
>
> Gustaf
>
> --
>
> ' Resets a sheet by clearing data on rows with known control characters
> Private Sub ResetSheet(wsh As Worksheet)
>
> Dim iCol As Integer
> Dim iRow As Integer
> Dim iLastRow As Integer
> Dim iFirstCol As Integer
> Dim iLastCol As Integer
> Dim iCtrlCol As Integer
> Dim sArray() As String
> Dim i As Integer
>
> sArray = Split(conCtrlStrings, " ")
>
> ' Find delimiting cells
> iLastRow = wsh.Cells(wsh.Rows.Count, "H").End(xlUp).Row
> iFirstCol = 41
> iLastCol = wsh.Cells(2, wsh.Columns.Count).End(xlToLeft).Column + 11
> iCtrlCol = 8
>
> ' Loop through rows
> For iRow = 6 To iLastRow
> ' Loop through control characters
> For i = 0 To UBound(sArray)
> ' If the Ctrl column on the current row matches a known Ctrl character
> If Cells(iRow, iCtrlCol) = sArray(i) Then
> ' Clear this row
> For iCol = iFirstCol To iLastCol
> Cells(iRow, iCol).ClearContents
> Cells(iRow, iCol).ClearComments
> Next iCol
> ' Skip to next row
> Exit For
> End If
> Next i
> Next iRow
>
> End Sub
>

 
Reply With Quote
 
Gustaf
Guest
Posts: n/a
 
      12th Jan 2009
Joel wrote:

> set ClearRange = Range(cells(iRow,iFirstCol),cells(irow,iLastCol)
> ClearRange.ClearContents
> ClearRange.ClearComments
>
> I think the Clear comments is what slows the code down significantly.


That was it! Together with Application.Screenupdating = False, it now clears sheets almost instantly! :-)

Gustaf
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hou would you optimize this code? HammerJoe@gmail.com Microsoft Excel Programming 8 17th Jan 2008 04:26 PM
Please Help me Optimize my Code TF Microsoft ASP .NET 10 28th Jun 2007 07:46 AM
optimize code Jusan Microsoft VB .NET 2 20th Jul 2004 08:42 AM
How can I optimize this code? wullux Microsoft Excel Programming 2 17th Dec 2003 07:13 PM
optimize the code Maneesg Microsoft Dot NET Compact Framework 1 23rd Oct 2003 01:10 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:17 PM.