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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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,416
Messages
6,178,513
Members
452,853
Latest member
philipnjk64

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