Using code at multiple places

Koen

New Member
Joined
Mar 10, 2011
Messages
18
Hi, I have the following code:

Code:
Option Explicit
Option Base 1

Private MyOPCServer As OPCServer
Private WithEvents MyOPCgroup1 As OPCGroup
Private WithEvents MyOPCgroup2 As OPCGroup
Private MyOPCItems1, MyOPCItems2 As OPCItem

Private Sub CommandButton1_Click()  'connect

    Set MyOPCServer = New OPCServer
    
    Call MyOPCServer.connect("S7200.OPCServer")
    
    Set MyOPCgroup1 = MyOPCServer.OPCGroups.Add("Gruppe1") 
    Set MyOPCgroup2 = MyOPCServer.OPCGroups.Add("Gruppe2") 
    
    MyOPCgroup1.IsSubscribed = True 
    MyOPCgroup1.UpdateRate = 0 
    MyOPCgroup2.IsSubscribed = True 
    MyOPCgroup2.UpdateRate = 0 
   
    Set MyOPCItems1 = MyOPCgroup1.OPCItems.AddItem("MicroWin.PLC1.Plaatnummer", 1)
    Set MyOPCItems2 = MyOPCgroup2.OPCItems.AddItem("MicroWin.PLC1.M1,0", 2)

End Sub
It works fine, but I want to run this code in:


Code:
Private Sub CommandButton1_Click()

End Sub
and in:


Code:
Private Sub Workbook_Open()
    
    
End Sub
This is probably a simple question, but how do I do this, and where and how should I do the declarations (especially the 'WithEvents' declaration is causing problems)?

Thank you very much!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,224,594
Messages
6,179,792
Members
452,942
Latest member
VijayNewtoExcel

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