+ Reply to Thread
Results 1 to 14 of 14

Put a 'Click' event in a Macro

  1. #1
    Registered User
    Join Date
    12-04-2005
    Posts
    19

    Put a 'Click' event in a Macro

    Can a ‘Click’ event be included within code to trigger a macro on another worksheet (WS)?
    I have a workbook which monitors share prices, and through a macro changes the value in columns H & I on worksheet (WS) 1 and cols E & F on WS2 should the value in cols G & D respectively be higher or lower than the values already there. (The macros on both sheets are similar but use different columns).
    On WS1 the macro is activated by ‘clicking’ on a Command Button, (the GREEN one on the attached file) which does other things before changing any values. No problem here.
    The values are imported from the web into WS3 and 4 (Import Share Price and Import Tracking) which are linked to cols G and D on WS1 and 2 respectively.
    On WS2 I have to physically go to that sheet and ‘Click’ on any cell to activate the macro, as the macro on both sheets starts as:
    Please Login or Register  to view this content.
    What I want to do is reduce the two actions into one so the macro on WS2 automatically runs after the actions on WS1 once the Command Button is pressed.
    I assume that the two macros cannot be combined, as WS2 would not be the active worksheet, so can WS2 be selected and the macro run with a ‘Click’ event on that sheet, or is there another way around this.
    I attach a sanitized file with all the code on both WS’s.
    To test any solution, enter a value greater and lesser than those shown in cells G51 and G52 on WS4 (Import Tracking). These values will then be shown in cells D53 and 54 on WS2, and should increase and decrease the values in cells E53 and F54 after the macro is run.
    Attached Files Attached Files
    Last edited by tvac; 06-29-2010 at 11:03 AM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Can you put a 'Click' event in a Macro

    If you move the code in the selection event into a routine in a standard code module then you can call it from the selection event and anywhere else you want to.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    12-04-2005
    Posts
    19

    Re: Can you put a 'Click' event in a Macro

    Quote Originally Posted by Andy Pope View Post
    If you move the code in the selection event into a routine in a standard code module then you can call it from the selection event and anywhere else you want to.
    Hi Andy
    Thanks for taking the time to reply, but you have just gone straight over my head. I inherited this workbook, and although I know my way around Excel (more or less), VBA is a mystery! I've only worked out what I know from logically looking at the code.
    Any further assistance would be appreciated.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Can you put a 'Click' event in a Macro

    Hello tvac,

    Andy is offline right now. I have looked at the workbook and agree with Andy's assessment. The attached workbook now updates the "Tracking" sheet when the "To see daily changes" button is clicked. The tracking sheet no longer updates when you select a different cell.

    I moved the code from the Worksheet_SelectionChange event into a separate VBA module named "Update_High_Low_Tracking_Values". Now this can be called from any event, code, or module in the VBA project to update the "Tracking" worksheet.
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Can you put a 'Click' event in a Macro

    Thanks Leith

  6. #6
    Registered User
    Join Date
    12-04-2005
    Posts
    19

    Re: Can you put a 'Click' event in a Macro

    Quote Originally Posted by Leith Ross View Post
    Hello tvac,

    Andy is offline right now. I have looked at the workbook and agree with Andy's assessment. The attached workbook now updates the "Tracking" sheet when the "To see daily changes" button is clicked. The tracking sheet no longer updates when you select a different cell.

    I moved the code from the Worksheet_SelectionChange event into a separate VBA module named "Update_High_Low_Tracking_Values". Now this can be called from any event, code, or module in the VBA project to update the "Tracking" worksheet.
    Hello Leith and Andy
    Thanks for that. I think I understand the solution, however, it still does not work. On pressing the Command Button I get a "Sub or Function Not Defined" error message.
    I note that the module is called:
    Update_High_Low_Tracking_Values
    but the line of code is:
    [CODECall UpdateHighLowValues]Please Login or Register to view this content.[/CODE]
    I have tried inserting 'Tracking' but still no joy.
    Regards - John

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Can you put a 'Click' event in a Macro

    Just a typo.

    Please Login or Register  to view this content.

  8. #8
    Valued Forum Contributor
    Join Date
    07-21-2008
    Location
    London, UK
    Posts
    326

    Re: Can you put a 'Click' event in a Macro

    Look in the prodecure you want and call it ie
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    12-04-2005
    Posts
    19

    Re: Can you put a 'Click' event in a Macro

    Hello Andy
    I changed the code as you suggest, and now no error messages. However, only the macros on the 'Stop Loss' sheet (WS1) run, including updating of higher/lower prices. It just stops there, and the 'Call' to activate the 'Tracking' (WS2) does not happen.
    Jack. Thanks for your contribution, but I think we are nearly there with Andy's solution.
    Andy, sorry to be such a pain, but could you look again please and maybe amend the file posted by Leith.

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Can you put a 'Click' event in a Macro

    Because the routine has been moved from the worksheet object we need to include a reference to the worksheet.

    Please Login or Register  to view this content.
    WS2 needs the event code changed slightly
    Please Login or Register  to view this content.
    The button on WS1 also need to call the routine
    Please Login or Register  to view this content.
    Even with those changes in place I'm not sure what will happen as the WS2 has nothing in A1 so the While/Wend loop will not execute.

  11. #11
    Registered User
    Join Date
    12-04-2005
    Posts
    19

    Re: Put a 'Click' event in a Macro

    [QUOTE][Even with those changes in place I'm not sure what will happen as the WS2 has nothing in A1 so the While/Wend loop will not execute. /QUOTE]
    Hi Andy
    Me again! Made the changes but everthing exactly as before, everything runs ok up to the point of calling WS2 - nothing. I even un-merged cells in Row 1 and entered a value in A1, still no good. What do we do next?

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Put a 'Click' event in a Macro

    Do you have data in the appropriate cells of WS2?
    Can you post the workbook you currently have that does not work.

  13. #13
    Registered User
    Join Date
    12-04-2005
    Posts
    19

    Re: Put a 'Click' event in a Macro

    Hi Andy
    All sorted. Data re-entered into Col A on WS2, I forgot that you need something there, but wiped it all out when I sanitized the workbook.
    It works perfectly and I have made all changes into the large master workbook and that works fine also.
    Thanks to you and Leith, you must be very patient people. If you are ever in the Preston area - I owe you a pint!
    Kind regards - John

  14. #14
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Put a 'Click' event in a Macro

    Thanks for the update. Glad you are sorted.

+ 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