D
David
I'm new to VBA scripting in Excel and just wrote my first
user-defined function. The function below works fine but
is slow. It takes about 30 seconds to calculate the sum of
the absolute value of a range consisting of 300 rows in a
single column (i.e. 300 cells). Is there something
fundamental about user-defined functions that makes them
terribly slow or am I not doing something that is killing
my performance? The Excel built-in worksheet functions are
fast!
I'm running Excel 97 SR-2.
The function is referenced in a worksheet cell
as, "=amplitude(A1-style-ref:A1-style-ref)".
Thanks, Thanks, Thanks!
(e-mail address removed)
'**********************************************************
Function amplitude(myRange)
' Returns the average of the absolute value of cells
in "myRange".
Dim iRow, iCol
Dim firstCol, lastCol
Dim firstRow, lastRow
Dim sum, myCount
Dim mySheetName
firstCol = myRange.Column
lastCol = myRange.Columns(myRange.Columns.Count).Column
firstRow = myRange.Row
lastRow = myRange.Rows(myRange.Rows.Count).Row
mySheetName = Application.Caller.Parent.Name
sum = 0
iCol = firstCol
Do
iRow = firstRow
Do
sum = sum + Abs(Worksheets(mySheetName).Cells(iRow,
iCol))
iRow = iRow + 1
Loop While iRow <= lastRow
iCol = iCol + 1
Loop While iCol <= lastCol
myCount = myRange.Count
amplitude = sum / myCount
End Function
user-defined function. The function below works fine but
is slow. It takes about 30 seconds to calculate the sum of
the absolute value of a range consisting of 300 rows in a
single column (i.e. 300 cells). Is there something
fundamental about user-defined functions that makes them
terribly slow or am I not doing something that is killing
my performance? The Excel built-in worksheet functions are
fast!
I'm running Excel 97 SR-2.
The function is referenced in a worksheet cell
as, "=amplitude(A1-style-ref:A1-style-ref)".
Thanks, Thanks, Thanks!
(e-mail address removed)
'**********************************************************
Function amplitude(myRange)
' Returns the average of the absolute value of cells
in "myRange".
Dim iRow, iCol
Dim firstCol, lastCol
Dim firstRow, lastRow
Dim sum, myCount
Dim mySheetName
firstCol = myRange.Column
lastCol = myRange.Columns(myRange.Columns.Count).Column
firstRow = myRange.Row
lastRow = myRange.Rows(myRange.Rows.Count).Row
mySheetName = Application.Caller.Parent.Name
sum = 0
iCol = firstCol
Do
iRow = firstRow
Do
sum = sum + Abs(Worksheets(mySheetName).Cells(iRow,
iCol))
iRow = iRow + 1
Loop While iRow <= lastRow
iCol = iCol + 1
Loop While iCol <= lastCol
myCount = myRange.Count
amplitude = sum / myCount
End Function