+ Reply to Thread
Results 1 to 9 of 9

Trigger code to open 2nd workbook when 1st workbook is attempted to be closed

  1. #1
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Trigger code to open 2nd workbook when 1st workbook is attempted to be closed

    Hi,
    The code below gets triggered before closing workbook but gets bugged.
    The code is supposed to open another workbook before closing and clear a cell and then close again, this is granted that other workbook is editable.
    Here's the code, I made the fonred where the code gets bugged.
    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    12-22-2015
    Location
    HK
    MS-Off Ver
    2010
    Posts
    532

    Re: Trigger code to open 2nd workbook when 1st workbook is attempted to be closed

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    'Updates database when main wkbk is closed
    Application.EnableEvents = False
    Application.ScreenUpdating = False

    Dim rcell As Long
    Dim LRV As Long

    Dim Path As String
    Path = "\\user\DataBase.xlsx"
    Dim DataBaseWb As Workbook: Set DataBaseWb = Workbooks.Open(Path)
    Dim wsV As Worksheet: Set wsV = Sheets("OffersV") here in red, you should check that workbook has this sheets or not.
    If Not DataBaseWb.ReadOnly Then

    LRV = wsV.Range("AI" & Rows.Count).End(xlUp).Row
    For rcell = 2 To LRV
    If wsV.Range("AI" & rcell) = ws.Range("G9") Then 'why ws?
    wsV.Range("BN" & rcell) = ""
    Exit For
    End If
    Next rcell

    DataBaseWb.Close True

    Else

    MsgBox "Data base currently in use, retry closing."

    Cancel = True
    DataBaseWb.Close False

    End If

    Application.EnableEvents = True
    Application.ScreenUpdating = True

    End Sub

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Trigger code to open 2nd workbook when 1st workbook is attempted to be closed

    Maybe:

    Please Login or Register  to view this content.

  4. #4
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Trigger code to open 2nd workbook when 1st workbook is attempted to be closed

    Thank you John.
    It works!
    Question: Why did the "." make such a difference? Why can't i write the code in this procedure like i normally do?
    Btw, i decided to place the code in a module and call the that code from the procedure and it worked

  5. #5
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Trigger code to open 2nd workbook when 1st workbook is attempted to be closed

    Hi ikboy,
    Thanks for your reply.
    Please see my comments below (in blue)
    Quote Originally Posted by ikboy View Post
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim wsV As Worksheet: Set wsV = Sheets("OffersV") here in red, you should check that workbook has this sheets or not.checked and this sheet exists
    If Not DataBaseWb.ReadOnly Then

    LRV = wsV.Range("AI" & Rows.Count).End(xlUp).Row
    For rcell = 2 To LRV
    If wsV.Range("AI" & rcell) = ws.Range("G9") Then 'why ws?You're right, i forgot to declare it as a variable but the code bugged even before it got up to this point

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Trigger code to open 2nd workbook when 1st workbook is attempted to be closed

    Quote Originally Posted by kosherboy View Post
    Thank you John.
    It works!
    Question: Why did the "." make such a difference? Why can't i write the code in this procedure like i normally do?
    Btw, i decided to place the code in a module and call the that code from the procedure and it worked
    It's not the "." that's important. You need to define the workbook. DataBaseWb.Sheets("OffersV")

  7. #7
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Trigger code to open 2nd workbook when 1st workbook is attempted to be closed

    Quote Originally Posted by JOHN H. DAVIS View Post
    It's not the "." that's important. You need to define the workbook. DataBaseWb.Sheets("OffersV")
    I hear, so why am i not required to define the workbook in regular modules?

    The code understands this very well in modules:
    Please Login or Register  to view this content.

  8. #8
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Trigger code to open 2nd workbook when 1st workbook is attempted to be closed

    You would have too if you are working with two workbooks. In your case The Activeworkbook (the one you are closing) and DatabaseWb (the one you are opening and working with). My guess is the Activeworkbook does not have a Sheet("OffersV"). If you don't define the workbook then excel will look for Sheet("OffersV") in the Activeworkbook.

  9. #9
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Trigger code to open 2nd workbook when 1st workbook is attempted to be closed

    I think i will start defining workbooks as it does sound more solid. until now i have not been defining the workbooks when declaring sheets and it seems to have worked, it's the first time by the procedures that it's causing issues.

+ 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] VB Code to open closed workbook and add print area
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-11-2015, 02:27 PM
  2. Matching data from closed workbook with open workbook based on cell values
    By kbkrueger in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-14-2015, 07:42 PM
  3. [SOLVED] Copying data from a closed workbook into an open workbook ignoring excel filter?
    By reach78 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 09-17-2013, 12:31 AM
  4. Macro to open a closed workbook and select copy cells into an open workbook
    By helloganesh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-10-2013, 02:00 PM
  5. retrieve object from multiple closed workbook and paste into open workbook.
    By niailmar in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-22-2012, 12:31 AM
  6. Open file after workbook executing code is closed
    By randell.graybill in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-13-2011, 10:05 PM
  7. Macro code to open a workbook only when closed
    By Jeff in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-16-2006, 04:25 PM

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