How do we search arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan Brown

    How do we search arrays

    I am writing a program to search for names. Right now I have it setup for as all the names to be in a Array.

    Code:
    Dim Names(4) as String
    Movies(0)=Ryan
    Movies(1)=Philip
    Movies(2)=Roger
    Movies(3)=Charles
    Movies(4)=Tim
    Now what I would like to do is have a program written that would search the array and tell me that the name is in the array or not. Another question that I have is would it matter if the names in the array were lower case or upper case. If so will I have to create a new array for lower case names.

    What I kind of have so far is this

    Code:
    If input1=movies() Then
    msgbox("Congrats that name is in the array
    end if
    If input1<>movies() Then
    msgbox("Please try again")
    end if
    Will this work???
    Thanks for your time and any help that you may give.
    Last edited by MMcCarthy; Oct 24 '10, 08:39 AM. Reason: added code tags
  • Rodney Roe
    New Member
    • Oct 2010
    • 61

    #2
    I'm not the best programer but this should work;

    Code:
    Sub TEST()
    Dim Names(5) As String
    Dim chkNames As String
    Dim chkarray As Boolean
    
    chkname = "Tim"
    
    Names(0) = "Ryan"
    Names(1) = "Philip"
    Names(2) = "Roger"
    Names(3) = "Charles"
    Names(4) = "Tim"
    
    For I = 0 To 4
        If UCase(chkname) = UCase(Names(I)) Then
            chkarray = True
            Exit For
        Else
            chkarray = False
        End If
    Next
    
    End Sub
    This code should take care of wether the string is lower case, uper case, or mixed as well.

    Comment

    • Ryan Brown
      New Member
      • Oct 2010
      • 1

      #3
      Ok that code helps me out a lot. But I have one problem that I just cant figure out with it. If I search for the name Ryan(which is the first name in the array) it finds it no problem. When I seach for the second or third of fourth name it cant find it. So bascially its not searching the rest of the array.

      Heres the code that I have right now

      Names(0) = "Ryan"
      Names(1) = "Andy"
      Names(2) = "Tim"
      Names(3) = "Roger"
      Names(4) = "Charles"

      For I = 0 To 5
      If UCase(chkname) = UCase(Names(I)) Then
      msgbox("Found")
      Exit For

      Else
      MsgBox ("Please Enter A Name")
      Exit For
      End If
      Next

      Any help would be appreciated

      Comment

      • Guido Geurs
        Recognized Expert Contributor
        • Oct 2009
        • 767

        #4
        1st: the array is from 0 to 4 ! NOT For I = 0 To 5
        2nd: when you check the first name, and its not valid, the ELSE set msgbox and EXIT FOR: so you don't loop through the rest of the values.

        Comment

        • Rodney Roe
          New Member
          • Oct 2010
          • 61

          #5
          Take out the "exit for" & the msgbox after the else statement like ggeu said. If you leave it there the program exits the loop before it can search the rest of the array.

          Comment

          Working...