Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 39
Default More Efficient IF

Hello

I've got a convaluted IF statement:

--

If Variable = 1 or Variable = 3 or Variable = 7 or Variable = 12 Then

--

Using other languages you could for example do something like this:

--

If Variable MATCHES (1,3,7,12) Then

--

What would be the best way using VBA? Do you increase the efficiency
of the code by adding brackets etc, as in PICK Basic?

Any other general efficiency tips would be appreciated as I've been
asked to update some complex code that was written a long time ago by
one of my ex-colleagues and is deemed inefficient, but I'm not expert
when it comes to VBA, as you probably noticed!

Thanks!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default More Efficient IF

I don't consider your If-Then statement to be convoluted at all... it is how
I would write it my own code. If you are looking for something more
"concise", well, you could use either of these...

If InStr("*1*3*7*12*", "*" & Variable & "*") Then

If Variable Like "[137]" Or Variable = 12 Then

but personally, I think you will find them harder to understand 6 months
from now if you have to come back to maintain or modify your code.

Rick


"David" wrote in message
ups.com...
Hello

I've got a convaluted IF statement:

--

If Variable = 1 or Variable = 3 or Variable = 7 or Variable = 12 Then

--

Using other languages you could for example do something like this:

--

If Variable MATCHES (1,3,7,12) Then

--

What would be the best way using VBA? Do you increase the efficiency
of the code by adding brackets etc, as in PICK Basic?

Any other general efficiency tips would be appreciated as I've been
asked to update some complex code that was written a long time ago by
one of my ex-colleagues and is deemed inefficient, but I'm not expert
when it comes to VBA, as you probably noticed!

Thanks!


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default More Efficient IF

You can use the worksheet functtion MATCH(). the prblem witth using this
method if the item is not found an error will occur. So you must handle the
errror

Variable = 7
On Error Resume Next
Results = WorksheetFunction.Match(Variable, Array(1, 3, 7, 12), 0)
On Error GoTo 0
If IsEmpty(Results) Then
MsgBox ("Did not find match")
End If

"David" wrote:

Hello

I've got a convaluted IF statement:

--

If Variable = 1 or Variable = 3 or Variable = 7 or Variable = 12 Then

--

Using other languages you could for example do something like this:

--

If Variable MATCHES (1,3,7,12) Then

--

What would be the best way using VBA? Do you increase the efficiency
of the code by adding brackets etc, as in PICK Basic?

Any other general efficiency tips would be appreciated as I've been
asked to update some complex code that was written a long time ago by
one of my ex-colleagues and is deemed inefficient, but I'm not expert
when it comes to VBA, as you probably noticed!

Thanks!


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default More Efficient IF

You can avoid using "on error.." by using the IsError
function:

validValues = Array(1, 3, 5, 7, 11)

if IsError(Application.Match(x, validValues, 0)) then
' x is not in list
Else
' x is in list
End If

I don't expect it makes much difference either way
in terms of efficiency, but it's more maintainable and
less error-prone.



On Sep 28, 12:04 pm, Joel wrote:
You can use the worksheet functtion MATCH(). the prblem witth using this
method if the item is not found an error will occur. So you must handle the
errror

Variable = 7
On Error Resume Next
Results = WorksheetFunction.Match(Variable, Array(1, 3, 7, 12), 0)
On Error GoTo 0
If IsEmpty(Results) Then
MsgBox ("Did not find match")
End If



"David" wrote:
Hello


I've got a convaluted IF statement:


--


If Variable = 1 or Variable = 3 or Variable = 7 or Variable = 12 Then


--


Using other languages you could for example do something like this:


--


If Variable MATCHES (1,3,7,12) Then


--


What would be the best way using VBA? Do you increase the efficiency
of the code by adding brackets etc, as in PICK Basic?


Any other general efficiency tips would be appreciated as I've been
asked to update some complex code that was written a long time ago by
one of my ex-colleagues and is deemed inefficient, but I'm not expert
when it comes to VBA, as you probably noticed!


Thanks!- Hide quoted text -


- Show quoted text -





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default More Efficient IF

Andrew: I don't like On Error statements. I try to avoid them whenever
possible. But in this case in excel 2003 your solution doesn't work. I
tried your solution before my posting and it failed so I was forced to use
the On Error method.

"Andrew Taylor" wrote:

You can avoid using "on error.." by using the IsError
function:

validValues = Array(1, 3, 5, 7, 11)

if IsError(Application.Match(x, validValues, 0)) then
' x is not in list
Else
' x is in list
End If

I don't expect it makes much difference either way
in terms of efficiency, but it's more maintainable and
less error-prone.



On Sep 28, 12:04 pm, Joel wrote:
You can use the worksheet functtion MATCH(). the prblem witth using this
method if the item is not found an error will occur. So you must handle the
errror

Variable = 7
On Error Resume Next
Results = WorksheetFunction.Match(Variable, Array(1, 3, 7, 12), 0)
On Error GoTo 0
If IsEmpty(Results) Then
MsgBox ("Did not find match")
End If



"David" wrote:
Hello


I've got a convaluted IF statement:


--


If Variable = 1 or Variable = 3 or Variable = 7 or Variable = 12 Then


--


Using other languages you could for example do something like this:


--


If Variable MATCHES (1,3,7,12) Then


--


What would be the best way using VBA? Do you increase the efficiency
of the code by adding brackets etc, as in PICK Basic?


Any other general efficiency tips would be appreciated as I've been
asked to update some complex code that was written a long time ago by
one of my ex-colleagues and is deemed inefficient, but I'm not expert
when it comes to VBA, as you probably noticed!


Thanks!- Hide quoted text -


- Show quoted text -




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default More Efficient IF

If you are looking for efficiencies, looking at this one is pole vaulting
over mouse turds. I suspect you need to look at the logic and algorithms of
the code to harvest major improvements - unless it is using a lot of recorder
style code such as select and selection, or it does a lot of screen updating
or is slowed down by the internal calculation of formulas in the worksheet or
it has inherent recursive calls in events.

Back to the If statement, It may be cumbersome to write, but it would't be
inefficient when executed. I suspect PICK basic evaluates your Match
statement slower than or equal to the way VBA evaluates this construct.

Using the worksheet function MATCH as pointed out by Joel would be a much
slower way to do it.

--
regards,
Tom Ogilvy



"David" wrote:

Hello

I've got a convaluted IF statement:

--

If Variable = 1 or Variable = 3 or Variable = 7 or Variable = 12 Then

--

Using other languages you could for example do something like this:

--

If Variable MATCHES (1,3,7,12) Then

--

What would be the best way using VBA? Do you increase the efficiency
of the code by adding brackets etc, as in PICK Basic?

Any other general efficiency tips would be appreciated as I've been
asked to update some complex code that was written a long time ago by
one of my ex-colleagues and is deemed inefficient, but I'm not expert
when it comes to VBA, as you probably noticed!

Thanks!


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default More Efficient IF

No, it works fine in xl2003. You didn't replace WorksheetFunction with
Application as Andrew suggested.

--
Regards,
Tom Ogilvy


"Joel" wrote:

Andrew: I don't like On Error statements. I try to avoid them whenever
possible. But in this case in excel 2003 your solution doesn't work. I
tried your solution before my posting and it failed so I was forced to use
the On Error method.

"Andrew Taylor" wrote:

You can avoid using "on error.." by using the IsError
function:

validValues = Array(1, 3, 5, 7, 11)

if IsError(Application.Match(x, validValues, 0)) then
' x is not in list
Else
' x is in list
End If

I don't expect it makes much difference either way
in terms of efficiency, but it's more maintainable and
less error-prone.



On Sep 28, 12:04 pm, Joel wrote:
You can use the worksheet functtion MATCH(). the prblem witth using this
method if the item is not found an error will occur. So you must handle the
errror

Variable = 7
On Error Resume Next
Results = WorksheetFunction.Match(Variable, Array(1, 3, 7, 12), 0)
On Error GoTo 0
If IsEmpty(Results) Then
MsgBox ("Did not find match")
End If



"David" wrote:
Hello

I've got a convaluted IF statement:

--

If Variable = 1 or Variable = 3 or Variable = 7 or Variable = 12 Then

--

Using other languages you could for example do something like this:

--

If Variable MATCHES (1,3,7,12) Then

--

What would be the best way using VBA? Do you increase the efficiency
of the code by adding brackets etc, as in PICK Basic?

Any other general efficiency tips would be appreciated as I've been
asked to update some complex code that was written a long time ago by
one of my ex-colleagues and is deemed inefficient, but I'm not expert
when it comes to VBA, as you probably noticed!

Thanks!- Hide quoted text -

- Show quoted text -




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
What is more efficient Brad Excel Discussion (Misc queries) 2 November 20th 06 09:13 PM
more efficient VBA? markx Excel Programming 4 August 7th 06 05:41 PM
Is there a more efficient way to do this? Steve Roberts Excel Programming 1 September 26th 05 05:34 PM
More efficient way? Steph[_3_] Excel Programming 6 June 23rd 04 09:34 PM
Which is more efficient? Norm[_5_] Excel Programming 3 April 2nd 04 04:24 PM


All times are GMT +1. The time now is 12:59 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"