ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Auto_Open (https://www.excelbanter.com/excel-programming/369245-auto_open.html)

Alastair79

Auto_Open
 
Is it possiable to say if a .csv file with the name "Albert" is open then run
the following macro else don't run anything. The file does not always exist
so can't just set it to that specific file, will have to be saved on the
Personal.xls

Thanks............... Alastair.

Die_Another_Day

Auto_Open
 
This is possible. However, I think you have to create an Application
Level Event and pass that down to your macro. This requires using a
class module, ThisWorkbook procedure, and a regular module
We'll start with the Class Module.
In Personal.xls add a class module and paste this code:
Option Explicit

Public WithEvents App As Application

Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
If Wb.Name = "Albert.csv" Then
YourMacro 'This Runs a macro with the name "YourMacro" Rename
as needed
End If
End Sub

Now onto the ThisWorkbook event
Under the "ThisWorkbook" code paste this:

Option Explicit

Private Sub Workbook_Open()
Set AppClass.App = Application
End Sub

Now add a regular module and add this:

Option Explicit
Public AppClass As New EventClass

Sub YourMacro()
MsgBox ActiveWorkbook.Name
End Sub

That should do it.

Charles Chickering
xl Geek

Alastair79 wrote:
Is it possiable to say if a .csv file with the name "Albert" is open then run
the following macro else don't run anything. The file does not always exist
so can't just set it to that specific file, will have to be saved on the
Personal.xls

Thanks............... Alastair.




All times are GMT +1. The time now is 06:06 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com