+ Reply to Thread
Results 1 to 4 of 4

Close a specific workbook with VBA

  1. #1
    Registered User
    Join Date
    07-28-2020
    Location
    Ecuador
    MS-Off Ver
    2016
    Posts
    12

    Close a specific workbook with VBA

    Hi Guys, I hope you can help me this time

    I have a login window create with a vba form and when the user close this workbook clicking on "X" button or cancel, the macro used makes all workbooks open, turn close automatically. The goal is that the macro only close the workbook with the vba form. The code is the following:

    Code for "Login" button
    Private Sub CommandButton1_Click()
    If Trim(txtUser.Text) = "" Then
    MsgBox "Enter user", vbExclamation, "Warning"
    txtUser.SetFocus
    Exit Sub
    End If
    If Trim(txtPass.Text) = "" Then
    MsgBox "Enter password", vbExclamation, "Warning"
    txtPass.SetFocus
    Exit Sub
    End If

    If Trim(txtUser.Text) = "USER" And Trim(txtPass.Text) = "PASSWORD" Then
    MsgBox "Access allowed", vbInformation, "Input Data"
    Application.Visible = True
    ThisWorkbook.salida = 2
    'Unload Me
    'MsgBox "The data entered is correct"
    End
    Else
    MsgBox "The data entered is incorrect, try again", vbExclamation, "Warning"
    End If
    End Sub

    Code for "Cancel" button
    Private Sub CommandButton2_Click()
    ThisWorkbook.salida = 1
    Unload Me
    'MsgBox "CANCEL key pressed, exit without recording"
    ThisWorkbook.Close savechanges:=False
    End Sub

    Code for "X" button
    Private Sub UserForm_Terminate()
    If ThisWorkbook.salida = 0 Then
    ThisWorkbook.salida = 1
    Unload Me
    'MsgBox "X key pressed, exit without recording"
    ThisWorkbook.Close savechanges:=False
    End If

    LOGIN.png

    NOTE: This macro is composed to other macros in the private mood to obligate to the user to "enable the use of the macros". All of this makes the first time the user after enter the correct data and access to the workspacet, only see a "Welcome Page" and then when the user enable the macros see all the sheets hide.

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,767

    Re: Close a specific workbook with VBA

    Replace this line

    Please Login or Register  to view this content.
    With

    Please Login or Register  to view this content.


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Registered User
    Join Date
    07-28-2020
    Location
    Ecuador
    MS-Off Ver
    2016
    Posts
    12

    Re: Close a specific workbook with VBA

    Thanks for your answer, but this not work
    The form keep closing the other workbooks too, not only the workbook that I want.

  4. #4
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,122

    Re: Close a specific workbook with VBA

    Please Login or Register  to view this content.

    Also...Please take a moment to read our forum rules...here
    Your post does not comply with Rule # 2
    2. Programming code must be enclosed in code tags to improve readability. (A, Z)
    Please Login or Register  to view this content.
    So...Edit your post...Highlight the code and press the # button
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] How can I completely close a specific workbook without closing other open workbooks
    By Sc0ut in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-07-2016, 04:06 PM
  2. [SOLVED] Close Workbook that has specific prefix
    By hzarry in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-11-2015, 07:53 AM
  3. Macro to close and re-open a workbook at specific time
    By hasanqz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-27-2015, 03:38 PM
  4. [SOLVED] Run macro then close a specific workbook
    By Justair07 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-28-2013, 09:47 AM
  5. [SOLVED] Edit code to close all workbooks except active workbook and other specific workbook
    By rocksan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-04-2012, 09:29 PM
  6. Close workbook at specific time
    By rhudgins in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-11-2011, 03:45 PM
  7. How to close specific workbook not all active workbooks?
    By Bon in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2006, 10:55 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1