PC Review


Reply
Thread Tools Rate Thread

Can this function be made more efficient?

 
 
=?Utf-8?B?Q2xheW1hbg==?=
Guest
Posts: n/a
 
      19th Jul 2007
In preparation for migrating data, I needed a way to lookup without the
lookup range being sorted. So, I wrote the following function - but it really
slows things down. It is used in a spreadsheet as follows:
=FINDINRANGE(B2,'other sheet'!B4:G199,6)
where the parameters are identical to VLOOKUP.

Code:

Public Function findinrange(lookitup As Range, rainge As Range, offsett As
Integer)

'Function findinrange works in a manner similar to LOOKUP, but it can find
'a value (or valyou) even if not sorted or if a different type (ie: numeric
vs. text)

hereitis = 0
valyou = lookitup.Value
If IsNumeric(valyou) Then valyou = Str$(valyou)
With rainge
rose = rainge.Rows.Count
ro = 1
Do Until ro = rose
lookhere = .Cells(ro, 1).Value
If IsNumeric(lookhere) Then lookhere = Str$(lookhere)
If InStr(1, lookhere, valyou) > 0 Then
hereitis = ro
ro = rose
Else
ro = ro + 1
End If
Loop
If hereitis = 0 Then
findinrange = CVErr(xlErrNA)
Else
findinrange = .Cells(hereitis, offsett).Value
End If
End With
End Function
--
Adios,
Clay Harryman
 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      19th Jul 2007
First thing is declare all of your variables. Any variable not declared will
be of type variant which is the slowest type of variable...
--
HTH...

Jim Thomlinson


"Clayman" wrote:

> In preparation for migrating data, I needed a way to lookup without the
> lookup range being sorted. So, I wrote the following function - but it really
> slows things down. It is used in a spreadsheet as follows:
> =FINDINRANGE(B2,'other sheet'!B4:G199,6)
> where the parameters are identical to VLOOKUP.
>
> Code:
>
> Public Function findinrange(lookitup As Range, rainge As Range, offsett As
> Integer)
>
> 'Function findinrange works in a manner similar to LOOKUP, but it can find
> 'a value (or valyou) even if not sorted or if a different type (ie: numeric
> vs. text)
>
> hereitis = 0
> valyou = lookitup.Value
> If IsNumeric(valyou) Then valyou = Str$(valyou)
> With rainge
> rose = rainge.Rows.Count
> ro = 1
> Do Until ro = rose
> lookhere = .Cells(ro, 1).Value
> If IsNumeric(lookhere) Then lookhere = Str$(lookhere)
> If InStr(1, lookhere, valyou) > 0 Then
> hereitis = ro
> ro = rose
> Else
> ro = ro + 1
> End If
> Loop
> If hereitis = 0 Then
> findinrange = CVErr(xlErrNA)
> Else
> findinrange = .Cells(hereitis, offsett).Value
> End If
> End With
> End Function
> --
> Adios,
> Clay Harryman

 
Reply With Quote
 
=?Utf-8?B?Q2xheW1hbg==?=
Guest
Posts: n/a
 
      19th Jul 2007
Thank you.
This did speed things up somewhat. Here is the new code (since there were no
variant types, I had to re-arrange some of the code):

Public Function findinrange(lookitup As Range, rainge As Range, offsett As
Integer)

'Function findinrange works in a manner similar to LOOKUP, but it can find
'a value (or valyou) even if not sorted or if a different type (ie: numeric
vs. text)

Dim hereitis As Integer, rose As Integer, ro As Integer
Dim valyou As String, lookhere As String

hereitis = 0
If IsNumeric(lookitup.Value) Then
valyou = Str$(lookitup.Value)
Else
valyou = lookitup.Value
End If
With rainge
rose = .Rows.Count
ro = 1
Do Until ro = rose
If IsNumeric(.Cells(ro, 1).Value) Then
lookhere = Str$(.Cells(ro, 1).Value)
Else
lookhere = .Cells(ro, 1).Value
End If
If InStr(1, lookhere, valyou) > 0 Then
hereitis = ro
ro = rose
Else
ro = ro + 1
End If
Loop
If hereitis = 0 Then
findinrange = CVErr(xlErrNA)
Else
findinrange = .Cells(hereitis, offsett).Value
End If
End With
End Function

--
Adios,
Clay Harryman


"Jim Thomlinson" wrote:

> First thing is declare all of your variables. Any variable not declared will
> be of type variant which is the slowest type of variable...
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Clayman" wrote:
>
> > In preparation for migrating data, I needed a way to lookup without the
> > lookup range being sorted. So, I wrote the following function - but it really
> > slows things down. It is used in a spreadsheet as follows:
> > =FINDINRANGE(B2,'other sheet'!B4:G199,6)
> > where the parameters are identical to VLOOKUP.
> >
> > Code:
> >
> > Public Function findinrange(lookitup As Range, rainge As Range, offsett As
> > Integer)
> >
> > 'Function findinrange works in a manner similar to LOOKUP, but it can find
> > 'a value (or valyou) even if not sorted or if a different type (ie: numeric
> > vs. text)
> >
> > hereitis = 0
> > valyou = lookitup.Value
> > If IsNumeric(valyou) Then valyou = Str$(valyou)
> > With rainge
> > rose = rainge.Rows.Count
> > ro = 1
> > Do Until ro = rose
> > lookhere = .Cells(ro, 1).Value
> > If IsNumeric(lookhere) Then lookhere = Str$(lookhere)
> > If InStr(1, lookhere, valyou) > 0 Then
> > hereitis = ro
> > ro = rose
> > Else
> > ro = ro + 1
> > End If
> > Loop
> > If hereitis = 0 Then
> > findinrange = CVErr(xlErrNA)
> > Else
> > findinrange = .Cells(hereitis, offsett).Value
> > End If
> > End With
> > End Function
> > --
> > Adios,
> > Clay Harryman

 
Reply With Quote
 
=?Utf-8?B?Q2hhcmxpZQ==?=
Guest
Posts: n/a
 
      19th Jul 2007
Another tip: Throw out "As Integer" and always use "As Long" (unless
required by a particular sub or function.) Why? Your row count can exceed
the limit of an integer and you will get an error. As a habit I use Long
everywhere.

"Clayman" wrote:

> Thank you.
> This did speed things up somewhat. Here is the new code (since there were no
> variant types, I had to re-arrange some of the code):
>
> Public Function findinrange(lookitup As Range, rainge As Range, offsett As
> Integer)
>
> 'Function findinrange works in a manner similar to LOOKUP, but it can find
> 'a value (or valyou) even if not sorted or if a different type (ie: numeric
> vs. text)
>
> Dim hereitis As Integer, rose As Integer, ro As Integer
> Dim valyou As String, lookhere As String
>
> hereitis = 0
> If IsNumeric(lookitup.Value) Then
> valyou = Str$(lookitup.Value)
> Else
> valyou = lookitup.Value
> End If
> With rainge
> rose = .Rows.Count
> ro = 1
> Do Until ro = rose
> If IsNumeric(.Cells(ro, 1).Value) Then
> lookhere = Str$(.Cells(ro, 1).Value)
> Else
> lookhere = .Cells(ro, 1).Value
> End If
> If InStr(1, lookhere, valyou) > 0 Then
> hereitis = ro
> ro = rose
> Else
> ro = ro + 1
> End If
> Loop
> If hereitis = 0 Then
> findinrange = CVErr(xlErrNA)
> Else
> findinrange = .Cells(hereitis, offsett).Value
> End If
> End With
> End Function
>
> --
> Adios,
> Clay Harryman
>
>
> "Jim Thomlinson" wrote:
>
> > First thing is declare all of your variables. Any variable not declared will
> > be of type variant which is the slowest type of variable...
> > --
> > HTH...
> >
> > Jim Thomlinson
> >
> >
> > "Clayman" wrote:
> >
> > > In preparation for migrating data, I needed a way to lookup without the
> > > lookup range being sorted. So, I wrote the following function - but it really
> > > slows things down. It is used in a spreadsheet as follows:
> > > =FINDINRANGE(B2,'other sheet'!B4:G199,6)
> > > where the parameters are identical to VLOOKUP.
> > >
> > > Code:
> > >
> > > Public Function findinrange(lookitup As Range, rainge As Range, offsett As
> > > Integer)
> > >
> > > 'Function findinrange works in a manner similar to LOOKUP, but it can find
> > > 'a value (or valyou) even if not sorted or if a different type (ie: numeric
> > > vs. text)
> > >
> > > hereitis = 0
> > > valyou = lookitup.Value
> > > If IsNumeric(valyou) Then valyou = Str$(valyou)
> > > With rainge
> > > rose = rainge.Rows.Count
> > > ro = 1
> > > Do Until ro = rose
> > > lookhere = .Cells(ro, 1).Value
> > > If IsNumeric(lookhere) Then lookhere = Str$(lookhere)
> > > If InStr(1, lookhere, valyou) > 0 Then
> > > hereitis = ro
> > > ro = rose
> > > Else
> > > ro = ro + 1
> > > End If
> > > Loop
> > > If hereitis = 0 Then
> > > findinrange = CVErr(xlErrNA)
> > > Else
> > > findinrange = .Cells(hereitis, offsett).Value
> > > End If
> > > End With
> > > End Function
> > > --
> > > Adios,
> > > Clay Harryman

 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      19th Jul 2007
Change your integer types to Long. Two reasons. One is that row number can
exceed 32k where integer ends. Secondly Long is faster than integer. Because
your system is 32 bit VBA has to do some fancy footwork to deal with what is
essentially a 16 bit number.
--
HTH...

Jim Thomlinson


"Clayman" wrote:

> Thank you.
> This did speed things up somewhat. Here is the new code (since there were no
> variant types, I had to re-arrange some of the code):
>
> Public Function findinrange(lookitup As Range, rainge As Range, offsett As
> Integer)
>
> 'Function findinrange works in a manner similar to LOOKUP, but it can find
> 'a value (or valyou) even if not sorted or if a different type (ie: numeric
> vs. text)
>
> Dim hereitis As Integer, rose As Integer, ro As Integer
> Dim valyou As String, lookhere As String
>
> hereitis = 0
> If IsNumeric(lookitup.Value) Then
> valyou = Str$(lookitup.Value)
> Else
> valyou = lookitup.Value
> End If
> With rainge
> rose = .Rows.Count
> ro = 1
> Do Until ro = rose
> If IsNumeric(.Cells(ro, 1).Value) Then
> lookhere = Str$(.Cells(ro, 1).Value)
> Else
> lookhere = .Cells(ro, 1).Value
> End If
> If InStr(1, lookhere, valyou) > 0 Then
> hereitis = ro
> ro = rose
> Else
> ro = ro + 1
> End If
> Loop
> If hereitis = 0 Then
> findinrange = CVErr(xlErrNA)
> Else
> findinrange = .Cells(hereitis, offsett).Value
> End If
> End With
> End Function
>
> --
> Adios,
> Clay Harryman
>
>
> "Jim Thomlinson" wrote:
>
> > First thing is declare all of your variables. Any variable not declared will
> > be of type variant which is the slowest type of variable...
> > --
> > HTH...
> >
> > Jim Thomlinson
> >
> >
> > "Clayman" wrote:
> >
> > > In preparation for migrating data, I needed a way to lookup without the
> > > lookup range being sorted. So, I wrote the following function - but it really
> > > slows things down. It is used in a spreadsheet as follows:
> > > =FINDINRANGE(B2,'other sheet'!B4:G199,6)
> > > where the parameters are identical to VLOOKUP.
> > >
> > > Code:
> > >
> > > Public Function findinrange(lookitup As Range, rainge As Range, offsett As
> > > Integer)
> > >
> > > 'Function findinrange works in a manner similar to LOOKUP, but it can find
> > > 'a value (or valyou) even if not sorted or if a different type (ie: numeric
> > > vs. text)
> > >
> > > hereitis = 0
> > > valyou = lookitup.Value
> > > If IsNumeric(valyou) Then valyou = Str$(valyou)
> > > With rainge
> > > rose = rainge.Rows.Count
> > > ro = 1
> > > Do Until ro = rose
> > > lookhere = .Cells(ro, 1).Value
> > > If IsNumeric(lookhere) Then lookhere = Str$(lookhere)
> > > If InStr(1, lookhere, valyou) > 0 Then
> > > hereitis = ro
> > > ro = rose
> > > Else
> > > ro = ro + 1
> > > End If
> > > Loop
> > > If hereitis = 0 Then
> > > findinrange = CVErr(xlErrNA)
> > > Else
> > > findinrange = .Cells(hereitis, offsett).Value
> > > End If
> > > End With
> > > End Function
> > > --
> > > Adios,
> > > Clay Harryman

 
Reply With Quote
 
=?Utf-8?B?Q2xheW1hbg==?=
Guest
Posts: n/a
 
      19th Jul 2007
I got used to sticking with Integer types back when memory was a serious
consideration. It's not that way anymore, is it? LOL

Thanks for the tip. I'll keep that in mind.

And, I didn't think that it wouldn't hold all the rows in the range...
--
Adios,
Clay Harryman


"Charlie" wrote:

> Another tip: Throw out "As Integer" and always use "As Long" (unless
> required by a particular sub or function.) Why? Your row count can exceed
> the limit of an integer and you will get an error. As a habit I use Long
> everywhere.
>
> "Clayman" wrote:
>
> > Thank you.
> > This did speed things up somewhat. Here is the new code (since there were no
> > variant types, I had to re-arrange some of the code):
> >
> > Public Function findinrange(lookitup As Range, rainge As Range, offsett As
> > Integer)
> >
> > 'Function findinrange works in a manner similar to LOOKUP, but it can find
> > 'a value (or valyou) even if not sorted or if a different type (ie: numeric
> > vs. text)
> >
> > Dim hereitis As Integer, rose As Integer, ro As Integer
> > Dim valyou As String, lookhere As String
> >
> > hereitis = 0
> > If IsNumeric(lookitup.Value) Then
> > valyou = Str$(lookitup.Value)
> > Else
> > valyou = lookitup.Value
> > End If
> > With rainge
> > rose = .Rows.Count
> > ro = 1
> > Do Until ro = rose
> > If IsNumeric(.Cells(ro, 1).Value) Then
> > lookhere = Str$(.Cells(ro, 1).Value)
> > Else
> > lookhere = .Cells(ro, 1).Value
> > End If
> > If InStr(1, lookhere, valyou) > 0 Then
> > hereitis = ro
> > ro = rose
> > Else
> > ro = ro + 1
> > End If
> > Loop
> > If hereitis = 0 Then
> > findinrange = CVErr(xlErrNA)
> > Else
> > findinrange = .Cells(hereitis, offsett).Value
> > End If
> > End With
> > End Function
> >
> > --
> > Adios,
> > Clay Harryman
> >
> >
> > "Jim Thomlinson" wrote:
> >
> > > First thing is declare all of your variables. Any variable not declared will
> > > be of type variant which is the slowest type of variable...
> > > --
> > > HTH...
> > >
> > > Jim Thomlinson
> > >
> > >
> > > "Clayman" wrote:
> > >
> > > > In preparation for migrating data, I needed a way to lookup without the
> > > > lookup range being sorted. So, I wrote the following function - but it really
> > > > slows things down. It is used in a spreadsheet as follows:
> > > > =FINDINRANGE(B2,'other sheet'!B4:G199,6)
> > > > where the parameters are identical to VLOOKUP.
> > > >
> > > > Code:
> > > >
> > > > Public Function findinrange(lookitup As Range, rainge As Range, offsett As
> > > > Integer)
> > > >
> > > > 'Function findinrange works in a manner similar to LOOKUP, but it can find
> > > > 'a value (or valyou) even if not sorted or if a different type (ie: numeric
> > > > vs. text)
> > > >
> > > > hereitis = 0
> > > > valyou = lookitup.Value
> > > > If IsNumeric(valyou) Then valyou = Str$(valyou)
> > > > With rainge
> > > > rose = rainge.Rows.Count
> > > > ro = 1
> > > > Do Until ro = rose
> > > > lookhere = .Cells(ro, 1).Value
> > > > If IsNumeric(lookhere) Then lookhere = Str$(lookhere)
> > > > If InStr(1, lookhere, valyou) > 0 Then
> > > > hereitis = ro
> > > > ro = rose
> > > > Else
> > > > ro = ro + 1
> > > > End If
> > > > Loop
> > > > If hereitis = 0 Then
> > > > findinrange = CVErr(xlErrNA)
> > > > Else
> > > > findinrange = .Cells(hereitis, offsett).Value
> > > > End If
> > > > End With
> > > > End Function
> > > > --
> > > > Adios,
> > > > Clay Harryman

 
Reply With Quote
 
=?Utf-8?B?Q2xheW1hbg==?=
Guest
Posts: n/a
 
      19th Jul 2007
Jim and Charlie (and anyone else who desires to chime in)

While we're on the subject of declaring variables, is it possible to declare
a group of variables as a certain type?

I've tried:
DIM a, b, c As String
but that only declares "c" as String type. I'm assuming "a" and "b" are both
variants.

I'm trying to avoid entering:
DIM a As String, b As String, c As String

Thanks!
--
Adios,
Clay Harryman

 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      19th Jul 2007
Check out this link...

http://www.cpearson.com/excel/variables.htm
--
HTH...

Jim Thomlinson


"Clayman" wrote:

> Jim and Charlie (and anyone else who desires to chime in)
>
> While we're on the subject of declaring variables, is it possible to declare
> a group of variables as a certain type?
>
> I've tried:
> DIM a, b, c As String
> but that only declares "c" as String type. I'm assuming "a" and "b" are both
> variants.
>
> I'm trying to avoid entering:
> DIM a As String, b As String, c As String
>
> Thanks!
> --
> Adios,
> Clay Harryman
>

 
Reply With Quote
 
=?Utf-8?B?Q2xheW1hbg==?=
Guest
Posts: n/a
 
      19th Jul 2007
Ah, yes. Mr. Pearson always has good information. I need to visit his site
more often.

Thanks for the link. That's a good habit to get into.
--
Adios,
Clay Harryman


"Jim Thomlinson" wrote:

> Check out this link...
>
> http://www.cpearson.com/excel/variables.htm
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Clayman" wrote:
>
> > Jim and Charlie (and anyone else who desires to chime in)
> >
> > While we're on the subject of declaring variables, is it possible to declare
> > a group of variables as a certain type?
> >
> > I've tried:
> > DIM a, b, c As String
> > but that only declares "c" as String type. I'm assuming "a" and "b" are both
> > variants.
> >
> > I'm trying to avoid entering:
> > DIM a As String, b As String, c As String
> >
> > Thanks!
> > --
> > Adios,
> > Clay Harryman
> >

 
Reply With Quote
 
Alan Beban
Guest
Posts: n/a
 
      19th Jul 2007
Although Chip's sites are always worth visiting, in this case the
quicker answer to your post is "too, bad; you're stuck with what you're
trying to avoid."

Alan Beban

Clayman wrote:
> Ah, yes. Mr. Pearson always has good information. I need to visit his site
> more often.
>
> Thanks for the link. That's a good habit to get into.

 
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
Re: How can this code be made more efficient? Roger Govier Microsoft Excel Programming 0 14th Dec 2006 03:26 PM
Re: How can this code be made more efficient? Mike Woodhouse Microsoft Excel Programming 0 14th Dec 2006 03:24 PM
Re: How can this code be made more efficient? Bob Phillips Microsoft Excel Programming 0 14th Dec 2006 03:17 PM
Can this code be made more efficient ? =?Utf-8?B?c29tYW55YnVnc3NvbGl0dGxldGltZXRvZml4dGhl Microsoft Access Form Coding 1 1st Jul 2004 07:46 PM
Can this procedure be made more efficient? DennisE Microsoft Excel Programming 7 26th Apr 2004 09:55 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:20 PM.