Worksheet_Change event for multiple external workbooks - excel

I have a class module that acts as a Worksheet_Change event for an external workbook. I am reworking my project, and the current setup only allows for one external workbook sheet_change event. I would, however, like this event listen for changes on multiple workbooks.
The amount of workbooks is unknown until midway in the code, so I can't create a predetermined amount of classes for each workbook.
Module Functions:
Dim oWb2 As New UpdaterUnkowns
Public Function
'Code...
Set oWb2.Workbook = newfile
End Function
Class module UpdaterUnknowns:
Public WithEvents m_wb As Workbook
Public CellVal As String
Public Property Set Workbook(wb As Workbook)
Set m_wb = wb
End Property
Public Property Get Workbook() As Workbook
Set Workbook = m_wb
End Property
Public Sub m_wb_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'Code...
End Sub
Set oWb2.Workbook = newfile sets the workbook for the class module. Could I pass multiple workbooks to the event?

To handle every open workbook, you could do something like this:
Dim UU_collection As Collection
sub mysub()
Set uu_collection = New Collection
Dim wb As Workbook
For Each wb In Application.Workbooks
Dim oWb2 As UpdaterUnknowns
Set oWb2 = New UpdaterUnknowns
Set oWb2.Workbook = wb
uu_collection.Add oWb2
Next wb
End Sub

Related

Excel: pass workbook object from sub to userform

I want to pass two objects (one open workbook and other a table) from sub procedure to userform. I have made both of them global before the sub.
When I write just the two public declarations before Sub (they are set inside Sub),works good within Sub but when the userform initializes, it throws the error "Object variable or with block variable not set"
Public N As Workbook
Public E As ListObject
Sub...
I tried to set these two objects before Sub, the Sub doesn't run, throws compile error, "Invalid outside procedure"
Public N As Workbook
Set N = Workbooks.Open(ThisWorkbook.Sheets("New").Range("A1").Text)
Public E As ListObject
Set E = N.Sheets(1).ListObjects(1)
Sub...
How to pass object variables from Sub procedures to Userforms? Currently the only solution I see is to set the objects in both the Sub procedure and the Userform.
Add a Sub to your UserForm to set your Workbook and ListObject objects.
Userform:
Private pWB As Workbook
Private pLO As ListObject
Public Sub SetObjects(ByVal wb As Workbook, ByVal lo As ListObject)
Set pWB = wb
Set pLO = lo
End Sub
In your calling code:
Sub CallingCode()
Dim UF As UserForm1 ' Don't use the global UserForm1 object
Set UF = New UserForm1
UF.SetObjects N, E
UF.Show
End Sub

VBA Global Object Variables

I need some help with global variables. The code I have below needs to be accessed by a multiple Modules and a User form. My attempts to make them a global failed.
Dim wb As Workbook
Dim ws As Worksheet
Set wb = Workbooks.Open("Some.xlsm")
Set ws = wb.Worksheets("Some Sheet")
I have tried making a module to hold them but ran into a runtime error. My program works fine if I copy this code to each function that uses it but that becomes messy. I would like to set it up to just have to change the two lines in order to affect the entire file. When I made a new module it did not like Dim.
Edit:
Public wb As Workbook
Public ws As Worksheet
Public Sub modVariables()
Set wb = Workbooks.Open("V:\My Stuff\Templates\Work Order Batch Creation Template\Sandbox\WorkOrderDatabase.xlsm")
Set ws = wb.Worksheets("Description - Processes")
End Sub
Just declare them as public variables like the following:
Public wb As Workbook
Public ws As Worksheet
Thats how it should look like:

Automatically run macro if the user inserted a new worksheet

I would like my macro to run automatically if it detects that the user inserted a new worksheet into the workbook (existing & new).
Sub macro_run()
Dim Newws As Worksheet
Dim wb As Excel.Workbook
Dim sample1 As Worksheet
With ThisWorkbook
Set sample1 = .Sheets("Template")
For Each wb In Application.Workbooks
If Newws = sample1 Then
Application.Run "PERSONAL.XLSB!Opennew"
End If
Next wb
End With
End Sub
As mentioned in the comments you need to handle WorkbookNewSheet at Application level.
'-- Create a new Class.
'-- Name it clsGlobalHandler.
'-- Following Code goes in that class
'/ Create a variable to hold Application Object
Public WithEvents xlApp As Application
'/ Handle NewSheet event. Invoked whenever a new sheet is added
Private Sub xlApp_WorkbookNewSheet(ByVal Wb As Workbook, ByVal Sh As Object)
MsgBox Sh.Name
End Sub
'-- Create a new module
'-- Following code goes there
Option Explicit
'/ A new instance for the Class that we created.
Dim oGh As New clsGlobalHandler
'/ To start tracking sheet additions call this method first. Most likely in WorkBook_Open
'/ Once called any new sheet across the app insatnce will be intercepted.
Sub SetGlobalHandler()
Set oGh.xlApp = Application
End Sub
'/ Call this to remove the global handler.
Sub ResetGlobalHandler()
Set oGh.xlApp = Nothing
End Sub

How to set an open excel file as a Workbook object in VBA

I need to set an excel file that is already open on the computer as the workbook object.
Dim wb As Workbook
Dim sh As Worksheet
Set wb = *the open excel file*
Set sh = wb.Worksheets("FlatFile-FichierPlat")
Set wb = Workbooks("yourWorkbookName")
You can just use GetObject to get a reference to the Excel application running it, and then iterate the workbook collection:
Dim wb As Workbook
Dim sh As Worksheet
Set wb = GetObject( ,"Excel.Application").ActiveWorkbook
Set sh = wb.Worksheets("FlatFile-FichierPlat")
For more information, see MSDN.
If your code is opening the file, then you already have the object reference:
Dim book As Workbook
Set book = Application.Workbooks.Open(path) 'reference is returned by the Open function
If the user can open the file at any arbitrary point in time, then you need to handle the Application.WorkbookOpen event - you can do that by declaring a Private WithEvents {name} As Excel.Application object variable at module scope in ThisWorkbook, and setting its reference to Application when ThisWorkbook opens, i.e. in the Workbook_Open handler:
Option Explicit
Private WithEvents app As Excel.Application
Private Sub Workbook_Open()
Set app = Application
End Sub
Private Sub app_NewWorkbook(ByVal Wb As Workbook)
'Wb holds the object reference for the new workbook that was created
End Sub
Private Sub app_WorkbookOpen(ByVal Wb As Workbook)
'Wb holds the object reference for the workbook that was opened
End Sub

Assign workbook to variable

I want to set ActiveWorkbook to a variable When workbook opens simultaneously ActiveWorkbook assign to a variable and that variable i can use in whole VBA excel project.
I tried to assigned in ThisWorkbook excel object on Workbook_open() function but it does not work.I provide that code below.
Private Sub Workbook_Open()
On Error Resume Next
Set WRBK = ActiveWorkbook
#If Mac Then
#Else
'unprotectVBProjProp
UnlockVBA ' Sujith ID: 12482
AddReferences ' Sujith ID: 12482
' protectVBProjProp
#End If
'MsgBox "xla Workbook opened"
Set eventInstance = New bwEvents
End Sub
So how can i set activeworkbook to a variable??
I am not so sure what are all the commands in the middle, like #If Mac Then , and UnlockVBA.
If you want to set the ActiveWorkbook to object WRBK, you will need to define WRBK in a regulare module as Public, and then use something like the code below:
Code in ThisWorkbook Module
Private Sub Workbook_Open()
Set WRBK = ActiveWorkbook
TestWorkbookName ' call sub <-- this is just to test the the workbook was assigned correctly
End Sub
Code in Regular Module
Option Explicit
Public WRBK As Workbook
Sub TestWorkbookName()
MsgBox WRBK.Name
End Sub

Resources