+ Reply to Thread
Results 1 to 7 of 7

Message box/UserForm with lookup

Hybrid View

Dave H9 Message box/UserForm with... 04-06-2011, 03:32 AM
Richard Buttrey Re: Message box/UserForm with... 04-06-2011, 06:25 AM
Dave H9 Re: Message box/UserForm with... 04-06-2011, 06:57 AM
snb Re: Message box/UserForm with... 04-06-2011, 07:21 AM
Richard Buttrey Re: Message box/UserForm with... 04-06-2011, 10:15 AM
Dave H9 Re: Message box/UserForm with... 04-06-2011, 10:04 AM
Dave H9 Re: Message box/UserForm with... 04-06-2011, 10:51 AM
  1. #1
    Forum Contributor Dave H9's Avatar
    Join Date
    03-10-2011
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    418

    Message box/UserForm with lookup

    Hi all, I am a novice with VBA so any help is much appreciated.

    I have attached an example workbook. I am trying to set this up so that when the book is opened a message box appears asking the user if they have a reference number (YES or NO answer), if NO then in the sheet "Input Sheet" cell B2 should read "Not Applicable". If the answer is YES a second message box should request the user type their reference and check this against the sheet "Request Register" column A, if it exists then input into the sheet "Input Sheet" cell B2, if not retry.

    I know this is probably easy for you pro's but I am struggling to even make a start.

    Thank you
    Attached Files Attached Files
    Last edited by Richard Buttrey; 05-25-2011 at 07:24 PM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Message box/UserForm with lookup

    Hi Dave,

    Rather than try and re-invent the wheel with a userform and command buttons, just use the standard Message and Input boxes.

    Call the following procedure from your workbook open event.

    Sub GetReference()
        Dim st As String, lRow As Long
        If MsgBox("Do you have a reference", vbYesNo, Title:="Before you begin") = vbNo Then
            Sheet1.Range("B2") = "Not Applicable"
        Else
            st = InputBox("Please enter your reference")
            On Error Resume Next
            lRow = Sheet2.Range("A:A").Find(What:=st).Row
            If lRow = 0 Then
                MsgBox "Please re-enter your reference"
                End
            Else
                Sheet1.Range("B2") = st
            End If
        End If
    End Sub
    Regards
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor Dave H9's Avatar
    Join Date
    03-10-2011
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    418

    Re: Message box/UserForm with lookup

    Hi Richard,

    Thank you very much, this seems a much simpler approach. I am now trying to include a loop so only a valid reference or N/A is returned in cell B2.

    I'll give it a go myself but any pointers would be appreciated.

    Thanks again

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Message box/UserForm with lookup

    For a simpler approach see this file.
    Last edited by snb; 04-06-2011 at 10:37 AM.



  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Message box/UserForm with lookup

    Hi,

    I like snb's approach of forcing the user to pick from a list, but if you want to use the MsgBox / Input Box route this simpler modified procedure will do what you want.

    Sub GetReference()
        Dim st As String, lRow As Long
        st = InputBox("Please enter your reference", Title:="Before you begin")
        On Error Resume Next
        lRow = Sheet2.Range("A:A").Find(What:=st).Row
        If lRow = 0 Then
            Sheet1.Range("B2") = "#N/A"
        Else
            Sheet1.Range("B2") = st
        End If
    End Sub
    Regards

  6. #6
    Forum Contributor Dave H9's Avatar
    Join Date
    03-10-2011
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    418

    Re: Message box/UserForm with lookup

    Thanks snb but I don't think this suits my application.

    I am trying to get the message to loop if an invalid reference is used, the code I am using is as follows

    Sub GetReference()
        Dim st As String, lRow As Long
        If MsgBox("Do you have a reference", vbYesNo, Title:="Before you begin") = vbNo Then
            Sheet1.Range("B2") = "Not Applicable"
        Else
            st = InputBox("Please enter your reference") 'this section can be exited by closing or cancelling
            On Error Resume Next
            lRow = Sheet2.Range("A:A").Find(What:=st).Row
            If lRow = 0 Then
                Do
                    st = InputBox("REFERENCE NOT FOUND - Please re-enter")
                    On Error Resume Next
                    lRow = Sheet2.Range("A:A").Find(What:=st).Row
                    Sheet1.Range("B2") = st
                Loop Until lRow > 0    ' Exit outer loop immediately.
                Sheet1.Range("B2") = "Not Applicable"
            Else
                Sheet1.Range("B2") = st
            End If
        End If
           
    End Sub
    I am probably missing the obvious but I have a few bugs I could do with sorting
    1. The first inputbox can be closed or cancelled by the user. I managed to have the input box in the loop return "Not Applicable" if it is cancelled or closed but can't get it to work on the first.
    2. Either of the input boxes will accept a value of "0". I'm sure this is due to the "lRow" element but I cannot find a workround.

    Any suggestions please?

  7. #7
    Forum Contributor Dave H9's Avatar
    Join Date
    03-10-2011
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    418

    Re: Message box/UserForm with lookup

    Thanks, I am trying to develop as part of a userform but at the moment I need to address the two bugs. I can then use a modified version to include in a userform.

    Thank you

+ 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