+ Reply to Thread
Results 1 to 9 of 9

Verify before proceeding

  1. #1
    Registered User
    Join Date
    05-18-2006
    Posts
    26

    Verify before proceeding

    Hello,

    Is there a way that I can check to see if a macro was run from within that macro before it proceeds with a part of the macro, maybe have it check something before proceeding with the rest of it ?
    What I want it to do is stop someone from run a macro again if it was already ran, and if it wasn't ran before to check some column headings to make sure they are all there, before proceeding with the macro.

    Thanks

  2. #2
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    There are various ways of doing this but I think the easiest way is to use a cell on a worksheet (maybe a hidden sheet) to store an update/updated flag.

    Then use the workbook.open event to set the flag to "update".

    Before running the checking macro see if the is "update"
    Then when you run the macro which does the update reset the flag to "updated".

    Note that if you try and hold the flag as a variable it will get reset each time the code finishes.

    regards

  3. #3
    Ardus Petus
    Guest

    Re: Verify before proceeding

    Dim bMacro1WasRun as boolean

    sub MySub()
    if bMacro1WasRun then
    <do something>
    else
    <do whatever>
    end if
    end sub

    sub Macro1
    <do some stuff>
    bMacro1WasRun = true
    end sub

    Et voilà

    --
    AP

    "snowing" <[email protected]> a écrit
    dans le message de news:
    [email protected]...
    >
    > Hello,
    >
    > Is there a way that I can check to see if a macro was run from within
    > that macro before it proceeds with a part of the macro, maybe have it
    > check something before proceeding with the rest of it ?
    > What I want it to do is stop someone from run a macro again if it was
    > already ran, and if it wasn't ran before to check some column headings
    > to make sure they are all there, before proceeding with the macro.
    >
    > Thanks
    >
    >
    > --
    > snowing
    > ------------------------------------------------------------------------
    > snowing's Profile:
    > http://www.excelforum.com/member.php...o&userid=34576
    > View this thread: http://www.excelforum.com/showthread...hreadid=547810
    >




  4. #4
    Registered User
    Join Date
    05-18-2006
    Posts
    26
    Thanks for the help.

    Ardus can you explain your code a little for me, I would need something to work from within the one macro

  5. #5
    Ardus Petus
    Guest

    Re: Verify before proceeding

    Tony's solution is smarter because it saves the "MacroWasRun" status

    You place a FALSE/TRUE value in some definite cell, then you test its
    contents.

    HTH
    --
    AP


    "snowing" <[email protected]> a écrit
    dans le message de news:
    [email protected]...
    >
    > Hello,
    >
    > Is there a way that I can check to see if a macro was run from within
    > that macro before it proceeds with a part of the macro, maybe have it
    > check something before proceeding with the rest of it ?
    > What I want it to do is stop someone from run a macro again if it was
    > already ran, and if it wasn't ran before to check some column headings
    > to make sure they are all there, before proceeding with the macro.
    >
    > Thanks
    >
    >
    > --
    > snowing
    > ------------------------------------------------------------------------
    > snowing's Profile:
    > http://www.excelforum.com/member.php...o&userid=34576
    > View this thread: http://www.excelforum.com/showthread...hreadid=547810
    >




  6. #6
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    Here is some code that does not use a cell for storage but one of the document properties - if necessary you can create a custom property. The advantage of using one of these properties is that it does not inflict anything on the sheets themselves.

    Private Sub Workbook_Open()
    ThisWorkbook.BuiltinDocumentProperties("Comments") = "N"
    End Sub



    Sub mySub()
    If ThisWorkbook.BuiltinDocumentProperties("Comments") = "N" Then
    do my run once code
    then set
    ThisWorkbook.BuiltinDocumentProperties("Comments") = ""
    End If

    do my always code

    End Sub

    Cheers (and thanks Ardus)

  7. #7
    Registered User
    Join Date
    05-18-2006
    Posts
    26
    Thanks guy for your help.

    Tony, your code seem to skip the "run once code" and go right to "do my always code"

    Am I doing something wrong?

    *****************************************
    Private Sub Workbook_Open()
    ThisWorkbook.BuiltinDocumentProperties("Comments") = "N"
    End Sub



    Sub mySub()
    If ThisWorkbook.BuiltinDocumentProperties("Comments") = "N" Then
    do my run once code
    then set
    ThisWorkbook.BuiltinDocumentProperties("Comments") = ""
    End If

    do my always code

    End Sub

  8. #8
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    The workbook_open module needs to be ThisWorkbook module (in VBA explorer VBAProject...Microsoft Excel Objects... ThisWorkbook)

    The rest of the code in a standard module. Put some msgbox call in to track what happens eg

    Private Sub Workbook_Open()
    ThisWorkbook.BuiltinDocumentProperties("Comments") = "N"
    MsgBox "initialise"
    End Sub



    Sub mySub()
    If ThisWorkbook.BuiltinDocumentProperties("Comments") = "N" Then
    MsgBox "run once"
    ThisWorkbook.BuiltinDocumentProperties("Comments") = ""
    End If

    MsgBox "do always"

    End Sub

    you should get "initialise" when you open the workbook. The other two should appear as you run mySub

  9. #9
    Registered User
    Join Date
    05-18-2006
    Posts
    26
    Thanks Tony, works great.

+ 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