+ Reply to Thread
Results 1 to 9 of 9

Set Active Cell on Command Button Click?

  1. #1
    Registered User
    Join Date
    11-06-2012
    Location
    northern california
    MS-Off Ver
    Excel 2002
    Posts
    10

    Set Active Cell on Command Button Click?

    I have a macro assigned to a command button which is supposed to copy the contents of the cell directly above the cell with the command button in it, and paste the results to sheet1, cell A1. But it's not working the way I expect it to, because I think the cell with the button in it needs to be the active cell. I thought that by clicking on the button, it's cell would have focus, but that's not the case. Can anyone help? This is what I have so far:

    Please Login or Register  to view this content.

  2. #2
    Registered User
    Join Date
    07-27-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007/2010
    Posts
    86

    Re: Set Active Cell on Command Button Click?

    Edit: Not answering your question
    Boon

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,083

    Re: Set Active Cell on Command Button Click?

    Presumably, you know which cell the button is in? In which case, you can refer to it directly rather than assuming it is the Active Cell.

    So, if the button is in cell G7, you could say:

    Please Login or Register  to view this content.

    Regards, TMS
    Last edited by TMS; 11-06-2012 at 05:24 PM. Reason: Poor cell reference choice
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Registered User
    Join Date
    07-27-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007/2010
    Posts
    86

    Re: Set Active Cell on Command Button Click?

    I think cadnauseum is referring to putting multiple command buttons in a worksheet, and assigned each command buttons with the same macro. I couldn't think of any way to solve that by using command button, except for creating multiple macros.

  5. #5
    Registered User
    Join Date
    11-06-2012
    Location
    northern california
    MS-Off Ver
    Excel 2002
    Posts
    10

    Re: Set Active Cell on Command Button Click?

    Quote Originally Posted by bheanloh View Post
    I think cadnauseum is referring to putting multiple command buttons in a worksheet, and assigned each command buttons with the same macro. I couldn't think of any way to solve that by using command button, except for creating multiple macros.
    Exactly! I could hardcode the values into the macros, but that would mean hundreds of different macros. I thought it would be more efficient to have a single macro perform the task. It doesn't have to be a command button, can you think of any other method?

  6. #6
    Forum Contributor
    Join Date
    07-05-2012
    Location
    Houston, Texas
    MS-Off Ver
    Excel 2016
    Posts
    165

    Re: Set Active Cell on Command Button Click?

    If bheanloh is correct, you could have a global variable and in the click_event for each button, set the value of the variable to a value specific to that button, then call the original sub. In the sub, use a select case statement to choose which button was clicked and which cell will be the active cell.
    Click on the * icon if this post has been helpful.

  7. #7
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,883

    Re: Set Active Cell on Command Button Click?

    Please Login or Register  to view this content.
    <----- If you were helped by my posts you can say "Thank you" by clicking the star symbol down to the left

    If the problem is solved, finish of the thread by clicking SOLVED under Thread Tools
    I don't wish to leave you with no answer, yet I sometimes miss posts. If you feel I forgot you, remind me with a PM or just bump the thread.

  8. #8
    Registered User
    Join Date
    11-06-2012
    Location
    northern california
    MS-Off Ver
    Excel 2002
    Posts
    10

    Re: Set Active Cell on Command Button Click?

    Quote Originally Posted by Xer2 View Post
    If bheanloh is correct, you could have a global variable and in the click_event for each button, set the value of the variable to a value specific to that button, then call the original sub. In the sub, use a select case statement to choose which button was clicked and which cell will be the active cell.
    Forgive my newbie ignorance, but if I make the text label for each command button unique (equal to the string I'm trying to paste in A1), the common macro could capture this and write it to A1 cell, correct?

    How do I go about capturing the text of the command button label?

  9. #9
    Forum Contributor
    Join Date
    07-05-2012
    Location
    Houston, Texas
    MS-Off Ver
    Excel 2016
    Posts
    165

    Re: Set Active Cell on Command Button Click?

    As I understand your problem, the below should work. If not, please let us know what else you may need.


    You could use something like this in the Sheet code:
    Please Login or Register  to view this content.
    and something like this in the module as the main sub:
    Please Login or Register  to view this content.

    See the attached photo:
    Possible Example.JPG

+ 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