Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

how prevent run macro for more than once and close file ?

0

hi experts  

I'm asking  if  there  is  way   prevent   run  the  macro   more  than  one  time  when  I  try  run  again  . it   should  pop message " you  can't   run  the  macro   more  than once" , "sorry ! you  should  open  the  file  again"   and   close  the  file with  save  the  changes    directlt  without  show  message   save .   

I  hope  my  question  is  acceptible 

Answer
Discuss

Answers

0
Selected Answer

Tubrak

Try this at whatever level your code is (changing the macro name to suit and adding whatever code you want to run once).

Option Explicit
Dim FirstRun As Boolean

Sub RunOnce()

Dim Warn As Integer

If FirstRun = False Then
    ' Your macro code
    FirstRun = True
    Else

    Warn = MsgBox("You can't run this macro more than once, sorry! Please open the file again (now saving file)", vbOKOnly)
    ThisWorkbook.Close savechanges:=True
    Exit Sub

End If

End Sub

Hope this is what you want. If however you wish to close Excel too, I suggest you do that if your workbook is the only one open (i.e. other workbooks aren't affected). The additions in bold below do that (and note that the Application.Quit command comes BEFORE the .Close):

Option Explicit
Dim FirstRun As Boolean

Sub RunOnce()

Dim Warn As Integer

If FirstRun = False Then
    ' Your macro code

    FirstRun = True
    Else

    Warn = MsgBox("You can't run this macro more than once, sorry! Please open the file again (now saving file/closing Excel)", vbOKOnly)
    If Workbooks.Count = 1 Then Application.Quit 'do this BEFORE closing
    ThisWorkbook.Close savechanges:=True

    Exit Sub

End If

End Sub
Discuss

Discussion

John 
thanks  for  your  solution , but  it's still  someting  I  would  fix  it  .  when  close  the  file  it  should  close  the  whole  application  but   the  EXCEL is still   open  . I  try  use  this 
Application.Quit
  but  not  success .   any  suggestion ?
tubrak (rep: 24) Jun 28, '21 at 5:36 am
Tubrak. I've changed my Answer to do that (but not close Excel is there's another workbook open at the same time). Hope this means you can Select my Answer as being a correct solution.

Next time, please try to put all your requirements in the original question (i.e. avoid expanding the question in the Discussion section)
John_Ru (rep: 6142) Jun 28, '21 at 8:17 am
I  don't thought  when  I  said  close  the  file      there  is  differnce  between  close  the  file   and  whole  application  . my  apologies ! anyway   this  is  exactly   what   I  want .   thanks  so  much  .
tubrak (rep: 24) Jun 28, '21 at 8:33 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login