check file in folder and overwrite

rakesh seebaruth

Active Member
Joined
Oct 6, 2011
Messages
303
Hi,

I have the following code

Sub saveas()
mydrive = "C:"
mydir = "excel"
myname = Sheets("sheet1").Range("e5")'
ms = mydrive & "\" & mydir & "\" & myname & ".xls"
Application.DisplayAlerts = False
ActiveWorkbook.SaveCopyAs Filename:=ms
Application.DisplayAlerts = True
End Sub


I want vba to check in C:excel if file("e5") exists .If exists overwrite,and protect with password ,and save it.

If does not exists then save it as save as .

I'm using excel 2003

thnaks
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hello

Consider using:

Code:
Sub saveas()
    ms = "C:\excel\" & Sheets("sheet1").Range("E5").Text & ".xls"
    If Len(Dir(ms)) Then Kill ms
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveCopyAs Filename:=ms
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
You can use the .Protect method on a workbook, like ActiveWorkbook.
 
Upvote 0
Re:runtime error

Hi wigi,

Thanks for your post. i refer to your first post.

when i save a new file(example abc.xls) , it's ok.

When i made changes in "abc.xls" and save it.It is supposed to kill it but i get runtime error 70.permission denied

please help
 
Upvote 0
Re: runtime error

Hi wigi,

Thanks for your post. i refer to your first post.

when i save a new file(example abc.xls) , it's ok.

When i made changes in "abc.xls" and save it.It is supposed to kill it but i get runtime error 70.permission denied

please help

Hi

So, basically, you are trying to kill (delete) a file while the file is opened? That will not be possible. You could save a copy from that file to another location and/or filename.
 
Upvote 0
Hi there,

Thanks for your reply.

If i close the file ,delete it and save it

is it going to work.

let's say :-
If Len(Dir(ms)) Then close file , kill and save)my code is not good of course.

please help
 
Upvote 0

Forum statistics

Threads
1,224,884
Messages
6,181,571
Members
453,054
Latest member
arz007

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