Excel VBA Auto_Open() detect what workbook you are opening

SourceBrett

New Member
Joined
Apr 19, 2019
Messages
6
Code:
[U]EXAMPLE CODE NOT REAL:
[/U]
Private Sub Auto_Open()

    If ( Activeworkbook.isNewDocuement() == false ) then
        Goto :EOF
    End If

    ' [code example]
    ' [code example]
    ' [code example]

:EOF
End Sub

I have a macro that runs each time excel open which works great but now I want it to run only when opening a blank/new workbook. I've tried a few things but can't figure out how to detect weather or not I'm opening a new workbook or an existing one. I'd like to run the check before running the code to save on processes.
Any suggestions?
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
If you are opening a NEW workbook....it wouldn't have code in it !!
So, I don't see how you are going to apply new code .
 
Upvote 0
So what happens it whenever I open excel no matter if I'm opening for a new document or open a document located on my desktop it goes through the Personal.xlsb Auto_Open macro. Which is fine and doesn't hurt anything but I just wanted to make the code a little more efficient by only going through the entire macro when it's a new document vs a document that's already saved.
 
Upvote 0
OK, maybe something like this to check if the workbook has been saved previously

Code:
Sub Auto_open()
Dim s As String
On Error GoTo EHandler
s = ActiveWorkbook.BuiltinDocumentProperties("last save time")
MsgBox s
Exit Sub
EHandler:
MsgBox "This is a new file"
End Sub
 
Upvote 0
From my testing ActiveWorkbook doesn't work until Auto_Open() finishes. I get the error when opening excel fresh and when opening a document. If this is impossible I'll just move on but I couldn't find this question anywhere on the internet.
 
Upvote 0
view
In all of my personal testing Activeworkbook comes up as nothing. Please see image for what I mean
https://drive.google.com/file/d/11VoQrAHX6-Hwwytyd-inseviiUZj9Jjt/view?usp=sharing
 
Upvote 0
In ThisWorkbook in Personal:

Code:
Dim WithEvents app As Excel.Application

Private Sub Workbook_Open()
  Set app = Application
End Sub

Private Sub app_WorkbookOpen(ByVal Wb As Workbook)
  If Len(Wb.Path) Then
    MsgBox "existing workbook"
  Else
    MsgBox "new workbook"
  End If
End Sub

Workbook_Open needs to run first.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

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