Using IF to find a value in an array.
Results 1 to 2 of 2

Thread: Using IF to find a value in an array.

  1. #1
    Registered User paul_kingtiger's Avatar
    Join Date
    May 2003
    Location
    London - UK
    Posts
    29

    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.

  2. #2
    Registered User CeeBee's Avatar
    Join Date
    Nov 2002
    Location
    USA
    Posts
    2,494
    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

  1. HDD upgrade in a RAID array?
    By musicman7722 in forum Windows Server 2003 & Windows Home Server
    Replies: 12
    Last Post: July 17th, 2007, 12:26 PM
  2. Replies: 0
    Last Post: December 29th, 2006, 09:39 PM
  3. Can't find a driver at all for my video card! Someone please help!
    By phx1 in forum Video Adapter/Monitor Drivers
    Replies: 6
    Last Post: April 10th, 2005, 07:15 PM
  4. PreciousAngel's Email - culprit known.
    By NooNoo in forum Tech Lounge & Tales
    Replies: 555
    Last Post: June 3rd, 2004, 12:47 PM
  5. [RESOLVED] Can't find a driver
    By Patrick/roe in forum Video Adapter/Monitor Drivers
    Replies: 5
    Last Post: June 4th, 1999, 01:58 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •