Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1241
  • Last Modified:

excel vba macro, run only once

I have a server delivering an Excel file to the browser.  

I have included some (novice-level but adequate) macro code to set appropriate attributes on each sheet of the file, to control online display and printing.  I want the macro to run when the file is delivered to the browser, but not to run again if the user saves the document and later re-opens it.

(It's a WebFocus application; the macro comes from a template .mht document stored on the server.)

What mechanicsm can be used?  

If there is a way for the auto_open macro to delete itseft, that would do it.  Otherwise, it will have to sense whether to proceed or quite -- I would want to have the first-time-only detection encapsulated in a function, so I can readily re-use it.

0
jbzg
Asked:
jbzg
  • 2
2 Solutions
 
nutschCommented:
I assume these changes the macro performs leave some marks, formats, values, etc that could be tested for when you first the macro, with a structure of

if macro has been run then
exit sub
else
run macro
end if

Do you have a sample file or sample code?

Thomas
0
 
dlmilleCommented:
Just create a hidden protected sheet with a cell having true or false.  Or create a Name with a value initially set to false

Then on auto open check that variable if false do your run once routine and set that variable to true

Next time auto open happens after save the flag will be true so no need to run the macro

Make sense?

Dave
0
 
dlmilleCommented:
You can find this:

Private Sub Workbook_Open()

    If Not [AlreadyRanOnce] Then
        'run your code
        MsgBox "Running your code", vbOK
        ActiveWorkbook.Names("AlreadyRanOnce").Value = True
    Else
        'do nothing, or continue Open processing
        MsgBox "Already Ran - no need to run", vbOK
    End If
End Sub

Open in new window


In the attached file.  I created the NAME called "AlreadyRanOnce" and set it to FALSE, which is your initial state.

Give it a shot!

Dave
RunOnlyOnceEver-r1.xls
0
 
TracyVBA DeveloperCommented:
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now