Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Speed this up for me, please

Dear friends,
I have a series of sheets used as databases, and I have written functions to
help me retrieve info from them. These work just fine, but as my application
grows so it slows down, and I am aware that this code is not optimised for
speed. I'm not the world's best programmer, so help would be welcome. This
function (and several others very very similar) are at the heart of my
application, so small improvements here might have a wide ranging effect.

The rules of the database....
1. The first row of the sheet contains titles for the columns.
2. The order of the columns is not guaranteed.
3. I don't always look up using the same key - so any of the columns might
be the key. (Let me do the worrying about duplicate entries and uniqeness of
keys...)
4. There is no limit to the width or height of the sheet (other than the
usual 2^8 columns, 2^16 rows, and memory space)

To find the entry whose name is Smith and return the phone number a
worksheet would have the following formula in a cell...
=Sinfo("Name","Smith","Phone")

Function Sinfo(lookupkey, lookupval, rtnkey, Optional vol)
If IsMissing(vol) Then vol = False
Application.Volatile (vol)
On Error GoTo ErrHandler
Set rng = Workbooks("data.xls").Worksheets("students").Range ("1:1")
lkcol = Application.WorksheetFunction.Match(lookupkey, rng, 0)
rtncol = Application.WorksheetFunction.Match(rtnkey, rng, 0)
With Workbooks("data.xls").Worksheets("students")
Set rng2 = .Range(.Cells(1, lkcol), .Cells(65535, lkcol))
End With
rtnrow = Application.WorksheetFunction.Match(lookupval, rng2, 0)
Sinfo = Workbooks("data.xls").Worksheets("students").Cells (rtnrow, rtncol)
Exit Function
ErrHandler:
Sinfo = "Not Found"
End Function

--
Adrian D.Bailey, Information and Systems Manager, Dept.Human Sciences
Loughborough University, Loughborough Leics, LE11 3TU, UK.
Tel: 01509 223007 Fax: 01509 223940

Community Warden, Storer and Burleigh Areas. Out-of-hours Tel: 01509 563263
--


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Speed this up for me, please


Adrian,
I declared all of the variables, specified a default value for the optional
argument ,only used one range variable and eliminated the need for "With".
Whether it is faster is the question...

Function Sinfo_R1(ByRef lookupKey As String, ByRef lookupVal As String, _
ByRef rtnKey As String, Optional bVol As Boolean = False) As String
On Error GoTo ErrHandler
Dim rng As Excel.Range
Dim lkCol As Long
Dim rtnCol As Long
Dim rtnRow As Long

Application.Volatile (bVol)
Set rng = Workbooks("data.xls").Worksheets("students").UsedR ange
lkCol = Application.Match(lookupKey, rng.Rows(1), 0) 'Lookup Name column
rtnCol = Application.Match(rtnKey, rng.Rows(1), 0) 'Lookup Phone column
rtnRow = Application.Match(lookupVal, rng.Columns(lkCol), 0)
Sinfo_R1 = rng(rtnRow, rtnCol).Value
Exit Function
ErrHandler:
Sinfo_R1 = "Not Found"
End Function

Sub FindIt()
MsgBox Sinfo_R1("Name", "Smith", "Phone")
End Sub
----------
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"Adrian D. Bailey"
wrote in message
Dear friends,
I have a series of sheets used as databases, and I have written functions to
help me retrieve info from them. These work just fine, but as my application
grows so it slows down, and I am aware that this code is not optimised for
speed. I'm not the world's best programmer, so help would be welcome. This
function (and several others very very similar) are at the heart of my
application, so small improvements here might have a wide ranging effect.

The rules of the database....
1. The first row of the sheet contains titles for the columns.
2. The order of the columns is not guaranteed.
3. I don't always look up using the same key - so any of the columns might
be the key. (Let me do the worrying about duplicate entries and uniqeness of
keys...)
4. There is no limit to the width or height of the sheet (other than the
usual 2^8 columns, 2^16 rows, and memory space)

To find the entry whose name is Smith and return the phone number a
worksheet would have the following formula in a cell...
=Sinfo("Name","Smith","Phone")

Function Sinfo(lookupkey, lookupval, rtnkey, Optional vol)
If IsMissing(vol) Then vol = False
Application.Volatile (vol)
On Error GoTo ErrHandler
Set rng = Workbooks("data.xls").Worksheets("students").Range ("1:1")
lkcol = Application.WorksheetFunction.Match(lookupkey, rng, 0)
rtncol = Application.WorksheetFunction.Match(rtnkey, rng, 0)
With Workbooks("data.xls").Worksheets("students")
Set rng2 = .Range(.Cells(1, lkcol), .Cells(65535, lkcol))
End With
rtnrow = Application.WorksheetFunction.Match(lookupval, rng2, 0)
Sinfo = Workbooks("data.xls").Worksheets("students").Cells (rtnrow, rtncol)
Exit Function
ErrHandler:
Sinfo = "Not Found"
End Function
--
Adrian D.Bailey, Information and Systems Manager, Dept.Human Sciences
Loughborough University, Loughborough Leics, LE11 3TU, UK.
Tel: 01509 223007 Fax: 01509 223940
Community Warden, Storer and Burleigh Areas. Out-of-hours Tel: 01509 563263
--


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
Is there any way to speed this up? Keith74 Excel Programming 5 December 15th 06 01:32 PM
VBA Speed up sparx Excel Discussion (Misc queries) 2 April 29th 06 04:59 PM
Can you speed UP drag speed? Ryan W Excel Discussion (Misc queries) 1 October 24th 05 06:09 PM
Speed tjh Excel Programming 1 December 16th 04 05:49 PM
Speed? Stu[_31_] Excel Programming 11 October 18th 03 09:41 PM


All times are GMT +1. The time now is 10:55 PM.

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

About Us

"It's about Microsoft Excel"