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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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,224,727
Messages
6,180,583
Members
452,988
Latest member
wcself81

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