My code line close Excel App

drom

Well-known Member
Joined
Mar 20, 2005
Messages
528
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Hi and thanks in advance!

I am having problems with the following code and I do not anderstand why
'wFxN is a file (eg: Pl_Proyect_V06.xlsm) I am opening as read only and I would like to close, and them activate ThisWorkbook

VBA Code:
Application.Wait Now + TimeValue("00:00:03")
ThisWorkbook.Activate   'Some times I am not able to activate ThisWorkbook unless I go step by step using F8
ThisWorkbook.Activate   'Does no t matter if I put 2 times, some times  ThisWorkbook is not the active book
Application.Wait Now + TimeValue("00:00:01")
Application.DisplayAlerts = False
If wFxN <> "" Then
Workbooks(wFxN).Close SaveChanges:=False             
  'HERE this line closes EXCEL app and reopens it
  'If I use F8 excel goes down, quits every book opened and the app is close
  'and a few seconds after Excel opens again a normal book, (book1)
End If



What am I missing ?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I'm assuming wFxN is a string variable, but perhaps something like this.
VBA Code:
    Dim WB As Workbook
   
    DoEvents
    ThisWorkbook.Activate   'Some times I am not able to activate ThisWorkbook unless I go step by step using F8
    DoEvents
   
    If ActiveWorkbook.Name <> ThisWorkbook.Name Then
        MsgBox "Error - cannot activate workbook '" & ThisWorkbook.Name & "'", vbCritical
        Exit Sub
    End If
   
    On Error Resume Next
    Set WB = Application.Workbooks(wFxN)
    On Error GoTo 0
   
   
    Application.DisplayAlerts = False
    If Not WB Is Nothing Then
        WB.Close SaveChanges:=False
    Else
        MsgBox "Error -  Workbook '" & wFxN & "' does not appear to be open", vbOKOnly Or vbCritical, Application.Name
        Exit Sub
    End If
 
Upvote 0
I'm assuming wFxN is a string variable, but perhaps something like this.
VBA Code:
    Dim WB As Workbook
  
    DoEvents
    ThisWorkbook.Activate   'Some times I am not able to activate ThisWorkbook unless I go step by step using F8
    DoEvents
  
    If ActiveWorkbook.Name <> ThisWorkbook.Name Then
        MsgBox "Error - cannot activate workbook '" & ThisWorkbook.Name & "'", vbCritical
        Exit Sub
    End If
  
    On Error Resume Next
    Set WB = Application.Workbooks(wFxN)
    On Error GoTo 0
  
  
    Application.DisplayAlerts = False
    If Not WB Is Nothing Then
        WB.Close SaveChanges:=False
    Else
        MsgBox "Error -  Workbook '" & wFxN & "' does not appear to be open", vbOKOnly Or vbCritical, Application.Name
        Exit Sub
    End If

I am having the same problems (I am confused)
Anyway Thanks
 
Upvote 0
I am having the same problems

Did you try the code I posted? I ask because if you did have the same problems, you should get an error message.

1705514923754.png


Could you be more specific about what is not working and how it is not working?
 
Upvote 0
Did you try the code I posted? I ask because if you did have the same problems, you should get an error message.

View attachment 105253

Could you be more specific about what is not working and how it is not working?
The macro goes with any message box, (goes OK until):
WB.Close SaveChanges:=False
but here excel turned off and opens again

This is why I am so confused
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,987
Members
449,093
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top