Solved

excel vba macro, run only once

Posted on 2011-03-04
5
1,031 Views
Last Modified: 2012-06-22
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
Comment
Question by:jbzg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 39

Assisted Solution

by:nutsch
nutsch earned 250 total points
ID: 35039388
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
 
LVL 42

Expert Comment

by:dlmille
ID: 35040069
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
 
LVL 42

Accepted Solution

by:
dlmille earned 250 total points
ID: 35040185
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
 
LVL 24

Expert Comment

by:broomee9
ID: 35356997
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question