Thread: Optimizing Code
View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson[_3_] Jim Thomlinson[_3_] is offline
external usenet poster
 
Posts: 983
Default Optimizing Code

I have a function that I need to run very frequently for an end user
application that is already slow enough. What I need to do is to add up the
values in every second column to the right of a given cell. I was hoping that
someone could look at this code and tell me if there is any way to squeek a
little more speed out of it...

Option Explicit

Sub test()
Dim x As Double

x = AddAlternatingColumns(Sheet2, Sheet2.Range("B2"))
MsgBox x
End Sub

Public Function AddAlternatingColumns(ByVal wks As Worksheet, ByVal
rngTarget As Range) As Double
Dim dblReturnValue As Double
Dim intLastColumn As Integer

intLastColumn = wks.Range("A1").SpecialCells(xlCellTypeLastCell).C olumn
Do While rngTarget.Column < intLastColumn
Set rngTarget = rngTarget.Offset(0, 2)
dblReturnValue = dblReturnValue + rngTarget.Value
Loop
AddAlternatingColumns = dblReturnValue
End Function

--
Thanks In Advance...

Jim Thomlinson