+ Reply to Thread
Results 1 to 5 of 5

List names, InputBox, check if name is on the list

Hybrid View

Laurence000 List names, InputBox, check... 09-07-2009, 06:31 AM
dominicb Re: List names, InputBox,... 09-07-2009, 06:46 AM
Laurence000 Re: List names, InputBox,... 09-07-2009, 07:59 AM
romperstomper Re: List names, InputBox,... 09-07-2009, 08:11 AM
Laurence000 Re: List names, InputBox,... 09-07-2009, 09:20 AM
  1. #1
    Registered User
    Join Date
    09-07-2009
    Location
    Denmark
    MS-Off Ver
    Excel 2007
    Posts
    3

    List names, InputBox, check if name is on the list

    Hi

    I'm new to VBA programming and still doing very basic stuff.

    I have a small homework to do and I'm really struggling, although I'm sure it's quite easy.

    I have an excel sheet with 30 names in column A and I'm asked to do the following:

    Write a procedure – a sub – that uses an input box to ask for a name. The procedure should
    then scan through the names in the list to search for the name and make a message box to
    state “X is not in the group” or “X is in the group”, where X is the name from the input box.

    HINT: You can make a variable “found” that starts with the value 0 and gets the value 1 if the name
    is found in the list.


    Can anyone give me a hint more, or help. I've tried many different things, but I can't get figure out the way to have the name variable which is entered in the input box to be compared to the 30 names on the list.

    Thanks so much!

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile Re: List names, InputBox, check if name is on the list

    Good morning Laurence000

    ...and welcome to the forum!!
    Quote Originally Posted by Laurence000 View Post
    Can anyone give me a hint more, or help. I've tried many different things, but I can't get figure out the way to have the name variable which is entered in the input box to be compared to the 30 names on the list.
    We're not really here to do your homework for you That's for you to do, with your teacher's assistance.

    That said, we're not monsters - we've all struggled with stuff before and needed help. Try posting what you've already got and someone will give you a shove in the right direction.

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  3. #3
    Registered User
    Join Date
    09-07-2009
    Location
    Denmark
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: List names, InputBox, check if name is on the list

    Hi and thanks for wanting to help. I understand you aint gonna do my exercises for me, so Ill show you what Ive done so far. That being said, this is for an Advance Excel class in Graduate Business School and the professor is going really fast...

    So here is what Ive done so far: I made different attempts

    Here is the 1st one:

    Public Sub NameListCheck()
        Dim Name As String
        
        
        Name = InputBox("What is your name?")
            If Name = Range("A:A") Then
                MsgBox ("Your name is on the list")
            Else
                MsgBox ("Your Name is not on the list")
            End If
    End Sub
    This didnt work due to: If Name = Range("A:A") Then

    So I continued and tried this (which was a bad idea):
    Public Sub NameListCheck()
        Dim Name As String
        Dim x As Range
        
        Name = InputBox("What is your name?")
        For Each x in Range("A:A")
    
              If Name = x Then
                MsgBox ("Your name is on the list")
            Else
                MsgBox ("Your Name is not on the list")
            End If
       Next
    End Sub
    This didnt work either and the loop never ends so I had to terminate excel....

    I tried a third thing:

    Public Sub NameListCheck()
        Dim Name As String
        Dim x As Variant
        Dim found As Boolean
        
        
        Name = InputBox("What is your name?")
        
        For Each x In Range("A1:A30")
            If Name = x Then
                found = True
            Else
                found = False
            End If
        Next
        
            If found = True Then
                MsgBox ("Your name is on the list")
            Else
                MsgBox ("your name is not on the list")
            End If
            
    End Sub
    This continuously displays the msgbox "your name is not on the list" no matter if I write a name that is on the list or not.

    I tried with the variable found as an integer either:
    Public Sub NameListCheck()
        Dim Name As String
        Dim x As Variant
        Dim found As Integer
        
        
        Name = InputBox("What is your name?")
        
        For Each x In Range("A1:A30")
            If Name = x Then
                found = 1
            Else
                found = 2
            End If
        Next
          
        If found = 1 Then
                MsgBox ("Your name is on the list")
        Else
                MsgBox ("your name is not on the list")
            End If
            
    End Sub
    Still... same problem. I dont really know what to do with this...

    Im sure the solution is super simple and I just don't think about this the right way (direction) so if anyone, wants to give me a hint or let me know what I am not doing properly here, I will be SUPER happy.

    thanks a lot!
    Last edited by dominicb; 09-07-2009 at 08:11 AM.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,009

    Re: List names, InputBox, check if name is on the list

    I'll take the last version. If the Name is found, you set the value of Found equal to one. But then you carry on checking each cell so if the next cell is not the Name you are looking for, you then set Found to 2. What you need to do is exit the loop if it's found:
    Public Sub NameListCheck()
    Dim Name As String
    Dim x As Variant
    Dim found As Integer
    
    
    Name = InputBox("What is your name?")
    
    ' set default value
    Found = 2
    For Each x In Range("A1:A30")
    If Name = x Then
    ' found it, so set value and exit loop
    found = 1
    Exit for
    End If
    
    Next x
    
    If found = 1 Then
    MsgBox "Your name is on the list"
    Else
    MsgBox "your name is not on the list"
    End If
    
    End Sub
    Note that you should also not have parentheses around the MsgBox arguments. You only use parentheses (basically) if you are returning a value from a function or if you are using the Call statement (which is unnecessary).

    Finally, you can actually make use of worksheet functions in VBA and skip the loop altogether:
    Public Sub NameListCheck()
    Dim Name As String
    
    
    Name = InputBox("What is your name?")
    
    If Application.WorksheetFunction.Countif(Range("A1:A30"), Name) > 0 Then
    MsgBox "Your name is on the list"
    Else
    MsgBox "your name is not on the list"
    End If
    
    End Sub
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Registered User
    Join Date
    09-07-2009
    Location
    Denmark
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: List names, InputBox, check if name is on the list

    Thanks so much!

    I understand both coding you showed me and it makes so much sense!

    Thanks especially for the second one, I had no idea we could shortcut in such a way! I learned important things today!

    Thanks again!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1