Solved

excel vba macro, run only once

Posted on 2011-03-04
5
878 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 41

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 41

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now