If Else...Good programming practice

M

Michelle Hillard

Hi guys,
I just wanted to know what is good programming practice with the If Else
statement.
I have the following code which works, but I have no Else statement if the
"y" does not exist. Should I put something in the Else part if "y" does not
exist, even though this code works?

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 4 Then
ThisRow = Target.Row
If Target.Value = "y" Then
Application.Run "Book1.xls!MoveRowsWithYinColumnD"
Else
End If
End If
End Sub

Thanks in advance


--


---------------------------------------------------------------------
"Are you still wasting your time with spam?...
There is a solution!"

Protected by GIANT Company's Spam Inspector
The most powerful anti-spam software available.
http://mail.spaminspector.com
 
C

Chip Pearson

Michelle,

Unless you need to execute some specific action if the target's
value does not equal 'y', you can remove the else part entirely.

If Target.Column = 4 Then
ThisRow = Target.Row
If Target.Value = "y" Then
Application.Run "Book1.xls!MoveRowsWithYinColumnD"
End If
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
M

Michelle Hillard

Thanks Chip,
I had a look at your web site and Im astounded by you and other MVPs (and
others) on here with the knowledge you guys know.
Thanks again.

--


---------------------------------------------------------------------
"Are you still wasting your time with spam?...
There is a solution!"

Protected by GIANT Company's Spam Inspector
The most powerful anti-spam software available.
http://mail.spaminspector.com
 
M

Michelle Hillard

Thanks Brian.

--


---------------------------------------------------------------------
"Are you still wasting your time with spam?...
There is a solution!"

Protected by GIANT Company's Spam Inspector
The most powerful anti-spam software available.
http://mail.spaminspector.com
 
T

Tushar Mehta

As Chip has already pointed out, there is no need for the Else part
unless you intend putting code there.

The question is when should there be code in the Else part? In some
cases -- and this is a judgement call -- I will have a final 'Else'
clause for an integrity check. Very few people do this on grounds on
performance. I am willing to waste a few bytes and a few processor
cycles to avert the once-in-a-blue-moon catastrophe.

Suppose in your case that the values in column D can by Y or N. I
would consider something along the lines of

If Target.Value="Y" Then
...
ElseIf Target.Value = "N" Then
Else
Abort processing with critical data integrity error
End if

Most people laugh at such a check or when I check gender against M, F,
and terminate processing it is neither.

On multiple occasions this kind of verification (not necessarily a
gender check but conceptually the same) has proven its worth. Two
significant instances that come to mind involved a regional bank in
Australia and a Fortune 50 company in the US. I trapped upstream
programming errors that saved each company millions in direct costs,
not to mention embarrassing front page coverage in the national media.

--
Regards,

Tushar Mehta, MS MVP -- Excel
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions
 

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