Closing ActiveWorkbook but not ThisWorkbook

MistakesWereMade

Board Regular
Joined
May 22, 2019
Messages
103
My program opens a spreadsheet. I want to close this one using the x symbol without asking me if I'd like to close all other spreadsheets.

I have this written in my ThisWorkbook coding section.

Code:
Public swb As String


Private Sub Workbook_Open()


    Application.ScreenUpdating = False
    swb = ThisWorkbook.Name
    ThisWorkbook.Application.Visible = False
    Application.ScreenUpdating = True
    
    SplashUserForm.Show


End Sub


Private Sub WbkClose()
 
If swb <> ActiveWorkbook.Name Then
    ActiveWorkbook.Close = True
    ThisWorkbook.Close = False
End If


End Sub

I want the workbook associated with swb to remain open and not asked to be canceled. I have a separate button in my userform that is used to close this workbook.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Thanks for the quick response!!! Not sure if it will be helpful or not but here it is.

Code:
Private Sub CommandButton1_Click()


    Dim Wbk As Workbook
    Dim Pth As String
    Dim myRange As Range
    
    Set myRange = ThisWorkbook.Worksheets("Data").Range("E2")
    
    Pth = Environ("Userprofile") & "\Desktop\My Files\"


    OpeningVar = Me.ComboBox1.Value


    Me.ComboBox1.Clear


    On Error Resume Next
    
    Set Wbk = Workbooks.Open(Pth & OpeningVar)
   
    myRange.Clear
   
    On Error GoTo 0
   
    If Wbk Is Nothing Then
        MsgBox "Workbook not found."
    End If
    
    myRange.Clear
    
End Sub
 
Upvote 0
Ok, remove this line from that sub & place it at the very top of the module, before any code
Code:
Dim Wbk As Workbook
and then use
Code:
Private Sub WbkClose()
      Wbk.Close True
End Sub
 
Last edited:
Upvote 0
Thanks for the suggestion!

It seems the problem still persists. When I close the active sheet, it always asks me to close my hidden sheet too. I want to be able to close any other sheet that I open without forcing me to close out of "ThisWorkbook" (aka my hidden sheet).

I've been stuck on this for a long time. I appreciate all the help. Any other suggestions?
 
Upvote 0
Are you talking about sheets or workbooks?
They are entirely different things, you cannot "close" a sheet without closing the entire workbook that it's in.
 
Upvote 0
Sorry, Yeah I'm still learning the proper terms for these things as they seem interchangeable at times from a beginner's standpoint.

I want to close out of the new workbook that I open with my program. I do not want to close the workbook that runs my program. My program's worksheets are hidden, instead having a userform that is available to interact with.

Hopefully my problem is a bit more clear with proper terminology!

Thanks for helping me clarify!
 
Upvote 0
That code should not be closing ThisWorkbook, so I suspect that you have some other code that is affecting things.
 
Upvote 0
Well, I came across the code below that seems to work for me. However, it only works once, as in I will close out of the active workbook (it won't save which is what I want) and then I can use my userform1 freely again. After opening another workbook using my userform1 though, I can't close out of the new active workbook without it asking if I want to save or not, and then asking me if I want to save the workbook that runs my userform1. Do you know how I can adjust this code so that it works every time rather than just once?

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)


    If swb <> ActiveWorkbook.Name Then
        ActiveWorkbook.Close savechanges:=False
        Application.Visible = False
        UserForm1.Show vbModeless
    End If


End Sub
 
Last edited:
Upvote 0
No, because I don't understand why you are getting those problems, unless there is some other code that is causing the problems.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

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