Here is your code with some cleaning and modification.
I have no way of testing it, as I don't know the values in the
various cells that you use to construct the path and file name.
Note that I deleted the declaration for "filename", as that is the
name of the argument in the SaveAs code and should not be declared.
"Cancel = True" was added in a couple of places, to prevent
Excel from saving the workbook upon exit.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
On Error GoTo ErrHandler
Dim i As Integer
Dim j As Integer
Dim cell As Excel.Range
Dim strPath As String
j = 0
'find # of colored cells:
For Each cell In Sheets(1).Range("a1:j55").Cells
If cell.Interior.ColorIndex = 19 Then
j = j + 1
End If
Next cell
If j = 0 Then
Sheets(1).Range("c5").Select
MsgBox "To print a blank form please use the Blank Form button."
Cancel = True
Exit Sub
End If
i = 0
'find # of colored cells that are empty:
' lots of merged cells in this Selection
For Each cell In Sheets(1).Range("a1:j55")
If cell.Interior.ColorIndex = 19 Then
If cell.Value = "" Then
i = i + 1
End If
End If
Next cell
If i 0 Then
MsgBox "Please fill out all the mandatory fields which are colored in yellow."
Cancel = True
Exit Sub
Else
On Error Resume Next
MkDir "C:\ABCD"
MkDir "C:\ABCD\site" & Sheets(1).Range("c5").Value
On Error GoTo ErrHandler
Application.EnableEvents = False
'clear the cells that shouldnt have anything in them:
For Each cell In Sheets(1).Range _
("d22,f22,h22,j22,d26,i26,g27,h28,d32,h32,c36,d37, h37,f38,b41:b45").Cells
If cell.Interior.ColorIndex < 19 Then
cell.Value = vbNullString
Range("c5").Select
End If
Next cell
'save the file :
strPath = Sheets(2).Range("h1").Value & Sheets(2).Range("e1").Value & ".xls"
If (Dir(strPath) = "") Then
ThisWorkbook.SaveAs Filename:=strPath
MsgBox Sheets(2).Range("e1").Value & "'s file has been saved to" & _
Sheets(2).Range("g1").Value
Cancel = True
Else
ThisWorkbook.Save
Cancel = True
End If
End If
ErrHandler:
Application.EnableEvents = True
End Sub
'-------------
"Jim Cone"
wrote in message
Sorry, I spoke before I thought.
The "()" in MkDir doesn't affect whether it works or not
Jim Cone
San Francisco, USA
"Jim Cone"
wrote in message
For starters, remove the ( ) in the MkDir statements...
MkDir "C:\ABCD"
MkDir "C:\ABCD\site" & Sheets(1).Range("c5").Value
Also, do not use On Error Resume Next when you are
developing code as it hides your mistakes.
(as you have discovered)
It appears that you create the directory every time the code runs.
You need to check if the directory exists and if not run the code.
For what's it's worth:
peace - absence of war
piece - part of something.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware