File to open hidden by macro only, but visible when opened through conventional means?

kymmeeh

New Member
Joined
May 20, 2010
Messages
8
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Inside File 1, if the user types "NEW" into Column B...I want File 2 to open in the background. It pops open a user form to get information from the user. Works fine.

Whenever I want another user to edit File 2, upon opening, it is always hidden. How can I address that?

VBA Code:
'Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
If Not Intersect(Target, Range("B10:B500")) Is Nothing Then
    If Target.Rows.count > 1 Or Target.Columns.count > 1 Then
        'nothing
    Else
        If InStr(1, Target.Value, "New", 1) > 0 Then
            If CheckFileIsOpen("File 2.xlsm") = False Then
                'On Error Resume Next
                    Application.ScreenUpdating = False 
                    Application.DisplayAlerts = False
                    Workbooks.Open ("S:\File 2.xlsm")
                    Windows("File 2.xlsm").Visible = False
                'On Error GoTo 0
            End If
            NewRMsBox.Show
        End If
    End If   
End If

Here is the coding that is in File 1 that closes the hidden File 2.
VBA Code:
'***Private Sub Workbook_BeforeClose(Cancel As Boolean)
'
If CheckFileIsOpen("File 2.xlsm") = True Then
    On Error Resume Next
    Workbooks("S:\File 2.xlsm").Save
    Workbooks("S:\File 2.xlsm").Close
    On Error GoTo 0
End If

Application.DisplayAlerts = False
If ThisWorkbook.ReadOnly Then
    ThisWorkbook.Close savechanges:=False
End If
Application.DisplayAlerts = True

'End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

    If CheckFileIsOpen("File 2.xlsm") Then
        On Error Resume Next
        Windows("File 2.xlsm").Visible = True
        Workbooks("S:\File 2.xlsm").Close True
        On Error GoTo 0
    End If

(...)
Artik
 
Upvote 0
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

    If CheckFileIsOpen("File 2.xlsm") Then
        On Error Resume Next
        Windows("File 2.xlsm").Visible = True
        Workbooks("S:\File 2.xlsm").Close True
        On Error GoTo 0
    End If

(...)
Artik
Thank you, Artik. I this in my File 1 Sub Workbook_BeforeClose, but after closing File 1, and reopening only File 2, it still opens as hidden.

I tried it adding the code to File 2 open Opening instead. This worked, but when opening File 2, now has two windows of Excel open, one blank (no file at all) and one with File 2. Are you able to help me modify this to make it work?

VBA Code:
Private Sub Workbook_Open()
On Error Resume Next
Windows("File 2.xlsm").Visible = True

On Error GoTo 0

End Sub
 
Upvote 0
I confess that I did not test the previous code with the Workbook_BeforeClose event procedure, but in a "regular" procedure written in the standard module. And there it worked correctly. However, I forgot that Workbook_BeforeClose has its quirks. This is just one of them.
Remove the Workbook_BeforeClose procedure from the ThisWorkbook module, and insert the Auto_Close procedure in the standard module (e.g. Module1). This is an old-style "event" procedure that is called when the workbook is closed.
VBA Code:
Sub Auto_Close()
    If CheckFileIsOpen("File 2.xlsm") Then
        'On Error Resume Next
        Windows("File 2.xlsm").Visible = True
        Workbooks("File 2.xlsm").Close True
        'On Error GoTo 0
    End If

    Application.DisplayAlerts = False
    If ThisWorkbook.ReadOnly Then
        ThisWorkbook.Close savechanges:=False
    End If
    Application.DisplayAlerts = True

End Sub
Note the order in which the procedures are done. If for what reason you need to have both procedures: Workbook_BeforeClose and Auto_Close, then _BeforeClose will execute first, followed by Auto_Close.

Artik
 
Last edited:
Upvote 0
Solution
Basically, part of the code does not make sense, so the code can be shortened to the form:
VBA Code:
Sub Auto_Close()
    
    If CheckFileIsOpen("File2.xlsm") Then
        'On Error Resume Next
        Windows("File2.xlsm").Visible = True
        Workbooks("File2.xlsm").Close True
        'On Error GoTo 0
    End If

    If ThisWorkbook.ReadOnly Then
        ThisWorkbook.Saved = True
    End If

End Sub

Artik
 
Upvote 0
Thank you, Artik! New to the Workbook_beforeclose, so good to know. Appreciate it.
 
Upvote 0

Forum statistics

Threads
1,216,049
Messages
6,128,496
Members
449,455
Latest member
jesski

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