Solved

excel vba macro, run only once

Posted on 2011-03-04
5
983 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
  • 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help with Clear Macro 4 26
Cost allcocation ... 10 23
Excel formula to report date modified 14 29
excel table with over 25000 rows. 3 32
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

828 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