Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default If/Then Macro

How do I write an If/Then/Else macro to work with a range
of values. For example, if a number is between 0 and 5% do
one thing but if it is between 0 and minus 5% do something
else. I have 10 ranges between -20% and +20%. Thanks.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default If/Then Macro

Check out the Select Case Statement in the VBA help Ken

--
Regards Ron de Bruin
(Win XP Pro SP-1 XL2002 SP-2)
www.rondebruin.nl



"Ken McDaniel" wrote in message ...
How do I write an If/Then/Else macro to work with a range
of values. For example, if a number is between 0 and 5% do
one thing but if it is between 0 and minus 5% do something
else. I have 10 ranges between -20% and +20%. Thanks.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default If/Then Macro

use a case statment

Sub AAAA()
Dim cell As Range
For Each cell In Range("A1:A3")

Select Case cell.Value
Case -0.2 To -0.15
MsgBox "-0.2 to -0.15"

Case -0.15 To -0.1
MsgBox "-0.15 To -0.1"

Case -0.1 To -0.05
MsgBox "-0.1 To -0.05"

Case -0.05 To 0#
MsgBox "0.05 To 0.0"

Case 0# To 0.05
MsgBox "0.0 To 0.05"

Case 0.05 To 0.1
MsgBox "0.05 To 0.1"

Case 0.1 To 0.15
MsgBox "0.1 To 0.15"

Case 0.15 To 0.2
MsgBox "-0.2 to -0.15"

Case Else
MsgBox "out of bounds"
End Select
Next

End Sub


You could also calculate what interval it is in, but the usefulness of that
would depend on what kinds of things you are going to do.

--
Regards,
Tom Ogilvy




"Ken McDaniel" wrote in message
...
How do I write an If/Then/Else macro to work with a range
of values. For example, if a number is between 0 and 5% do
one thing but if it is between 0 and minus 5% do something
else. I have 10 ranges between -20% and +20%. Thanks.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,337
Default If/Then Macro

try this idea
Sub ifnum()
If [i1] 20 Then [k2] = 20
If [i1] 15 Then [k2] = 15
'etc
End Sub

"Ken McDaniel" wrote in message
...
How do I write an If/Then/Else macro to work with a range
of values. For example, if a number is between 0 and 5% do
one thing but if it is between 0 and minus 5% do something
else. I have 10 ranges between -20% and +20%. Thanks.



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
Macro recorded... tabs & file names changed, macro hangs Steve Excel Worksheet Functions 3 October 30th 09 11:41 AM
Macro Help Needed - Excel 2007 - Print Macro with Auto Sort Gavin Excel Worksheet Functions 0 May 17th 07 01:20 PM
My excel macro recorder no longer shows up when recording macro jack Excel Discussion (Misc queries) 1 February 5th 07 09:31 PM
My excel macro recorder no longer shows up when recording macro jack Excel Discussion (Misc queries) 3 February 5th 07 08:22 PM


All times are GMT +1. The time now is 04:01 PM.

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

About Us

"It's about Microsoft Excel"