Solved

VBA between statement

Posted on 2008-10-03
9
2,077 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
 
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now