+ Reply to Thread
Results 1 to 17 of 17

adding procedure after click button from the second time

  1. #1
    Registered User
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    94

    adding procedure after click button from the second time

    Hi Experts,
    I have code ,running the code from the first time but if I click the button again then should call
    sub macro1() before running my original code is sub test().
    every time save and close the file.
    the first time when run my original code will be when open the file .
    I hope this clear guys.

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: adding procedure after click button from the second time

    Create global (public) variable to hold some value when it runs first time.
    https://learn.microsoft.com/en-us/of...ring-variables

    Then variable = value. Call the other subroutine.

    Or alternately, hold some value in a cell. Then reset it upon save & close.
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  3. #3
    Registered User
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    94

    Re: adding procedure after click button from the second time

    I will check the link.

  4. #4
    Registered User
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    94

    Re: adding procedure after click button from the second time

    I see the link show me declare variable!
    what is relevant with my demand?

  5. #5
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: adding procedure after click button from the second time

    You declare some public variable. Let's say Boolean and set it to false initially.
    Add process at end of 1st sub to set it to true.

    Then you'd write another sub that calls both 1st and 2nd, but check if this variable is true. If so run the 2nd sub, if false, run the 1st sub.

    This is used as check to see if 1st sub was executed.

  6. #6
    Registered User
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    94

    Re: adding procedure after click button from the second time

    this will just run for sub test macro !
    Please Login or Register  to view this content.
    what's the correct?

  7. #7
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,030

    Re: adding procedure after click button from the second time

    So...On first opening file...When pressing [button]
    Call Test
    On pressing [button] again Call Macro
    And then when closing file and opening again what must run then....Test OR Macro
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  8. #8
    Registered User
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    94

    Re: adding procedure after click button from the second time

    And then when closing file and opening again what must run then....Test OR Macro
    should be as the the file is open , as you mentioned
    So...On first opening file...When pressing [button]
    Call Test
    On pressing [button] again Call Macro

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: adding procedure after click button from the second time

    Create a Static variable in test procedure, like
    Please Login or Register  to view this content.

  10. #10
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,030

    Re: adding procedure after click button from the second time

    every time save and close the file.
    the first time when run my original code will be when open the file .
    And then when closing file and opening again what must run then....Test OR Macro
    Last edited by Sintek; 06-23-2024 at 09:58 AM.

  11. #11
    Registered User
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    94

    Re: adding procedure after click button from the second time

    thanks jindon .
    your code will run the first code after finishing will run the second code , this is not what I want , sorry.
    On first opening file...When pressing [button]
    will running first code ,On pressing [button] again will running first code (every time pressing [button] after pressing from the first time will just run the second code.
    when save & close file , opening file again ...When pressing [button] the process start again (just run first code from first pressing [button],when repeat pressing [button] just running second code.
    so first code will run from first time pressing [button] an run the second code from the second time when file is opening.
    Last edited by Omar M; 06-23-2024 at 10:13 AM.

  12. #12
    Registered User
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    94

    Re: adding procedure after click button from the second time

    And then when closing file and opening again what must run then....Test OR Macro
    may you see post 11 ?
    I hope hlep you.

  13. #13
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,030

    Re: adding procedure after click button from the second time

    You are contradicting yourself...
    On first opening file...When pressing [button]
    will running second code
    On pressing [button] again will running first code (every time pressing [button] after pressing from the first time will just run the second code.
    Then you say...
    when save & close file , opening file again ...When pressing [button] the process start again (just run first code from first pressing [button]
    when repeat pressing [button] just running second code.
    So what is it...???

  14. #14
    Registered User
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    94

    Re: adding procedure after click button from the second time

    sorry about error , sintek .
    indeed I find out and I was editing before you comment
    editing post 11

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: adding procedure after click button from the second time

    Then like this?
    Please Login or Register  to view this content.
    ?

  16. #16
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,030

    Re: adding procedure after click button from the second time

    So first tiime always Macro1 thereafter always Macro2....
    If file closes and opens then repeat above...

    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    06-07-2023
    Location
    Libya
    MS-Off Ver
    office 2019
    Posts
    94

    Re: adding procedure after click button from the second time

    both two codes works greatly .
    thank you so much,guys.

+ 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. click button on webpage with "data-procedure" info
    By windzz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-24-2022, 02:54 PM
  2. [SOLVED] Click button to Sort by Alpha, then click button to return Original order
    By ChrisXcel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-29-2020, 10:15 PM
  3. click button to display time
    By chriswrcg in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 10-11-2016, 01:31 PM
  4. [SOLVED] Click button and freeze time textbox
    By chin67326 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-01-2016, 01:32 PM
  5. Button Out of Excel That Logs Time & Day Every Click
    By imsteve123 in forum Excel General
    Replies: 2
    Last Post: 07-06-2015, 06:04 PM
  6. Change textbox text with button click procedure
    By akotronis in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-22-2014, 09:26 AM
  7. Capture System Time On button click
    By sap_iias in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-02-2014, 10:45 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