May not be the best way to do this but this code works
Sub saveif()
If Range("A1") = Range("B3") Then
ActiveWorkbook.Save
End If
End Sub
HTH
Matt
I want to save my excel sheet according to a condition like A1 = B3. If the condition is false, sheet could not be saved.
How can I do this? Is it possible?
Thank you for your concern,Matt.
If I want my sheet not to be saved from excel's file-->save or ctrl-->s
unless a condition is true,
what should I do?
I compile this source. May be it will be useful for others.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim wSht As Worksheet
Dim cel_Warning, cel_Cond1, cel_Cond2 As Range
Dim allwShts As Sheets
Dim cel As Range
Dim i As Integer
Cancel = False
Set allwShts = Worksheets
For Each wSht In allwShts
Set cel_Warning = wSht.Range("IT2")
Set cel_Cond1 = wSht.Range("IU2")
Set cel_Cond2 = wSht.Range("IV2")
For i = 1 To 20
If Not IsEmpty(cel_Warning(i)) Then
If Not cel_Cond1(i).Value = cel_Cond2(i).Value Then
a = MsgBox(wSht.Name + Chr(13) + cel_Warning(i))
Cancel = True
End If
End If
Next i
Next wSht
If Cancel = True Then
a = MsgBox("File will not be saved!")
End If
End Sub
Like this thread? Share it with others