View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Norman Jones Norman Jones is offline
external usenet poster
 
Posts: 5,302
Default How to avoid saving over an excisting Workbook.?.?

Hi Ryan,

What I want is a macro that asks...

If the value in cell B5 is the same as any of the file names in the folder
with the workbooks that were saved as the previous values in B5 Then give
me
a warning that the value in B5 has been used so that I do not accidently
save
over one of the saved workbooks.


This is normal behaviour.

Unless alert messages have been disabled by means of an

Application.DisplayAlerts = False

instruction, any attempt to save to an existing file name will engender a
confirmatory warning.

If you want to check pre-emptively, then try something like:

'=========================
Public Sub Tester()
Dim sh As Worksheet
Dim sStr As String

Set sh = ActiveSheet

sStr = Application.DefaultFilePath & "\" '"YourFolderPath" & "\"

If Dir(sStr & sh.Range("B5").Value) < "" Then
MsgBox "file exists"
' A workbook already exists with this name
' so do something
Else
'Workbook does not exist, OK to save!
MsgBox "Workbook does not exist"
End If

End Sub

'<<=========================


---
Regards,
Norman



"ryanmhess" wrote in message
...
I am not versed in VBA.

Right now I have a macro that saves 1 worksheet as a new workbook and
saves
as a value in cell B5. Everytime I change the value of B5 and run the
macro
I get a new workbook.

What I want is a macro that asks...

If the value in cell B5 is the same as any of the file names in the folder
with the workbooks that were saved as the previous values in B5 Then give
me
a warning that the value in B5 has been used so that I do not accidently
save
over one of the saved workbooks.