View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
sheela sheela is offline
external usenet poster
 
Posts: 43
Default If..Else Statement in Excel

Bob,

Can I use Select Case instead of If..Else statement? Is
Select Case is used only when we select the options that
we code or can it be done similarly to the if..else
statement whereby it calculates based on the input keyed
in?

I hope I posted the questions correctly.

thanks,

Sheela
-----Original Message-----
Sheela,

The code you posted is syntactically correct (meaning it

is written
according to the rules of VB). I cannot tell if the

logic is correct because
I don't know the meaning of what you are trying to do.

If you are still
having problems with this, I think you should focus on

getting something
running, even if it is not totally correct, then step

thru the code,
checking values against your assumptions at each step.

Some observations:

First, you are using Value in some places and Text in

others. They are
equivalent, so to simplify, just use Text everywhere.

Second, you are using math operations on text which can

have unforeseen
consequences. ***Note that strings that do not "look"

like numbers to VB are
interpreted as zero.*** Strings sometimes get

concatenated (get stuck
together end to end) and become an entirely unexpected

number, instead of
being mathematically added together.

Ideally, all text values (which are a String data type)

should be changed to
a suitable numeric type (Integer, Single, Double, Long)

when you do math
operations with them and changed back to strings when

assigned to a textbox
Text property. Let's assume for the time being that VB

is handling this
correctly for you, which indeed it tries to do. Do keep

this
math-operations-with-text idea in the back of your mind

as a potential
source of failure.

Third, this snippet of code occurs in your if-ElseIf

block three times:

If Len(FirmStandbyCharge) < 1 Then
txtFirmStandbyCharge.Text = "0"
Else
txtFirmStandbyCharge.Text = Format

(FirmStandbyCharge, "######.##")
End If

Create a separate subroutine. Move this code there. Call

the subroutine from
your If-ElseIf code in place of the code removed. This

will simplify the
If-ElseIf code which will make it easier to troubleshoot.

I will post separately a modified version of your if-

block that illustrates
the separate subroutine idea. I think your objective

should be to get the
code simplified and syntactically correct so that it

runs without complaint,
then step thru the code, examining values at each step

to see if they are
what you expect.

Bob Kilmer


"Sheela" wrote in message
...
Is this structure correct. I've made some changes and
followed like what you suggested but the results is

still
not displaying on the screen.
---------------------------------------------------
If txtDmdPeak.Text <= txtDtotal.Text Then
If txtCalculatedDemand.Text < txtDsbf.Text Then
txtFirmStandbyCharge.Text = (txtDsbf.Value -
txtCalculatedDemand.Value) * txtMaxDmcFirm.Value
If txtFirmStandbyCharge.Value = "" Then
txtFirmStandbyCharge.Value = "0"
Else
txtFirmStandbyCharge.Value = Format
(txtFirmStandbyCharge.Value, "######.##")
End If
End If


ElseIf txtDsbf.Text < txtCalculatedDemand.Text Then
If txtCalculatedDemand.Text < txtDtotal.Text

Then
txtFirmStandbyCharge.Text =

(txtDtotal.Value -
txtCalculatedDemand.Value) * txtMaxDmcNonFirm.Value
If txtFirmStandbyCharge.Text = "" Then
txtFirmStandbyCharge.Text = "0"
Else
txtFirmStandbyCharge.Value = Format
(txtFirmStandbyCharge.Value, "######.##")
End If
End If

ElseIf txtCalculatedDemand.Text txtDtotal.Text Then
txtFirmStandbyCharge.Text =
txtCalculatedDemandCharge.Value + txtEmp.Value +
txtEmop.Value
If txtFirmStandbyCharge.Text = "" Then
txtFirmStandbyCharge.Text = "0"
Else
txtFirmStandbyCharge.Value = Format
(txtFirmStandbyCharge.Value, "######.##")
End If
Else
txtFirmStandbyCharge.Value = "0"
End If
-----------------------------------------------------
Please help me. I'm really confused. Thank you very

much
for your help.

Regards,
Sheela

<snip


.