Need macro to only close workbook at the end if it wasn't already open when Workbook Open Event runs

sspatriots

Well-known Member
Joined
Nov 22, 2011
Messages
572
Office Version
  1. 365
Platform
  1. Windows
Hi,

My "Workbook_Open" event calls the code below when I open the workbook. This code in turn will open another workbook temporarily (just long enough to copy some information from it) and then closes it again. I need to be able have the code first check to see if the referenced file is already open when this code is initialized, because if it is I don't want my code to close the reference workbook at the end. I hope this makes sense. Not sure how else to explain this.


Thanks, SS


VBA Code:
Sub Refresh_n_ResizeAllPOTables()

Dim wb1, wb2 As Workbook
Dim file_path1 As String

Set wb1 = ThisWorkbook

file_path1 = "H:\Jobs\SPS-PO Block History - DEM-030524-01.xlsm"      'Path to PO Block History - DEM.xlsm
Set wb2 = Workbooks.Open(Filename:=file_path1, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)     'PO Block History - DEM.xlsm

'Does a lot of stuff here

wb2.Close SaveChanges:=False

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Please try this. Below is a snippet of code to check if a workbook is open already. RegOpen stores whether the file was open before running this procedure so I can choose to close it or not at the end. RegFile stores just the filename with the extension.

VBA Code:
Dim RegOpen As Boolean
Dim RegWB As Workbook
Dim RegFile As String

  On Error Resume Next
  If wbOpen(RegFile, RegWB) = False Then                          'Check if open already, return open RegWB
    Set RegWB = Workbooks.Open(RegPath, WriteResPassword:="admin")
    RegOpen = False
  Else
    RegOpen = True
  End If
  On Error GoTo 0
  If RegWB Is Nothing Then
    MsgBox "Register file not found"
    Exit Sub
  End If


VBA Code:
Function wbOpen(fileName As String, wbO As Workbook) As Boolean
    
    Dim WB As Workbook
    For Each WB In Application.Workbooks
      If WB.Name = fileName Then
        Set wbO = WB
        wbOpen = True
        Exit For
      End If
    Next WB
        
End Function
 
Upvote 0
In your case you may want to make RegOpen as a public variable to you can use it in other routines. Add to the top of a standard module.
PUBLIC RegOpen as boolean
 
Upvote 0
Try this:

VBA Code:
Sub Refresh_n_ResizeAllPOTables()
  Dim wb As Workbook, wb1 As Workbook, wb2 As Workbook
  Dim file_path1 As String, sFile As String
  Dim isOpen As Boolean
  
  Set wb1 = ThisWorkbook
  
  sFile = "SPS-PO Block History - DEM-030524-01.xlsm"
  file_path1 = "H:\Jobs\" & sFile      'Path to PO Block History - DEM.xlsm
  
  For Each wb In Workbooks
    If LCase(wb.Name) = LCase(sFile) Then
      isOpen = True
      Exit For
    End If
  Next
  
  If isOpen = False Then
    Set wb2 = Workbooks.Open(Filename:=file_path1, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)     'PO Block History - DEM.xlsm
  End If
  
    'Does a lot of stuff here
    
  If isOpen = False Then
    wb2.Close SaveChanges:=False
  End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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