+ Reply to Thread
Results 1 to 9 of 9

Macro to run only once

  1. #1
    Forum Contributor
    Join Date
    12-08-2015
    Location
    London, England
    MS-Off Ver
    2021
    Posts
    145

    Macro to run only once

    Hello,

    I made a small macro that runs off a command button, which the user needs to press only once when they've completed using the sheet.

    The macro edits some columns and removes things that aren't required.

    But if the user accidentally clicks the button more than once it repeats the process, removing information that needs to be kept. Is there something that can be added to the macros code that would only allow it to be pressed once?

    Cheers,

    Weasel.

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Macro to run only once

    Hi,

    You could remove / hide / disable the button at the end of its code. What kind of button is it?
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Valued Forum Contributor meabrams's Avatar
    Join Date
    10-03-2014
    Location
    USA
    MS-Off Ver
    2007, 2010, 2013, 2016
    Posts
    451

    Re: Macro to run only once

    Try....

    Please Login or Register  to view this content.
    Last edited by meabrams; 04-03-2017 at 07:48 AM. Reason: Miss Placement of boolean
    Bramz

  4. #4
    Forum Contributor
    Join Date
    12-08-2015
    Location
    London, England
    MS-Off Ver
    2021
    Posts
    145

    Re: Macro to run only once

    Sorry struggling to apply the above code

    I've added it to my macro like this:

    Private Sub CommandButton1_Click()
    Public Only1 As Boolean
    Sub Run1x()

    If Only1 = False Then
    Only1 = True
    Cells.Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Range("L:L,G:G").Select
    Range("G1").Activate
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Range("F5").Select
    End If

    End Sub

    But it's not working - can you show me where i'm going wrong?

    Thanks again

  5. #5
    Forum Contributor
    Join Date
    12-08-2015
    Location
    London, England
    MS-Off Ver
    2021
    Posts
    145

    Re: Macro to run only once

    Sorry struggling to apply the above code

    I've added it to my macro like this:

    Private Sub CommandButton1_Click()
    Public Only1 As Boolean
    Sub Run1x()

    If Only1 = False Then
    Only1 = True
    Cells.Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Range("L:L,G:G").Select
    Range("G1").Activate
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Range("F5").Select
    End If

    End Sub

    But it's not working - can you show me where i'm going wrong?

    Thanks again

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Macro to run only once

    At the top of the code module add
    Please Login or Register  to view this content.
    then amend your code to
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    12-08-2015
    Location
    London, England
    MS-Off Ver
    2021
    Posts
    145

    Re: Macro to run only once

    I must be doing something wrong

    Copied the above two sets of code over my existing & tried to run it - it says:

    Compile error:

    End If without block if

    Can you show me what i've missed?

    Thanks

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Macro to run only once

    Apologies- remove the End If line. I neglected to do so when I adjusted the initial code.

  9. #9
    Forum Contributor
    Join Date
    12-08-2015
    Location
    London, England
    MS-Off Ver
    2021
    Posts
    145

    Re: Macro to run only once

    That's perfect - thanks for your patience

+ 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] Macro to add another Macro to a button to add into a larger Macro.
    By taylorsm in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-03-2016, 10:51 AM
  2. [SOLVED] Define variable in macro than calling that macro inside another macro
    By jakopak in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-21-2015, 10:58 AM
  3. Using a macro on workbook1 to create a button in wb2 and assigning macro "wb2!macro"
    By penfold1992 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-13-2014, 11:39 AM
  4. [SOLVED] Macro to show Which macro didnt work in a nested macro
    By akhileshgs in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-10-2013, 03:21 AM
  5. Perform macro "on open" specific file- store macro in Personal Macro Workbook?
    By thompssc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-17-2012, 12:38 PM
  6. lookup macro, solver macro, realtime macro
    By xelhelp in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-02-2011, 06:14 PM
  7. Cannot find macro error when running a macro from a macro in a diffrent workbook.
    By Acrobatic82 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-05-2010, 09:22 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