Excel VBA help

V

vmc

I am needing some help with this vba code. What I am trying to do is first
verify if cell A4 is not empty, if it is not empty then I need it to make
sure there is data in the other columns before anyone can save. This is what
I got so far but it is saving and not bring the popup message that data is
missing.


Private Sub Workbook_BeforeSave(ByVal SaveAsUi As Boolean, Cancel As Boolean)

If IsEmpty(A4) = False Then 'make sure that A4 is not empty prior to running
the below if


If Worksheets("User's Sheet").Range("B4").Value = "" Then
msg = "Program Short Name must be filled in before saving."
Cancel = True
End If

'and

If Worksheets("User's Sheet").Range("F4").Value = "" Then
msg = "Gender must be filled in before saving."
Cancel = True
End If

'and

If Worksheets("User's Sheet").Range("G4").Value = "" Then
msg = "Color Name must be filled in before saving."
Cancel = True
End If

'and


If Worksheets("User's Sheet").Range("H4").Value = "" Then
msg = "Size must be filled in before saving."
Cancel = True
End If

'and

If Cancel Then
MsgBox msg
End If




End If
End Sub
 
M

Mike H

A slightly different approach

Private Sub Workbook_BeforeSave(ByVal SaveAsUi As Boolean, Cancel As Boolean)
Dim msg(6)
Dim c As Range
dim x as long
Set sht = Sheets("User's Sheet")
msg(1) = "Cannor save A4 must be filled in"
msg(2) = "Program Short Name must be filled in before saving."
msg(3) = "Gender must be filled in before saving."
msg(4) = "Color Name must be filled in before saving."
msg(5) = "Size must be filled in before saving."
x = 1
For Each c In sht.Range("A4,b4,f4,G4,H4")
If IsEmpty(c) Then
MsgBox msg(x) & " " & c.Address
Cancel = True
Exit Sub
End If
x = x + 1
Next
End Sub
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.
 

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