Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with number comparisons 1

Status
Not open for further replies.

bmc1234

Programmer
Jul 21, 2005
50
US
I am using some VBA in excel and I have a simple group of if/else if statements that isn't behaving properly. Here is my code:

Code:
insertionFee = 0
    If startPrice <= 0.99 Then
        insertionFee = 0.2
    ElseIf startPrice >= 1 & startPrice <= 9.99 Then
        insertionFee = 0.35
    ElseIf startPrice >= 10 & startPrice <= 24.99 Then
        insertionFee = 0.6
    ElseIf startPrice >= 25 & startPrice <= 49.99 Then
        insertionFee = 1.2
    ElseIf startPrice >= 50 & startPrice <= 199.99 Then
        insertionFee = 2.4
    ElseIf startPrice >= 200 & startPrice <= 499.99 Then
        insertionFee = 3.6
    Else
        insertionFee = 4.8
    End If

insertionFee and startPrice are both declared as Doubles and startPrice is set previously through an inputbox. When a high value such as 150 is entered, startPrice is set to .35 instead of the proper value. What can possibly be the cause of this? Thanks.
 
bmc1234,
To concatenate, or not to concatenate, that is the question.
[tt]ElseIf startPrice >= 1 [red]&[/red] AND startPrice <= 9.99 Then[/tt]

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
... [red]startPrice[/red] is set to .35

I assume that you mean

... [red]insertionFee[/red] is set to .35

It would appear that your supplied start price of 150 is being interpreted as 1.50 and, for that value, 0.35 is the correct insertionFee value.

Could something be modifying the value of startprice after the input but before this IF-code is processed?
 
bmc1234,

I suggest you step through your code in debug mode and examine the flow/ variable values as execution progresses.

I expect you code can be shortened as follows;

insertionFee = 0
If startPrice <= 0.99 Then
insertionFee = 0.2
ElseIf startPrice <= 9.99 Then
insertionFee = 0.35
ElseIf startPrice <= 24.99 Then
insertionFee = 0.6
ElseIf startPrice <= 49.99 Then
insertionFee = 1.2
ElseIf startPrice <= 199.99 Then
insertionFee = 2.4
ElseIf startPrice <= 499.99 Then
insertionFee = 3.6
Else
insertionFee = 4.8
End If

regards Hugh,
 
CautionMP: Thanks, that little change made the difference. I started by just using statements like 1<= startPrice <=9.99 and that didn't work. So then I changed it to the code that I posted earlier and that didn't help but replacing & with 'And' seems to work. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top