Solved

VBA between statement

Posted on 2008-10-03
9
2,078 Views
Last Modified: 2013-11-28
In VBA, how do I write an expression that limits BETWEEN to values.  For example:

If [Lookup]= 1 to 10 Then msgbox "found it"

The above doesn't work in VBA but this is what I want to do.  I want it to look for the values 1 to 10.

How can I make this work?

Thanks.
 
0
Comment
Question by:dddw
  • 4
  • 3
  • 2
9 Comments
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 250 total points
ID: 22633783
If [Lookup] >= 1  AND [Lookup] <= 10 Then msgbox "found it"
0
 

Author Comment

by:dddw
ID: 22633883
This helps.  I should have elaborated a little more.  The field [Lookup] has 1 to 30 options.  I want it to find only the option from 10 to 20.  Would code If [Lookup] >= 10  AND [Lookup] <= 20 Then msgbox "found it" still work?  
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 total points
ID: 22634020
Select Case [Lookup]
   Case 1 to 10
      'do 1-10 stuff
   Case 11 to 20
      'do 11-20 stuff
   Case Else
       'do anything else stuff
End Select

>I should have elaborated a little more.
One of the more annoying asker issues is the continuous stream of follow-on questions, so yes please try to state all your requirements up front in the original question.  Thanks in advance.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 250 total points
ID: 22634208
>>  Would code If [Lookup] >= 10  AND [Lookup] <= 20 Then msgbox "found it" still work?

For that specific case -- yes (no harm in trying it)

That said, Jim's approach is far more flexible if there are any other missing details :-)

0
 

Author Comment

by:dddw
ID: 22634337
So would the following work.  Thanks for your patience.

Select Case [Lookup]
   Case 1 to 10
      If [Lookup] >= 1 AND [Lookup] <= 10 Then msgbox "Found 1-10"
   Case 11 to 20
      If [Lookup] >= 11 AND [Lookup] <= 20 Then msgbox "Found 11-20"
   Case Else
       msgbox "not found"
End Select
0
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 250 total points
ID: 22634379
You're combining independant solutions, and creating redundant code.

That will work, but is unnecessarily complicated.

You have two solutions here:

- An If-Then statement
- A Select Case block

They both work fine independantly of one another.

Try coding them up -- and testing them...
0
 

Author Closing Comment

by:dddw
ID: 31502746
Thank you.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 22634418
A Select-Case block is like a very readable mega if-then-else block...
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 22634471
Thanks for the split.  Good luck with your project.  -Jim
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question