How to detect workbook New openings (vba)

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
๐™ท๐šŽ๐š•๐š•๐š˜ ๐š ๐š˜๐š›๐š•๐š

๐™ต๐š˜๐š› ๐šŽ๐šŽ๐šก๐šŠ๐š–๐š™๐š•๐šŽ, ๐š’๐š— ๐šŠ๐šŒ๐šŒ๐šŽ๐šœ๐šœ ๐šข๐š˜๐šž ๐šŒ๐šŠ๐š— ๐š˜๐š™๐šŽ๐š— ๐š“๐šž๐šœ๐š ๐š˜๐š—๐šŽ ๐š๐š’๐š•๐šŽ ๐šŠ๐š ๐šŠ ๐š๐š’๐š–๐šŽ.

๐™ธ ๐šŠ๐š– ๐š๐š›๐šข๐š’๐š—๐š ๐š๐š˜ ๐šŠ๐šŠ๐šŒ๐š‘๐š’๐šŽ๐šŸ๐šŽ ๐š๐š‘๐šŠ๐š ๐š’๐š— ๐šŽ๐šก๐šŒ๐šŽ๐š•.

๐šœ๐š˜ ๐š ๐š‘๐šŽ๐š— ๐š’ ๐šŠ๐š– ๐š˜๐š™๐šŽ๐š—๐š’๐š—๐š ๐š–๐šข ๐š๐š’๐š•๐šŽ ๐š’ ๐šŒ๐šŠ๐š— ๐šŒ๐š˜๐šž๐š—๐š ๐š๐š‘๐šŽ ๐š—๐š—๐šž๐š–๐š‹๐šŽ๐š› ๐š˜๐š ๐š˜๐š™๐šŽ๐š—๐šŽ๐š ๐š ๐š˜๐š›๐š”๐š‹๐š˜๐š˜๐š”๐šœ ๐š๐š‘๐šŽ๐š— ๐šŒ๐š•๐š˜๐šœ๐šŽ ๐š–๐šข ๐š๐š’๐š•๐šŽ ๐š˜๐š› ๐š˜๐š๐š‘๐šŽ๐š›๐š ๐š’๐šœ๐šŽ

๐šƒ๐š‘๐šŽ ๐š˜๐š—๐š•๐šข ๐š’๐šœ๐šœ๐šž๐šŽ ๐š’ ๐š‘๐šŠ๐šŸ๐šŽ ๐š—๐š˜๐š  ๐š’๐šœ ๐š‘๐š˜๐š  ๐š๐š˜ ๐š๐šŽ๐š๐šŽ๐šŒ๐š that when I am trying to open a different file while my file with the vba code is opened

If there is a way, I will love to know.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You'll need to use an application-level event handler. Try the following...

[Class module]

VBA >> Insert >> Class module, and name it clsApp.

VBA Code:
Option Explicit

Public WithEvents xl As Application

Private Sub xl_WorkbookOpen(ByVal Wb As Workbook)
    If Not Wb Is ThisWorkbook Then
        MsgBox "WorkbookOpen, " & Wb.Name
        'your code here
        '
        '
    End If
End Sub

[Regular module]

VBA >> Insert >> Module

VBA Code:
Option Explicit

Dim xlApp As clsApp

Sub init()

    Set xlApp = New clsApp
  
    Set xlApp.xl = Application
  
End Sub

[ThisWorkbook module]

VBA Code:
Option Explicit

Private Sub Workbook_Open()
    init
End Sub

Then save the workbook, close and re-open it.

Hope this helps!
 
Last edited:
Upvote 0
Okay nice
It is getting closer

It works fine with already saved or created files but when i create new workbook the message alert does not show up
 
Upvote 0
For a new workbook, you'll need to add another event handler. So your class module should now look something like this...

VBA Code:
Option Explicit

Public WithEvents xl As Application

Private Sub xl_NewWorkbook(ByVal Wb As Workbook)
    If Not Wb Is ThisWorkbook Then
        MsgBox "NewWorkbook, " & Wb.Name
        'your code here
        '
        '
    End If
End Sub

Private Sub xl_WorkbookOpen(ByVal Wb As Workbook)
    If Not Wb Is ThisWorkbook Then
        MsgBox "WorkbookOpen, " & Wb.Name
        'your code here
        '
        '
    End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,226,481
Messages
6,191,263
Members
453,650
Latest member
EKwitz

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