+ Reply to Thread
Results 1 to 15 of 15

Thread: VBA to prompted user for employee number.

  1. #1
    Registered User
    Join Date
    01-26-2012
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    15

    VBA to prompted user for employee number.

    Hello,
    I really need some help on this. So, basically what I am trying to do is to create an inventory control sheet. I have many buttons on a spreadsheet (IN and OUT, all in column A) that when IN is pressed I want IN to apear in column D and the row coresponding to the button that was pushed. I also want the user to be prompted with an input box to enter the employee number and then have it placed in E and the coresponding row for that button. Any help on this would be greatly apriciated.
    Last edited by Absent; 01-27-2012 at 02:45 PM.

  2. #2
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: VBA to prompted user for employee number.

    Hi Absent
    Assign this code to your Command Button
    Option Explicit
    Sub test()
        Dim r As Range
        Dim eNumber As String
    
        Set r = ActiveSheet.Buttons(Application.Caller).TopLeftCell
        eNumber = Application.InputBox _
                (Prompt:="Please enter your Employee Number.", _
                Title:="Employee Number", Type:=2)
        If eNumber = "" Then
            MsgBox "Please Enter Your Employee Number"
            Exit Sub
        Else
            Range(Cells(r.Row, 5), Cells(r.Row, 5)).Value = eNumber
        End If
        Range(Cells(r.Row, 4), Cells(r.Row, 4)).Value = ActiveSheet.Buttons(Application.Caller).Characters.Text
    End Sub
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    01-26-2012
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: VBA to prompted user for employee number.

    that solves my problem a bit, in terms of that the button will alow me to copy it to all rows and will stay with its respective row. but i have two butons, and in button and an out button. I have 100 rows. so far i have ahd to right 100 vba for the in button and 100 for the out button.

    the in button code i have so far is---

    Sub Check_IN_2()
    '
    ' Check_IN Macro
    '

    '
    Range("$D2").Select
    ActiveCell.FormulaR1C1 = "IN"
    Range("E2").Select
    Selection.ClearContents
    End Sub


    and the code i have for the out button is ----

    Sub Check_out_2()
    '
    ' Check_out Macro
    '

    '
    Range("$D2").Select
    ActiveCell.FormulaR1C1 = "OUT"
    Dim MYNUMBER
    MYNUMBER = InputBox("Please Enter Employee Number", "MY NUMBER INPUT BOX")
    Range("E2").Value = MYNUMBER
    End Sub

    i know there is a way to consolidate all these lines into one code that will run dynamically and prevent me from having to write them all into 200 different macros

  4. #4
    Registered User
    Join Date
    01-26-2012
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: VBA to prompted user for employee number.

    jaslake,
    also the code that you wrote writes "Button 6" in column D instead of in or out depending on which button is clicked.

  5. #5
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: VBA to prompted user for employee number.

    Hi absent
    First, you MUST use code tags around any code you post to a Thread. Click on Forum Rules...it describes how to do this.
    Please post a dummy file so I can see what your worksheet looks like...are both buttons on the same row? In the same cell?

    Regarding this
    also the code that you wrote writes "Button 6" in column D instead of in or out depending on which button is clicked
    The code is picking up the Text of the Button that's clicked. I just assumed the Buttons were labeled "IN" and "OUT".

    So, show me what you have...perhaps I can help.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  6. #6
    Registered User
    Join Date
    01-26-2012
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: VBA to prompted user for employee number.

    My apologies jaslake,

    the in code is...
    Sub Check_IN_2()
    '
    ' Check_IN Macro
    '
    
    '
        Range("$D2").Select
        ActiveCell.FormulaR1C1 = "IN"
        Range("E2").Select
        Selection.ClearContents
    End Sub
    and this is the out code ...
    Sub Check_out_2()
    '
    ' Check_out Macro
    '
    
    '
        Range("$D2").Select
        ActiveCell.FormulaR1C1 = "OUT"
            Dim MYNUMBER
    MYNUMBER = InputBox("Please Enter Employee Number", "MY NUMBER INPUT BOX")
    Range("E2").Value = MYNUMBER
    End Sub
    here is the requested sample, keep in mind it extends for about 100 rows
    sample.jpg

    yes the in and out buttons are in the same cell, it would be nice to be able to place the two buttons and then just copy them all the way through the 100 rows

  7. #7
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: VBA to prompted user for employee number.

    Hi absent
    No apologies necessary...don't want you to get hammered by a Moderator.
    I really can't do much with a picture. Send me a copy of your workbook (with existing code). Eliminate (or change) proprietary information. If you're not willing to do so, I'll need to do it myself and I prefer to not guess.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  8. #8
    Registered User
    Join Date
    01-26-2012
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: VBA to prompted user for employee number.

    Not a problem. Rows 1-20 have my existing code on it.
    Attached Files Attached Files

  9. #9
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: VBA to prompted user for employee number.

    Hi absent

    This code is in the attached for each Command Button in the worksheet and appears to do as you require. To make it easier for you to copy the Command Buttons to additional rows, click on the Office Button, click on Excel Options, click on Customize, in the Drop Down "Commands Not in Ribbon" select "Select Multiple Objects". This will allow you to copy the Command Buttons to additional rows (of course, you MUST DELETE the pre-existing Command Buttons before you copy the new ones). The same code applies to ALL command buttons. Let me know how you make out.

    Option Explicit
    Sub test()
        Dim r As Range
        Dim eNumber As String
    
        Set r = ActiveSheet.Buttons(Application.Caller).TopLeftCell
    
        Select Case ActiveSheet.Buttons(Application.Caller).Characters.Text
        Case "OUT"
            eNumber = Application.InputBox _
                    (Prompt:="Please enter your Employee Number.", _
                    Title:="Employee Number", Type:=2)
            If eNumber = "" Then
                MsgBox "Please Enter Your Employee Number"
                Exit Sub
            Else
                Range(Cells(r.Row, 5), Cells(r.Row, 5)).Value = eNumber
                Range(Cells(r.Row, 4), Cells(r.Row, 4)).Value = ActiveSheet.Buttons(Application.Caller).Characters.Text
            End If
        Case "IN"
            Range(Cells(r.Row, 4), Cells(r.Row, 4)).Value = ActiveSheet.Buttons(Application.Caller).Characters.Text
            Range(Cells(r.Row, 5), Cells(r.Row, 5)).Value = ""
        End Select
    End Sub
    Attached Files Attached Files
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  10. #10
    Registered User
    Join Date
    01-26-2012
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: VBA to prompted user for employee number.

    Great, that works really well! thank you very much. writing individual vba for each button was a very tedious task. thank you for making it easier. I have one more question though. would it be possible to easily add a confirmation box when clicking the in button, one that requires a yes or no responce and when you click no it terminates the script?

  11. #11
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: VBA to prompted user for employee number.

    Hi Absent
    Try this code...replace the underlined portion with your question
    Option Explicit
    Sub test()
        Dim r As Range
        Dim eNumber As String
        Dim Answer As String
    
        Set r = ActiveSheet.Buttons(Application.Caller).TopLeftCell
    
        Select Case ActiveSheet.Buttons(Application.Caller).Characters.Text
        Case "OUT"
            eNumber = Application.InputBox _
                    (Prompt:="Please enter your Employee Number.", _
                    Title:="Employee Number", Type:=2)
            If eNumber = "" Then
                MsgBox "Please Enter Your Employee Number"
                Exit Sub
            Else
                Range(Cells(r.Row, 5), Cells(r.Row, 5)).Value = eNumber
                Range(Cells(r.Row, 4), Cells(r.Row, 4)).Value = ActiveSheet.Buttons(Application.Caller).Characters.Text
            End If
        Case "IN"
            Answer = MsgBox("Type your question here", vbYesNo, "Proceed?")
            If Answer = vbYes Then
                Range(Cells(r.Row, 4), Cells(r.Row, 4)).Value = ActiveSheet.Buttons(Application.Caller).Characters.Text
                Range(Cells(r.Row, 5), Cells(r.Row, 5)).Value = ""
            Else
                Exit Sub
            End If
        End Select
    End Sub
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  12. #12
    Registered User
    Join Date
    01-26-2012
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: VBA to prompted user for employee number.

    That is Great, i almost had it correct, i was missing the IF answer= thank you so much. one other thing that im missing in that im trying to have the cancel box in the employee number input box cancel the script from running when cancel is pressed. i know it an If statement that needs to be put in but but i keep getting Comile error: Variable not Defined. any suggestions?

  13. #13
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: VBA to prompted user for employee number.

    Hi Absent

    Try this
    Option Explicit
    Sub test()
        Dim r As Range
        Dim eNumber As String
        Dim Answer As String
    
        Set r = ActiveSheet.Buttons(Application.Caller).TopLeftCell
    
        Select Case ActiveSheet.Buttons(Application.Caller).Characters.Text
        Case "OUT"
    TryAgain:
            eNumber = Application.InputBox _
                    (Prompt:="Please enter your Employee Number.", _
                    Title:="Employee Number", Type:=2)
            If eNumber = "False" Then Exit Sub
            If eNumber = "" Then
                MsgBox "Please Enter Your Employee Number"
                GoTo TryAgain
            Else
                Range(Cells(r.Row, 5), Cells(r.Row, 5)).Value = eNumber
                Range(Cells(r.Row, 4), Cells(r.Row, 4)).Value = ActiveSheet.Buttons(Application.Caller).Characters.Text
            End If
        Case "IN"
            Answer = MsgBox("Type your question here", vbYesNo, "Proceed?")
            If Answer = vbYes Then
                Range(Cells(r.Row, 4), Cells(r.Row, 4)).Value = ActiveSheet.Buttons(Application.Caller).Characters.Text
                Range(Cells(r.Row, 5), Cells(r.Row, 5)).Value = ""
            Else
                Exit Sub
            End If
        End Select
    End Sub
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  14. #14
    Registered User
    Join Date
    01-26-2012
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: VBA to prompted user for employee number.

    greta! thank you so much. It works!

  15. #15
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: VBA to prompted user for employee number.

    Hi Absent
    Happy I could help.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0