Prevent overwriting a file

P

Patrick C. Simonds

The code below is triggered by a command1 button on on UserForm1. It saves
the document with a name derived by combining data in certain cells. My
problem is if that document name already exists. I do not want the user to
have the option of replacing the original file or renaming the current
file. I want a MsgBox which tells them that the file already exists. Once
the MsgBox is dismissed I want UserForm1 to reappear so they can select a
new date.


Private Sub CommandButton1_Click()
Dim myRange As Range
Dim myDate As Range
Set myRange = Worksheets("Relief Board").Range("C3")
Set myDate = Worksheets("Relief Board").Range("C4")

Protection.unprotect_all_sheets

myRange.Value = ""
myDate.Value = Calendar1.Value

Protection.protect_all_sheets

ActiveWorkbook.SaveAs Filename:="P:\AA Exception\ " & Worksheets("Relief
Board").[B3].Value & ", " & Worksheets("Relief Board").[D3].Value &
"_Exception Sheet", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, Password:="",
WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

Unload StartNewDay

End Sub
 
L

Leith Ross

Patrick said:
The code below is triggered by a command1 button on on UserForm1. I
save
the document with a name derived by combining data in certain cells.
M
problem is if that document name already exists. I do not want the use
t
have the option of replacing the original file or renaming the curren
file. I want a MsgBox which tells them that the file already exists
Onc
the MsgBox is dismissed I want UserForm1 to reappear so they can selec

new date
Code
-------------------Private Sub CommandButton1_Click(
Dim myRange As Rang
Dim myDate As Rang
Set myRange = Worksheets("Relief Board").Range("C3"
Set myDate = Worksheets("Relief Board").Range("C4"

Protection.unprotect_all_sheet

myRange.Value = "
myDate.Value = Calendar1.Valu

Protection.protect_all_sheet

ActiveWorkbook.SaveAs Filename:="P:\AA Exception\ " & Worksheets("Relie
Board").[B3].Value & ", " & Worksheets("Relief Board").[D3].Value
"_Exception Sheet",
FileFormat:=xlOpenXMLWorkbookMacroEnabled, Password:=""
WriteResPassword:="",
ReadOnlyRecommended:=False, CreateBackup:=Fals

Unload StartNewDa

End Su
-------------------

Hello Patrick

You can use the Dir statement to test if a file exists. You can eve
use wild card characters to match similar names.

Code
-------------------

FileName = Dir("P:\AA Exception\MyFile.xls"

'If the file doesn't exist, FileName will be an empty string ""
If FileName <> "" The
MsgBox "File Exists. Please Use a Different Name.
UserForm1.Sho
End I

-------------------

--
Leith Ros

Sincerely,
Leith Ross

'The Code Cage' (http://www.thecodecage.com/
 
P

Patrick C. Simonds

I have to admit, I do not even know where to begin to integrate this into my
current code.


Leith Ross said:
The code below is triggered by a command1 button on on UserForm1. It
saves
the document with a name derived by combining data in certain cells.
My
problem is if that document name already exists. I do not want the user
to
have the option of replacing the original file or renaming the current
file. I want a MsgBox which tells them that the file already exists.
Once
the MsgBox is dismissed I want UserForm1 to reappear so they can select
a
new date.
Code:
--------------------Private Sub CommandButton1_Click()
Dim myRange As Range
Dim myDate As Range
Set myRange = Worksheets("Relief Board").Range("C3")
Set myDate = Worksheets("Relief Board").Range("C4")

Protection.unprotect_all_sheets

myRange.Value = ""
myDate.Value = Calendar1.Value

Protection.protect_all_sheets

ActiveWorkbook.SaveAs Filename:="P:\AA Exception\ " & Worksheets("Relief
Board").[B3].Value & ", " & Worksheets("Relief Board").[D3].Value &
"_Exception Sheet", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, Password:="",
WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

Unload StartNewDay

End Sub
--------------------

Hello Patrick,

You can use the Dir statement to test if a file exists. You can even
use wild card characters to match similar names.

Code:
--------------------

FileName = Dir("P:\AA Exception\MyFile.xls")

'If the file doesn't exist, FileName will be an empty string "".
If FileName <> "" Then
MsgBox "File Exists. Please Use a Different Name."
UserForm1.Show
End If

--------------------


--
Leith Ross

Sincerely,
Leith Ross

'The Code Cage' (http://www.thecodecage.com/)
 
L

Leith Ross

Patrick said:
I have to admit, I do not even know where to begin to integrate thi
into m
current code


Patrick C. Simonds;205378 Wrote
The code below is triggered by a command1 button on on UserForm1. I
save
the document with a name derived by combining data in certain cells
M
problem is if that document name already exists. I do not want th use
t
have the option of replacing the original file or renaming th curren
file. I want a MsgBox which tells them that the file already exists
Onc
the MsgBox is dismissed I want UserForm1 to reappear so they ca selec

new date Code
-------------------

Private Sub CommandButton1_Click(
Dim myRange As Rang
Dim myDate As Rang
Set myRange = Worksheets("Relief Board").Range("C3"
Set myDate = Worksheets("Relief Board").Range("C4"
Protection.unprotect_all_sheet

myRange.Value = "
myDate.Value = Calendar1.Valu
Protection.protect_all_sheet

ActiveWorkbook.SaveAs Filename:="P:AA Exception " Worksheets("Relie
Board").[B3].Value & ", " & Worksheets("Relief Board").[D3].Valu
"_Exception Sheet",
FileFormat:=xlOpenXMLWorkbookMacroEnabled, Password:=""
WriteResPassword:="",
ReadOnlyRecommended:=False, CreateBackup:=Fals
Unload StartNewDa
End Su
-------------------

Hello Patrick
You can use the Dir statement to test if a file exists. You can eve
use wild card characters to match similar names

FileName = Dir("P:AA ExceptionMyFile.xls"
'If the file doesn't exist, FileName will be an empty string ""
If FileName <> "" The
MsgBox "File Exists. Please Use a Different Name.
UserForm1.Sho
End I


-
Leith Ros
Sincerely
Leith Ros
'The Code Cage' ('The Code Cage' (http://www.thecodecage.com/)
(http://www.thecodecage.com/forumz/showthread.php?t=56428

Hello Patrick

I added the code for you. Try it out and let me know if there ar
problems

Code
-------------------

Private Sub CommandButton1_Click(

Dim FileName As Strin
Dim myRange As Rang
Dim myDate As Rang

Set myRange = Worksheets("Relief Board").Range("C3"
Set myDate = Worksheets("Relief Board").Range("C4"

Protection.unprotect_all_sheet
myRange.Value = "
myDate.Value = Calendar1.Valu
Protection.protect_all_sheet

CHeckFileName
FileName = "P:\AA Exception\ "
& Worksheets("ReliefBoard").[B3].Value
& ", " & Worksheets("Relief Board").[D3].Value
& "_Exception Sheet

'If the file doesn't exist, FileName will be an empty string ""
If Dir(FileName) <> "" The
MsgBox "File Exists. Please Use a Different Name.
UserForm1.Sho
GoTo CheckFileNam
End I

ActiveWorkbook.SaveAs FileName:=FileName,
FileFormat:=xlOpenXMLWorkbookMacroEnabled,
Password:="",
WriteResPassword:="",
ReadOnlyRecommended:=False,
CreateBackup:=Fals

Unload StartNewDa

End Su

-------------------

--
Leith Ros

Sincerely,
Leith Ross

'The Code Cage' (http://www.thecodecage.com/
 
P

Patrick C. Simonds

Thank you

I get a Subscript out of range error with the code below highlighted. I
thought the problem might be that the type of file was not indicated (in
this case xlsm) so I tried adding before the last quote mark but I still got
the same error.


FileName = "P:\AA Exception\ " _
& Worksheets("ReliefBoard").[B3].Value _
& ", " & Worksheets("Relief Board").[D3].Value _
& "_Exception Sheet"
 
L

Leith Ross

Patrick said:
Thank you

I get a Subscript out of range error with the code below highlighted
I
thought the problem might be that the type of file was not indicate
(in
this case xlsm) so I tried adding before the last quote mark but
still got
the same error.


FileName = "P:\AA Exception\ " _
& Worksheets("ReliefBoard").[B3].Value _
& ", " & Worksheets("Relief Board").[D3].Value _
& "_Exception Sheet"

Hello Patrick,

The only subscripts would be cells. Everything else is a fixed string
Do the cells have the correct values? Are all the names of th
worksheets spelled correctly? If all that checks out OK, I really don'
know what the problem would be. I don't have Excel 2007 so I can't ru
it even if you posted the workbook


--
Leith Ros

Sincerely,
Leith Ross

'The Code Cage' (http://www.thecodecage.com/
 
P

Patrick C. Simonds

I am sorry to have wasted your time on that last question. I have been so
wrapped up in this project I totally missed that ReliefBoard should have
been Relief Board (two words).


Leith Ross said:
Thank you

I get a Subscript out of range error with the code below highlighted.
I
thought the problem might be that the type of file was not indicated
(in
this case xlsm) so I tried adding before the last quote mark but I
still got
the same error.


FileName = "P:\AA Exception\ " _
& Worksheets("ReliefBoard").[B3].Value _
& ", " & Worksheets("Relief Board").[D3].Value _
& "_Exception Sheet"

Hello Patrick,

The only subscripts would be cells. Everything else is a fixed string.
Do the cells have the correct values? Are all the names of the
worksheets spelled correctly? If all that checks out OK, I really don't
know what the problem would be. I don't have Excel 2007 so I can't run
it even if you posted the workbook
.


--
Leith Ross

Sincerely,
Leith Ross

'The Code Cage' (http://www.thecodecage.com/)
 
L

Leith Ross

Patrick said:
I am sorry to have wasted your time on that last question. I have bee
so
wrapped up in this project I totally missed that ReliefBoard shoul
have
been Relief Board (two words).


Leith Ross said:
Thank you

I get a Subscript out of range error with the code belo highlighted.
I
thought the problem might be that the type of file was no indicated
(in
this case xlsm) so I tried adding before the last quote mark but I
still got
the same error.


FileName = "P:AA Exception " _
& Worksheets("ReliefBoard").[B3].Value _
& ", " & Worksheets("Relief Board").[D3].Value _
& "_Exception Sheet"

Hello Patrick,

The only subscripts would be cells. Everything else is a fixe string.
Do the cells have the correct values? Are all the names of the
worksheets spelled correctly? If all that checks out OK, I reall don't
know what the problem would be. I don't have Excel 2007 so I can' run
it even if you posted the workbook
.


--
Leith Ross

Sincerely,
Leith Ross

'The Code Cage' ('The Code Cage' (http://www.thecodecage.com/))

Hello Patrick,

I misspell words a lot when coding. My typing skills are my weakness
Did that solve the problem

--
Leith Ros

Sincerely,
Leith Ross

'The Code Cage' (http://www.thecodecage.com/
 
P

Patrick C. Simonds

Yes it did. Thank you very much!



Leith Ross said:
I am sorry to have wasted your time on that last question. I have been
so
wrapped up in this project I totally missed that ReliefBoard should
have
been Relief Board (two words).


Leith Ross said:
Patrick C. Simonds;205750 Wrote:
Thank you

I get a Subscript out of range error with the code below highlighted.
I
thought the problem might be that the type of file was not indicated
(in
this case xlsm) so I tried adding before the last quote mark but I
still got
the same error.


FileName = "P:AA Exception " _
& Worksheets("ReliefBoard").[B3].Value _
& ", " & Worksheets("Relief Board").[D3].Value _
& "_Exception Sheet"

Hello Patrick,

The only subscripts would be cells. Everything else is a fixed string.
Do the cells have the correct values? Are all the names of the
worksheets spelled correctly? If all that checks out OK, I really don't
know what the problem would be. I don't have Excel 2007 so I can't run
it even if you posted the workbook
.


--
Leith Ross

Sincerely,
Leith Ross

'The Code Cage' ('The Code Cage' (http://www.thecodecage.com/))
------------------------------------------------------------------------
Leith Ross's Profile:
'The Code Cage Forums - View Profile: Leith Ross' (http://www.thecodecage.com/forumz/members/leith-ross.html)
View this thread: 'Prevent overwriting a file - The Code Cage Forums' (http://www.thecodecage.com/forumz/showthread.php?t=56428)

Hello Patrick,

I misspell words a lot when coding. My typing skills are my weakness,
Did that solve the problem?


--
Leith Ross

Sincerely,
Leith Ross

'The Code Cage' (http://www.thecodecage.com/)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top