IF THEN ELSE statement: Need to STOP a Macro if something happens

M

Mike Cross

Problem:

I have a current Macro that does a goal seek. However,
if any of 4 cells is blank or 0 then the Macro crashes.

I am trying to write code that will stop the Macro if ANY
of the four cells has a 0 or a blank value. Otherwise,
run the Macro. I think it will take a series of IF/THEN
ELSE statements but I am not proficient at writing them.
Any ideas?

Current Macro:
Application.ScreenUpdating = False
Sheets("Cash Flow").Select
Range("D49").Select
Range("D49").GoalSeek Goal:=1, ChangingCell:=Sheets
("Program Input").Range( _
"J192")
Sheets("Program Input").Select
Range("J192").Select
Application.ScreenUpdating = True
End Sub

Thanks in advance for your response!

Regards,

Mike
 
D

David Benson

Mike,

You don't say which 4 cells your need to test. Assuming that the cells are
A1:D1, you could write single IF/THEN statement such as:

If Range("A1") = 0 Or Range("B1") = 0 Or Range("C1") = Or Range("D1") =
0 then Exit Sub

Just substitute the references for the real cells of interest.

-- David
 
T

Teggaman

Mike, pretend your cells are a1,a2,a3,a4

Sub teg()

If IsEmpty(Range("a1")) = True Or Range("a1") = 0 Then
GoTo er_msg
ElseIf IsEmpty(Range("a2")) = True Or Range("a2") = 0 Then
GoTo er_msg
ElseIf IsEmpty(Range("a3")) = True Or Range("a3") = 0 Then
GoTo er_msg
ElseIf IsEmpty(Range("a4")) = True Or Range("a4") = 0 Then
GoTo er_msg
End If

'Goalseek code

'End of goal seek code

Exit Sub


er_msg:
MsgBox ("error, zero vals exist")
End Sub
 
M

mike cross

David:

Thank you so much! I was having troubles writing if/then
statements for some reason. IT WORKS GREAT!

Mike
 

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