|
-
May 5th, 2010, 10:54 AM
#1
Registered User
Using IF to find a value in an array.
Hello,
I'm writing some VBA code for an Excel sheet.
What I want to do is run an IF statement to run some code. The IF has to be triggered if a value in a target cell is in an array.
So if the target cell value is in the array the code runs, if it isn't I use Else, EndIF to bypass the code.
I'm at a loss and would appreciate any help.
-
May 5th, 2010, 11:18 AM
#2
Registered User
This will mark odd numbers smaller than 10, they are defined in the OddNumbers array
Code:
'--trigger a check on cell edit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
CheckNumber Target
End Sub
Private Sub CheckNumber(ByVal Target As Excel.Range)
Dim OddNumbers As Variant
Dim IsOdd As Boolean
Dim i As Integer
Dim CellVal As Integer
OddNumbers = Array(1, 3, 5, 7, 9)
CellVal = Val(Target.Value)
i = LBound(OddNumbers)
'--check if number is in array
While i <= UBound(OddNumbers) And IsOdd = False
If CellVal = OddNumbers(i) Then
IsOdd = True
End If
i = i + 1
Wend
If IsOdd = True Then
MarkNumberAsOdd Target
End If
End Sub
Private Sub MarkNumberAsOdd(ByVal Target As Excel.Range)
ActiveSheet.Cells(Target.Row, Target.Column + 1) = "is odd"
End Sub
Similar Threads
-
By musicman7722 in forum Windows Server 2003 & Windows Home Server
Replies: 12
Last Post: July 17th, 2007, 12:26 PM
-
By mikeys in forum Windows XP
Replies: 0
Last Post: December 29th, 2006, 09:39 PM
-
By phx1 in forum Video Adapter/Monitor Drivers
Replies: 6
Last Post: April 10th, 2005, 07:15 PM
-
By NooNoo in forum Tech Lounge & Tales
Replies: 555
Last Post: June 3rd, 2004, 12:47 PM
-
By Patrick/roe in forum Video Adapter/Monitor Drivers
Replies: 5
Last Post: June 4th, 1999, 01:58 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|
Bookmarks