Greater than/Less than logic not working?

User Name

Spammer
Joined
Aug 10, 2010
Messages
182
The following section of code has been giving me some problems. The Rsh calculation and Rshminx calculation work fine but the greater than/less than logic isn't being followed when Rsh is compared to Rshminx.

I noticed that Rshminx wasn't declared at all so I declared it and am noticing a significant change. Question is: if you don't have the variable declared will this screw up the greater than/less than comparison?

Hmmm... Rsh was declared as string as well... Could be another part of the problem.

HTML:
Rsh = ((Isc * Rs - Voc) * (e ^ ExponentOC - e ^ ExponentMP) + (Voc - Vmp - Imxp * Rs) * (e ^ ExponentOC - e ^ ExponentSC)) / (Imxp * (e ^ ExponentOC - e ^ ExponentSC) + Isc * _
        (e ^ ExponentMP - e ^ ExponentOC))

Rshminx = (Vmp / (Isc - Imxp))
        
If Rsh < Rshminx Then
CountRsh = CountRsh + 1
GoTo Step0
End If
 
Last edited:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
What type of variable did you declare Rshminx as? That could make a difference, as you could lose decimal places in the stored value.

Try putting:

Debug.print Rshminx before your IF Rsh < Rshminx line and pulling up the immediate window to see what the value of that variable is...
 
Upvote 0
Rshminx is declared as Double...

I'm seeing similar problems in other parts of my code when Rshminx is compared to undeclared variables. I thought undeclared variables default to double but something strange is going on.

I had intended for my algorithm to throw away Rsh values if they were less than the Rshminx value. Funny thing is I'm getting outputs for Rsh that are actually negative even though Rshminx is always positive. The silver lining here is that I'm comparing my results to the results of another algorithm I'm trying to imitate and they happen to get negative Rsh values in similar places - Had I properly declared by variables I wouldn't have found this out.

By the way... I'm not getting any errors/debug pop ups. I'm just running into an issue with the greater than/less than logic not being followed. I've actually watched in debug mode as two values were compared and the wrong choice was made between the options. I suspect this has to do with how I've declared my variables and I just wanted to confirm if this could be a possibility.
 
Last edited:
Upvote 0
The relative numbers were in the neighborhood of Rshminx being around 88.534673 with Rsh being 111.58657. I'd get a fail rather than a pass when these values came up to the argument. As I said, even negative Rsh values were passing through the logic - this shouldn't be possible but if happened just the same.

If Rsh<Rshminx
GoTo stepx
End if

Again, Rsh was declared as string with Rshminx declared as double and I stepped through the logic in debug mode only to see it fail. I've since changed Rsh to double and gone in a different direction with the code making this question a curiosity rather than a critical concern. I was thinking the source of the problem was the string vs. double thing but the replies here indicate I probably need to look around my code for another issue.
 
Upvote 0
Obviously best to use the right data type for your variables, but I'm not sure it would cause your issue in this case.

Consider that this returns "False":

Code:
Sub test()
Dim my_text As String, my_dbl As Double

my_text = 88.534673

my_dbl = 111.58657

MsgBox my_text > my_dbl

End Sub

If you step through the code, you'll see my_text store the value "88.534673".

In a spreadsheet "88.534673">111.58657 returns TRUE. In VBA, it returns FALSE.
 
Upvote 0
I agree that should be how VBA treats the logic but I'm saying I watched VBA pass a false argument as true. As in:

If 88.xyz > 111.xyz then
Goto stepx
End if

My code was jumping to stepx you see. You're saying that assigning string vs double to the variables shouldn't cause this. If that's the case then something else was at the root of the issue. Maybe I didn't see what I thought I say. Thank you for your help.
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top