PDA

View Full Version : Number Comparison



RECrerar
08-16-2007, 03:48 AM
Hi, i have the following code, used in userform that should check that the value Entered in the "Elem From" text box is smaller than the number entered in the "Elem To" text box, and shows an error message and clears the entries if these conditions are not met.

This works for most values, However is I enter for example 95001 in the 'Elem From' box and 100000 in the 'Elem To' box the error message is displayed and the values removed. 100000 is greater than 95001 so why does this happen and how do I stop it happening?

If ElemFrom.Value > ElemTo.Value Then
Msg = "Please re-enter the Element Range" & vbNewLine & vbNewLine
Msg = Msg & "The Element Range must be FROM Low TO High Element ID"
Error = MsgBox(Msg, , "Invalid Data Entry")

ElemFrom.Text = ""
ElemTo.Text = ""

ElemFrom.SetFocus

Exit Sub
End If

Bob Phillips
08-16-2007, 04:20 AM
If CDbl(ElemFrom.Text) > CDbl(ElemTo.Text) Then

RECrerar
08-16-2007, 04:53 AM
Thanks xld, works perfectly now

mdmackillop
08-16-2007, 05:31 AM
BTW the problem arises because you are comparing numbers as text, where 10 comes before 9. XLD's code converts the text to numerical values.

RECrerar
08-16-2007, 07:09 AM
Thanks for clarifying that. I assumed it was something to do with 1 being lower than 9 and it not looking at the whole number. Did mean to actually ask what the code actually did, but now I don' have to, so thank-you.

By the way. Can you recomend any good books/resources for VBA. At the moment I only have Excel VBA Programming for Dummies and although it was perfect the first couple of weeks, I've got to the point where it can't answer my questions, hence me constantly being on here bugging you guys!

Bob Phillips
08-16-2007, 07:21 AM
VBA Programmers Reference Manual by Bullen, Bovey, Green, Rosenfeld. DON'T buy the 2003 version, that suffers from a bed edi, get the 2002 versions, or even 2007 if you have Excel 2007.

RECrerar
08-16-2007, 07:57 AM
Cool, well that is now on its way from Amazon, wonder if I can claim it on expenses?!

unmarkedhelicopter
08-16-2007, 09:32 AM
VBA Programmers Reference Manual by Bullen, Bovey, Green, Rosenfeld. DON'T buy the 2003 version, that suffers from a bed edi, get the 2002 versions, or even 2007 if you have Excel 2007.

What's a "bed edi" ?

Bob Phillips
08-16-2007, 10:14 AM
Read it, and you will see.