Dupe Excel File recognition

samchox

New Member
Joined
Feb 2, 2004
Messages
29
Main.xls is a workbook that has an Auto_open macro that saves the workbook automatically as Sam_mm_dd.xls in path c:\Files\

So when Main.xls is opened on Feb 10, its gets saved as Sam_2_10.xls

Now, multiple users will access this workbook during the day, so when Main.xls is opened for the second time in the day, the user gets a prompt 'Sam_2_10.xls already exists, overwrite Y/n, etc'

Instead of that, I want a macro that will realize that this user needs to open Sam_2_10.xls from C:\Files\ and should automatically revert to that auto saved workbook, instead of offering to overwrite it and wipe off all thats been done previously for the day.

From the users perspective, there need not be any intervention, the user just opens Main.xls, if it has not yet been auto saved for the day, excel will do it for them. And if it has already been auto saved, excel will open the relevant workbook for them.

Any Suggestions
:unsure:
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

Damon Ostrander

MrExcel MVP
Joined
Feb 17, 2002
Messages
4,239
Hi samchox,

I believe the code would just be

If DIR("c:\Files\" & TodayFilename) <> "" then
'file exists
Workbooks.Open c:\Files\" & TodayFilename
ThisWorkbook.Close
End If

Of course, TodayFilename must contain the string you have previously created from today's date, e.g., "Sam_2_10.xls".

I didn't add code to handle the situation where another user has the file open at the time this macro runs, but hope this gets you started.

Damon
 

samchox

New Member
Joined
Feb 2, 2004
Messages
29
Thanks Damon

Heres what i have, doesnt seem to work that way I expected it to. It still asks me if i want to overwrite Y/N

Sub Auto_open()

If Dir("I:\SHARE\CAM\productivity\Repository\samarth\", vbDirectory) = vbNullString
Then MkDir "I:\SHARE\CAM\productivity\Repository\samarth\"
ActiveWorkbook.SaveAs Filename:= _
"I:\SHARE\CAM\productivity\Repository\Samarth\" & Range("ab3") & ".xls"

If Dir("I:\SHARE\CAM\productivity\Repository\samarth\" & vbDirectory) <> "" Then
'file exists
Workbooks.Open "I:\SHARE\CAM\productivity\Repository\samarth\" & vbDirectory
ThisWorkbook.Close
End If
End Sub
 

Damon Ostrander

MrExcel MVP
Joined
Feb 17, 2002
Messages
4,239
Hi again sam,

The second test is not working because of the vbDirectory, and the fact that it doesn't attempt to find the file name you mentioned. Assuming that somehow your code has previously put the correct updated filename in the cell ab3 of the active worksheet, I believe this is what you really want:

If Dir("I:\SHARE\CAM\productivity\Repository\samarth\", vbDirectory) = vbNullString
Then MkDir "I:\SHARE\CAM\productivity\Repository\samarth\"
ActiveWorkbook.SaveAs Filename:= _
"I:\SHARE\CAM\productivity\Repository\Samarth\" & Range("ab3") & ".xls"

ElseIf Dir("I:\SHARE\CAM\productivity\Repository\samarth\" & Range("ab3") & ".xls" ) <> "" Then
'file exists
Workbooks.Open "I:\SHARE\CAM\productivity\Repository\samarth\" & Range("ab3") & ".xls"
ThisWorkbook.Close
End If
End Sub
 

samchox

New Member
Joined
Feb 2, 2004
Messages
29

ADVERTISEMENT

Im getting a compile error : Else without if

And if I change the ElseIf to just If, like below, its just overwriting my workbook.

Sub Auto_open()

If Dir("I:\SHARE\CAM\productivity\Repository\samarth\", vbDirectory) = vbNullString Then MkDir "I:\SHARE\CAM\productivity\Repository\samarth\"
ActiveWorkbook.SaveAs Filename:= _
"I:\SHARE\CAM\productivity\Repository\Samarth\" & Range("ab3") & ".xls"

If Dir("I:\SHARE\CAM\productivity\Repository\samarth\" & Range("ab3") & ".xls") <> "" Then
'file exists
Workbooks.Open "I:\SHARE\CAM\productivity\Repository\samarth\" & Range("ab3") & ".xls"
ThisWorkbook.Close
End If
End Sub
 

Damon Ostrander

MrExcel MVP
Joined
Feb 17, 2002
Messages
4,239
Hi samchox,

I believe the problem is that the MkDir statement is on the same line as the If Dir... Try this:

Sub Auto_open()

If Dir("I:\SHARE\CAM\productivity\Repository\samarth\", vbDirectory) = vbNullString Then
MkDir "I:\SHARE\CAM\productivity\Repository\samarth\"
ActiveWorkbook.SaveAs Filename:= _
"I:\SHARE\CAM\productivity\Repository\Samarth\" & Range("ab3") & ".xls"

ElseIf Dir("I:\SHARE\CAM\productivity\Repository\samarth\" & Range("ab3") & ".xls") <> "" Then
'file exists
Workbooks.Open "I:\SHARE\CAM\productivity\Repository\samarth\" & Range("ab3") & ".xls"
ThisWorkbook.Close
End If
End Sub

Damon
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,168,091
Messages
5,857,303
Members
431,870
Latest member
Muratculous

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
Top