i want to create a vba macro for excel 2013. this vba macro must be available for all excel files, i make a search in the internet and i found that i must put the code in Excel add-in, so i try to make a code for event opening of excel; the code is the following:
Private Sub Workbook_Open()
If (ActiveWorkbook.Path = "C:\GED\TEMP") Then
MsgBox "Hello"
End If
End Sub
the problem is that when i open excel file, vba dont know the active workbook because it opens the file in XLSTART first then my current file, so i have the following error:Run-time error '91 ': Object variable or With block variable not set. So any idea please; i should check the path of the workbook at the opening
You'll need Application-level events to trap the opening of any workbook. Replace your code with this:
Option Explicit
Private WithEvents app As Excel.Application
Private Sub app_WorkbookOpen(ByVal Wb As Workbook)
If UCase$(Wb.Path) = "C:\GED\TEMP" Then MsgBox "Hello"
End Sub
Private Sub Workbook_Open()
Set app = Application
End Sub
Related
I created a simple userform for testing, but one issue comes up and i have no idea how to resolve it.
This is the case:
When I open .xlsm file for the first time and close the userform and workbook, this closes both of them.
But when I continue to click the .xlsm file again, close the userform and the workbook, it (the userform) pops up again.
It seems the workbook doesn't close.
Then i need to close the userform again to close this workbook.
I would like help to understand why the userform comes up again?
The userform only has one listbox controls.
The following code is in a module:
Private Sub Auto_Open()
UserForm1.Show
MsgBox "Close2"
ThisWorkbook.Close
End Sub
The userform contains the following code:
Private Sub UserForm_Initialize()
Application.Visible = False
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
MsgBox "Close1"
End Sub
You could try to put this into the workbook code section as followed.
And not put it in a separate module.
Private Sub Workbook_Open()
Application.Visible = False
userform1.Show
MsgBox "Close2"
ThisWorkbook.Close
End Sub
you can remove the code for the user form initialize.
The thing is as its hidden you might open it twice cause you do cause it does not respond directly.
I have a workbook with multiple external links,
I don't want these to update when the workbook is opened, by me or any other user.
I have tried to achieve this with the simple work book open command below:
Private Sub Workbook_Open()
Workbook.UpdateLinks = xlUpdateLinksNever
End Sub
Am I missing something ?
Put the code in the ThisWorkbook:
Private Sub Workbook_Open()
Me.UpdateLink = xlUpdateLinksNever
End Sub
So I have a macro I wrote that I want to run on close of the workbook.
Unfortunately, the only way to apparently do this is to place the macro in the ThisWorkbook module of the actual file as opposed to having it sit in the PERSONAL.XLSB.
This is not desirable for a few reasons:
The macro would have to be put into every workbook it needs to be run on--I have hundreds.
The workbooks would need to be saved as macro enabled which, in my experience, many email servers won't accept emails with macro enabled workbooks attached.
So ideally I would like to be able to run the macro from the PERSONAL.XLSB in just a general module.
Any suggestions about how this might be possible?
EDIT:
Per instructions at:
http://www.cpearson.com/excel/AppEvent.aspx
PERSONAL.XLSB
CExcelEvents class module
Private WithEvents App As Application
Private Sub Class_Initialize()
Set App = Application
End Sub
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
MsgBox "New Workbook: " & Wb.Name
End Sub
PERSONAL.XLSB
ThisWorkbook
Private XLApp As CExcelEvents
Private Sub Workbook_Open()
Set XLApp = New CExcelEvents
End Sub
Doesn't work if you try to open a different workbook. If you click on PERSONAL.XLSB in recent documents it will trigger the message.
Move this into another ThisWorkbook object for a specific workbook and it still only works on that workbook:
Private XLApp As CExcelEvents
Private Sub Workbook_Open()
Set XLApp = New CExcelEvents
End Sub
So even though the class module is in PERSONAL.XLSB, it appears you still have to put the above into the workbook you want it to run on it, which I think I would still require saving it as an .XLSM and would run into email filter issues.
For some reason this did start working with everything in PERSONAL.XLSB although I didn't change anything. Exciting, but would like to know why.
However, now that I am trying to change the example to actually work how I need it with BeforeClose. So I updated to the following:
PERSONAL.XLSB
CExcelEvents class module
Private WithEvents App As Application
Private Sub Class_Initialize()
Set App = Application
End Sub
Private Sub App_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
MsgBox "Closing the workbook."
End Sub
PERSONAL.XLSB
ThisWorkbook
Private XLApp As CExcelEvents
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Set XLApp = New CExcelEvents
End Sub
Back to what happened yesterday... will only trigger when you go to close PERSONAL.XLSB. One would think that since the PERSONAL.XLSB opens with all workbooks, it would trigger regardless, but it isn't. Again, saving in the ThisWorkbook object of the target workbook works, but isn't a solution due to having to save as an .XLSM and email filters.
You can look at Application Events, which allow you to hook into events at the Application level, using code which can be in your Personal.xlsb
Here's a good starting point: http://www.cpearson.com/excel/AppEvent.aspx
I'm writing vba which manipulates data within a worksheet however I'm trying to make it run whenever a workbook is opened.
The problem I'm having is that due to the workbook (that the code needs to run on) is different/new every time, I need the auto_open code to be within a personal macro workbook.
Sub Auto_Open()
Dim bookname As String
Dim checkbook As String
Dim Workbook As Workbook
For Each Workbook In Application.Workbooks
bookname = Workbook.Name
checkbook = Left(bookname, 3)
If checkbook = "EDN" Then
Data_generator
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.Quit
Else
End If
Next Workbook
End Sub
When this code runs it checks all open workbooks and sees if the first 3 letters of it are 'EDN', if it is then run the public sub called 'Data_generator', save it and quit. If it isn't check the next open workbook, etc.
When a file is opened from windows explorer, excel launches (with both the desired workbook and the personal macro workbook) however because excel opens the personal macro workbook first and runs the code before opening the desired workbook it doesn't find a workbook called 'EDN'.
If the above code is ran after both workbooks have opened then the code works as intended and cycles through each open workbook to see if there's one called 'EDN' (this was proved by putting a messagebox after the 'then' and running the code), if so run the sub.
I've proved this by putting a messagebox after the 'else', when this is done it displays the messagebox with the workbook I want, not open. After the message box is cleared, the workbook then opens.
Is there any way to make the desired workbook open first or any other work around for this?
You can create an Add-in that runs whenever a workbook is open
https://msdn.microsoft.com/en-us/library/office/gg597509(v=office.14).aspx
this tool may help to create the Add in http://www.andypope.info/vba/ribboneditor.htm
You should be able to use the Application.OnWindow event to trigger a macro when a file is opened or closed.
In ThisWorkbook
Private Sub Workbook_Open()
Call StartTracking
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call StopTracking
End Sub
In a Module
Function StartTracking()
Application.OnWindow = "AutoRunOnWindowChange"
End Function
Function StopTracking()
Application.OnWindow = ""
End Function
Function AutoRunOnWindowChange()
If Left(ActiveWorkbook.Name, 3) = "EDN" Then
Call Data_generator
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.Quit
End If
End Function
As I very often have to problem, that the tick vanishes in the settings: Calculate before save. (I don't know the exact term as my office version is in German).
That's why I tried to use VBA to solve the problem. I used the following code in my Excel file:
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
If Application.CalculateBeforeSave = False Then
If MsgBox("Caution! Should >calculatebeforesave< be activated?", vbYesNo) = vbYes Then
Application.CalculateBeforeSave = True
Else
End If
Else
End If
End Sub
I put this into "Thisworkbook". But I would like this code to be ran in every workbook I work with (at least all these which allow for macros).
My suggestion was to write ActiveWorkbook_BeforeSave... instead of Workbook_BeforeSave and then put the code in a module in the PERSONAL Macro file. But this doesn't work.
I think you need to use the Excel Applications events rather than workbook events to achieve this, such as in this example
In your PERSONAL workbook right click and insert a a Class Module (Class 1)
Add something similar to below to Class 1:
Public WithEvents appevent As Application
Private Sub appevent_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
'Add what you would like to happen before a workbook closes
End Sub
Next open ThisWorksheet and add code along these lines (I think the PERSONAL workbook opens automatically when Excel starts):
Dim myobject As New Class1
Private Sub Workbook_Open()
Set myobject = Application
End Sub