VBA check if a new workbook is created

Wicked_

Board Regular
Joined
Jun 5, 2018
Messages
81
Hi.

Got a question.
Is there a way to create a VBA code, that constantly checks a specific folder, if a new workbook is created?
For it to be easy to understand, lets pretende workbook1 is called 1.xlsm (which im using), then if i create a book called 2.xlsm inside that same folder, i want to code to recognize it, and launch a specific userform inside book 1.

Is this possible ?

Thanks.
 
Sub My_onTime needs to be in module22 not ThisWorkbook as well

Ok, moved it to module22:

Sub My_onTime()
My_onTime
End Sub


Sub CheckForFile()


Dim CFF_name As String
Dim CFF_Found As Boolean
If CFF_Found = FileExists(TestBook2) Then
'create new file
UserForm7.Show

End If


'call timer again
My_onTime
End Sub


Then i get a error:
Out of stack space (not familiar with that error)

at the sub my_ontime i put there.
It opened the userform, which worked great, but also, if i renamed the testbook2 to something else, it will still open the userform, even if it does not match the CFF_Found.
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
The Sub My_onTime is wrong, should be -

Code:
Sub My_onTime()
    Application.OnTime Now + TimeValue("00:01:00"), "CheckForFile"  [COLOR=#444444]End Sub
[/COLOR]

Is TestBook2 in FileExists(TestBook2) a variable or a fixed string? If a fixed string it should be in quotes.
 
Upvote 0
The Sub My_onTime is wrong, should be -

Code:
Sub My_onTime()
    Application.OnTime Now + TimeValue("00:01:00"), "CheckForFile"  [COLOR=#444444]End Sub
[/COLOR]

Is TestBook2 in FileExists(TestBook2) a variable or a fixed string? If a fixed string it should be in quotes.


The workbooks that im going to enter there is going to be a fixed string.
I updated the code, and i get no more errors! yay.

Still tho, it shows the userform every 1 min, even if i use quotes or not. (also tried adding .xlsm after the name, with no luck)
 
Upvote 0
Did you put the quotes round the name?
 
Upvote 0
Hmmm well to start with - I am an idiot :stickouttounge: so the use of FileExists is

Code:
If FileExists("C:\Temp\TestBook2.txt") Then

Note : It also includes the directory path
 
Upvote 0
Hmmm well to start with - I am an idiot :stickouttounge: so the use of FileExists is

Code:
If FileExists("C:\Temp\TestBook2.txt") Then

Note : It also includes the directory path

Haha no worries mate :P

That worked, but in a weird way.
If i say:

If FileExist("C:\Temp\TestBook2.xlsm") Then

Then the userform wont show, but if i edit the file itself to example TestBook3, then it launches fine.
Its like If FileExist looks if it does not exist?

And the last "problem" (i swear :P )
I want it to autorun when the workbook is launched.
I tried:

Private Sub Workbook_Initialize()
My_onTime (and some other ways to write it). but it wont autorun (shouldent that be inside Initialize?)
 
Upvote 0
Did you put this in ThisWorkbook?

Code:
Private Sub Workbook_Open()
 CheckForFile
End Sub
 
Upvote 0
Did you put this in ThisWorkbook?

Code:
Private Sub Workbook_Open()
 CheckForFile
End Sub

/facepalm...

Thanks :P that worked like a charm.

Know if its any logical explanation why it wont open the userform if it actually finds the correct xlsm file? It all works perfect, except that it opens the form when it cant find the file i wrote in fileexist. If i change the book name to something else, it works, but if it got the correct name, it will ignore it.
 
Upvote 0
Not sure, I used this as a test.

Code:
Sub Testfile()
If FileExists("C:\Temp\TestBook2.txt") Then
    MsgBox "Found"
Else
    MsgBox "Nope"
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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