+ Reply to Thread
Results 1 to 11 of 11

Rememberign which command button was pressed in a userform

  1. #1
    Forum Contributor vamosj's Avatar
    Join Date
    04-23-2004
    Location
    Oregon
    MS-Off Ver
    2010
    Posts
    294

    Rememberign which command button was pressed in a userform

    I have a userform that has around 100 command buttons on them.

    Two are associated with each label on the useform.

    The labels are populated with the names of work positions (dynamic).

    My vision on this is to have the supervisor be able to pull up this userform, which shows all the employment positions. they can then hit one of two buttons (color coded command buttons) next to a label to have it either move up the list or down the list until they are happy with the results.

    I was wondering if there was a way to recall what button was pressed in that specific button's code, have it retain that number, and then run a macro that would adjust the labels accordingly.

    Example:
    Please Login or Register  to view this content.
    I have name associations with each label to command button (i.e.. label 2 is associated with commandbutton2 for move up the list and commandbutton 52 to move down the list).

    I know this could be done by going through each individual command button and just having a variable assigned a number and have it carry over, but was wondering if there was a way to do this for learning sake.
    Janos S. Vamos
    Data Systems Technician/Fire Controlman PO1(SW/AW)
    US Navy Retired


    Remember, Record Macro can be your friend for figuring out solutions.

    Good or Bad, let me know how I did by clicking on the "Add Reputation" * just to the lower left of here. it only takes a few seconds to let someone know.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Rememberign which command button was pressed in a userform

    I don't really understand what you are trying to achieve here.

    But I wrote a Userform last year that had numerous textboxes, listboxes, labels etc.

    It got so unwieldy that I had to return to basics.

    For example
    I made a concious decision to remember what sort of data each text box was allowed to hold.

    Name for information
    Name for lookup
    Number
    Telephone Number
    EmailAddress

    Next i wrote limited what the enter and change events for each textbox was allowed to do.
    The beauty of this approach is that my event subroutines became very simble, so simple that I would create them in excel and copy them to VBA.


    I ended up with the following code in my userform #1
    Basically I use a Public Variable T to store the textbox number that I click on or change.
    I use another Public Variable to store which textbox I want the user to move to next.

    I then call a standard subroutine that uses T and N and an array that is used to control precisely what happens.


    Please Login or Register  to view this content.

  3. #3
    Forum Contributor vamosj's Avatar
    Join Date
    04-23-2004
    Location
    Oregon
    MS-Off Ver
    2010
    Posts
    294

    Re: Rememberign which command button was pressed in a userform

    I'll try to illustrate it. CB = Command Button

    [CB1] [CB51] [Label 1]
    [CB2] [CB52] [Label 2]
    [CB3] [CB53] [Label 3]


    I press CB2 to move the data from Label 2 to Label 1
    A small macro in that button pulls the number 2 from the name
    it runs another macro that uses the 2 to swap the data from Label 2 to Label 1.

  4. #4
    Forum Contributor vamosj's Avatar
    Join Date
    04-23-2004
    Location
    Oregon
    MS-Off Ver
    2010
    Posts
    294

    Re: Rememberign which command button was pressed in a userform

    Bump, still looking for some assistance.

  5. #5
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Rememberign which command button was pressed in a userform

    I would investige a list box and just use two up and down buttons.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  6. #6
    Forum Contributor vamosj's Avatar
    Join Date
    04-23-2004
    Location
    Oregon
    MS-Off Ver
    2010
    Posts
    294

    Re: Rememberign which command button was pressed in a userform

    That would definitely limit the amount of command buttons I would need to use so thank you on that. I would still like to know if it is possible to pull the number from an object such as a command button, combobox, listbox, etc... using something like the function below or some other small routine that would do the task.

    Right(PostForm.ListBox.Name, Len(PostForm.ListBox.Name) - Len("ListBox"))

  7. #7
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Rememberign which command button was pressed in a userform

    I'm not exactly sure what you're trying to do.

    Please Login or Register  to view this content.
    Would return 12.

  8. #8
    Forum Contributor vamosj's Avatar
    Join Date
    04-23-2004
    Location
    Oregon
    MS-Off Ver
    2010
    Posts
    294

    Re: Rememberign which command button was pressed in a userform

    I have a multitude of these buttons.. I want one macro that I can copy and paste into each of these buttons that will return the number of the button that was pressed. I am not sure how much more clear I can make this.

    This way all I have to do is copy/paste the code into each button without having to go through and say what number buttons was pressed. I know I could just put in the macro B = 1 for commandbutton one, but I wanted something that will pull the number of the button for me instead of having to go through all of them and say B = 1 for button 1, B = 2 for button 2, B = 3 for button 3, B = 4 for button 4, etc.................

    Something like what I put in the original code..

    CB = Right(PostForm.CommandButton.Name, Len(PostForm.CommandButton1.Name) - Len("CommandButton"))

    So if I pressed button number 45, CB = 45. if I pressed button number 89, CB = 89 without having to go into each button and just stating that CB = (Whatever button number)



    Quote Originally Posted by Solus Rankin View Post
    I'm not exactly sure what you're trying to do.

    Please Login or Register  to view this content.
    Would return 12.

  9. #9
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Rememberign which command button was pressed in a userform

    Ok.

    Lets try this.

    I have created a userform with 120 command buttons.

    I have created a macro to display the userform.

    In the Worksheet I have created sample VB code to drive a command box

    I have created another macro to create the code that I want to use to control all the command buttons.

    So open the workbook. and press create code to create the code to drive the 120 command buttons.

    copy this code and paste it into visual basic, under the Userform Activation code.

    close visual basic.

    click on show userform.

    click on any command button.
    Last edited by mehmetcik; 08-09-2014 at 06:51 PM.

  10. #10
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Rememberign which command button was pressed in a userform

    This is the Workbook that I created for you.
    Attached Files Attached Files

  11. #11
    Forum Contributor vamosj's Avatar
    Join Date
    04-23-2004
    Location
    Oregon
    MS-Off Ver
    2010
    Posts
    294

    Re: Rememberign which command button was pressed in a userform

    mehmetcik, never really thought of creating a macro to create my macros. This is ingenious and works beautifully. Thank you.

    From the lack of the main question being answered, I guess there is not a way to pull the actual name of the button being pressed from a generic macro that would cover multiple buttons.


    Thanks for your solution to this, it'll save me a ton of time.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Command Button when pressed will incurease textbox value
    By Gal403 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-08-2014, 01:50 AM
  2. command button when pressed add the number 1, then 2, then 3 and so on
    By kateroe in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-06-2012, 08:28 AM
  3. Making one sheet visible once a Week or when command button is pressed
    By realniceguy5000 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-11-2008, 04:34 PM
  4. How to limit the number of times a command button can be pressed
    By justchris in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-15-2005, 06:23 AM

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