Application event

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Can someone please explain the difference between these two ideas:

Idea 1:

This is in ThisWorkbook:

Code:
Option Explicit

    Private abc As Class1

Private Sub Workbook_Open()

    Set abc = New Class1

End Sub

This is in Class1:

Code:
Option Explicit

    Private WithEvents xlApp As Application

Private Sub Class_Initialize()

    Set xlApp = Application

End Sub

Private Sub xlApp_NewWorkbook(ByVal Wb As Workbook)
  
    Wb.Worksheets("Sheet1").Range("A1").Value = "Created on " & Date

End Sub


Idea 2:

This is in ThisWorkbook:

Code:
Option Explicit

    Private abc As New Class1

Private Sub Workbook_Open()

    Set abc.xlApp = Application

End Sub

This is in Class1:

Code:
Option Explicit

    Public WithEvents xlApp As Application

Private Sub Class_Initialize()

    Set xlApp = Application

End Sub

Private Sub xlApp_NewWorkbook(ByVal Wb As Workbook)

    Wb.Worksheets("Sheet1").Range("A1").Value = "Created on " & Date

End Sub


The differences being between Sub Workbook_Open(), where one is:

Code:
Set abc = New Class1

and the other:

Code:
Set abc.xlApp = Application

Also in Class1, WithEvents is declared as Private in the First but Public in the second.

Both First and Second work, so what impact do the differences have?

Thanks
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
In the second example, you are hooking the application events twice unnecessarly. You can just get rid of :
VBA Code:
Private Sub Class_Initialize()

    Set xlApp = Application

End Sub

BTW, the first method is what you should use... Have a read at this What's the difference between Dim As New vs Dim / Set

thanks

re delcaring public v private, how comes it doesn't seem to make a difference in these two examples?
 
Upvote 0
re delcaring public v private, how comes it doesn't seem to make a difference in these two examples?

Do you mean ;
VBA Code:
Public/Private WithEvents xlApp As Application

If you declare withevents Private then it won't compile at :
VBA Code:
  Set abc.xlApp = Application

Bexause the xlApp class Method won't be visible to the caller code
 
Upvote 0
Do you mean ;
VBA Code:
Public/Private WithEvents xlApp As Application

If you declare withevents Private then it won't compile at :
VBA Code:
  Set abc.xlApp = Application

Bexause the xlApp class Method won't be visible to the caller code

Yes I did.

Thanks again for the explanation.
 
Upvote 0
You are welcome.

I strongly recommend that read the page in the link I posted above if you haven't read it yet.


Yes I have read it and am aware of the benefits of instantiating using two lines instead of a single line.
 
Upvote 0

Forum statistics

Threads
1,214,858
Messages
6,121,956
Members
449,057
Latest member
FreeCricketId

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