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.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Can the code that saves the new book run your code? Otherwise how often do you want Excel to check the folder?
 
Upvote 0
Can the code that saves the new book run your code? Otherwise how often do you want Excel to check the folder?

The code itself is not supposed to save a new book. Basically just "scan" for a new book with a spesific name. Example: Book1 got the code that scans for a new book. If i create a book2 in the same folder, it runs a userform in book1. If i then run book2, and create a book3, the userform will open in book2, and so on.

How often? everything from 1 sec to 10 min i guess is ok, it just checks the folder every now and then. (example every 1 minute, it checks if a new book with a spesific name is created)
 
Upvote 0
This is the timer code you will need.

In workbook.open
Code:
'Call timer to check for user form
My_onTime

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

Check for file
Code:
Sub CheckForFile()
   'What ever code you like

   'call timer again
   My_onTime
End Sub

Is this the sort of thing you were after?
 
Upvote 0
This is the timer code you will need.

In workbook.open
Code:
'Call timer to check for user form
My_onTime

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


Check for file
Code:
Sub CheckForFile()
   'What ever code you like

   'call timer again
   My_onTime
End Sub

Is this the sort of thing you were after?

Thanks for the timer check! Worked nicely.
Now i just need a way to basically check if "book X" exists in the CheckForFile sub.

The book where i got this macro will be copied every now and then, with a incremental new name (number (think version)), so i need the macro to check if (example: Book Y exist) then do a code, else if does not exist, do nothing, and it keeps looping and checking every 1 min.
 
Upvote 0
Well a file exists function is
Code:
'######################################################
'Function FileExists checks if the file exists in
' the directory concerned.
'######################################################
Function FileExists(ByVal FileSpec As String) As Boolean
Dim Attr As Long
ErrMsg = FileSpec & " directory not found."


  ' Guard against bad FileSpec by ignoring errors
  ' retrieving its attributes.
  On Error Resume Next
  Attr = GetAttr(FileSpec)
  If Err.Number = 0 Then
    ' No error, so something was found.
    ' If Directory attribute set, then not a file.
    FileExists = Not ((Attr And vbDirectory) = vbDirectory)
    'error message if not found
    'If FileExists = False Then MsgBox ErrMsg, vbCritical, ThisWorkbook.Name
  End If
End Function

Code:
Dim CFF_name as string
Dim CFF_Found as boolean

Sub CheckForFile()
    If CFF_Found=[COLOR=#574123] FileExists([/COLOR]CFF_name) then
       'create new file
    End If

   'call timer again
   My_onTime
End Sub
 
Upvote 0
Well a file exists function is
Code:
'######################################################
'Function FileExists checks if the file exists in
' the directory concerned.
'######################################################
Function FileExists(ByVal FileSpec As String) As Boolean
Dim Attr As Long
ErrMsg = FileSpec & " directory not found."


  ' Guard against bad FileSpec by ignoring errors
  ' retrieving its attributes.
  On Error Resume Next
  Attr = GetAttr(FileSpec)
  If Err.Number = 0 Then
    ' No error, so something was found.
    ' If Directory attribute set, then not a file.
    FileExists = Not ((Attr And vbDirectory) = vbDirectory)
    'error message if not found
    'If FileExists = False Then MsgBox ErrMsg, vbCritical, ThisWorkbook.Name
  End If
End Function

Code:
Dim CFF_name as string
Dim CFF_Found as boolean

Sub CheckForFile()
    If CFF_Found=[COLOR=#574123] FileExists([/COLOR]CFF_name) then
       'create new file
    End If

   'call timer again
   My_onTime
End Sub

Hmm, i feel like i'm doing something wrong. Got a error message that the macro does not exist or something when i run it.

I inserted both of those codes in "ThisWorkbook", and tried running it without edititing, and as well as changing the "CFF_name" in the CheckForFile, but same error.
Am i doing something wrong here ?
 
Upvote 0
Hmm, i feel like i'm doing something wrong. Got a error message that the macro does not exist or something when i run it.

I inserted both of those codes in "ThisWorkbook", and tried running it without edititing, and as well as changing the "CFF_name" in the CheckForFile, but same error.
Am i doing something wrong here ?

The function and the sub go in a regular module not in "This workbook"
 
Upvote 0
The function and the sub go in a regular module not in "This workbook"
Ah i see.

Ok, so now i got this

ThisWorkbook:

Private Sub Workbook_Open()


Worksheets("Velg").Activate


My_onTime
End Sub


Sub My_onTime()


Application.OnTime Now + TimeValue("00:00:05"), "CheckForFile"
End Sub


and in module22:




'######################################################
'Function FileExists checks if the file exists in
' the directory concerned.
'######################################################
Function FileExists(ByVal FileSpec As String) As Boolean
Dim Attr As Long
ErrMsg = FileSpec & " directory not found."




' Guard against bad FileSpec by ignoring errors
' retrieving its attributes.
On Error Resume Next
Attr = GetAttr(FileSpec)
If Err.Number = 0 Then
' No error, so something was found.
' If Directory attribute set, then not a file.
FileExists = Not ((Attr And vbDirectory) = vbDirectory)
'error message if not found
'If FileExists = False Then MsgBox ErrMsg, vbCritical, ThisWorkbook.Name
End If
End Function












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


When i run the code from the workbook, it waits 5 sec, then gives the error "Sub or Function not defined on "My_onTime" in module22

What do i do wrong here ?
 
Upvote 0
Sub My_onTime needs to be in module22 not ThisWorkbook as well
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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