+ 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

    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.

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

    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.

  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

    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