I feel like this is a really obvious bug but I really can't figure it out. It seems as if the comparison operators in VBScript aren't working properly. I'm writing a program in which the user enters an order amount, and this amount is compared to 1000. If I enter 200 for the order amt, the program outputs that 200 > 1000 = true.
Here is the code for my program:
largeOrderAmt = 1000
orderAmt = Inputbox("What is the order amount?")
Document.write(orderAmt & largeOrderAmt & (orderAmt > largeOrderAmt))
'Calculations
If orderAmt <= largeOrderAmt then
Document.write ("if statement called <br>")
...
Else
Document.write ("else statement called <br>")
EndIf
and here is the output:
200 1000True else statement called
I really don't get it. This is extremely frustrating. Any help would be appreciated, thank you!
That's because the values are compared as strings:
"200" > "1000" = True
Convert your input into a number using CInt or CDbl, e.g.:
orderAmt = CDbl(Inputbox("What is the order amount?"))
Related
I have been working on this vb script for around an hour trying to get it to work there is many skype spammer scipts but i want to make one that includes a random number generator my script is this i call it at the moment "Skype_randizer_mk1"
If anyone would be able to take a look at it it would be greatly appreciated.
When i was posting this the website said i had to indent this so it may look a little strange
The Delay variable is the amount of time it will take to enter another number
I don't mind if this program makes only numerical values that is what i intend for it to do
set shell = createobject ("wscript.shell")
dim max
dim min
dim delay
max = 100
min = 1
delay = 0.00000001
for i = 1 to 5
randomize
intnumber = int((max - min + 1) * rnd + min )
wscript.echo intnumber
Next
for b=1 to delay
shell.sendkeys (intnumber)
wscript.sleep(delay)
if not isnumeric(delay) then
wscript.quit
end if
msgbox "You have 5 seconds to get to your inputbox."
wscript.sleep ( 5000 )
Next
You have lots of problems with your code:-
You should ALWAYS declare your variables using Dim: e.g. Dim shell
You are missing a Next for one of your For loops
Line 10 doesn't make much sense. It says: for b=1 to delay, but delay = 0.00000001, so your loop will never run. Also, why does this section even need to loop? I think you probably just want an If/Then/Else
Line 11 should probably say shell.SendKeys, not strshell.sendkeys as this is an uninitialised variable
Line 13 is checking for a numeric delay value. How will this ever be anything other than numeric when you are assigning a value of 0.00000001 on line 4 and it never changes. As a result, you will not exit the script until the for loop on line 5 has executed 5 times.
I have a big block of data >50 across and >1500 down and some of the entries are very large negative numbers like -1000000 or -9820000 and I want all of those to be turned into -100's.
I also want any non-zero numbers that are above -100 to show 2 decimals.
I thought this vba macro would work, but its causing excel to freeze and the excel screen turns all grey and idk whats happening.
I think it might be because there are so many cells, so it takes a long time and over loads something, is there any way to make this code more efficient??
Sub Blah()
For ColNum = 2 To WorksheetFunction.CountA(Range("1:1"))
For RowNum = 2 To WorksheetFunction.CountA(Range("A:A"))
If Cells(RowNum, ColNum) < -101 Then Cells(RowNum, ColNum) = -100
If Cells(RowNum, ColNum) <> 0 And Cells(RowNum, ColNum).Value > -100 Then Cells(RowNum, ColNum).NumberFormat = "0.00"
Next RowNum
Next ColNum
End Sub
Although Tmdean pointed out the solution, I'll post a sample code which might be of help.
Edit1: It seems assigning negative value in cell took a while as well. So apply the same principle. Get the relevant cells first and assign value in one go.
Sub marine()
Dim r As Range, c As Range, nonzero As Range, s As Range
Set r = ActiveSheet.UsedRange
For Each c In r
Select Case True
Case c.Value <= -101
'~~> Identify the cells first and combine all of them, don't assign value
If s Is Nothing Then Set s = c _
Else Set s = Union(s, c)
Case c.Value <> 0 And c.Value > -100
'~~> Identify the cells first and combine all of them, do not format
If nonzero Is Nothing Then Set nonzero = c _
Else Set nonzero = Union(nonzero, c)
End Select
Next
'~~> Once you got all the cells, assign value in one go
If Not s Is Nothing Then s.Value = -100
'~~> Once you got all the cells, format in one go
If Not nonzero Is Nothing Then nonzero.NumberFormat = "0.00"
End Sub
You can replace this with your For Loop, whatever is easier.
You can also be more explicit on setting the Range Object instead or using UsedRange. HTH.
There's nothing in that code that's particularly slow. How many cells are you changing? The only suggestion I have is to set the NumberFormat for the entire range beforehand (to 0.00), then turn set it back to General only for the cells that are 0 or -100 in your loop. Changing the NumberFormat is probably the most expensive operation, so you want to minimize the times you set it for individual cells.
Someone will be here shortly to recommend you turn off Application.ScreenUpdating.
Afternoon,
Im playing around with a little bidding script im trying to write. But im having trouble with formatNumber function.
currentBid = 50.51 'from database dataType double(16,2)
yourBid = isNumeric(Request("bid"))
If FormatNumber(yourBid,2) > FormatNumber(currentBid,2) Then
Response.Write"bid successful... woop woop"
else
Response.Write"you cant bid below the current asking price"
end if
But if i was to bid 1000 is writes "you cant bid below the current asking price"
Please advise
Regards
Shane
'Changed as advised
currentBid = 50.51 'value from database
If IsNumeric(Request.Form("bid")) Then
yourBid = CDbl(Request.Form("bid"))
end if
You have two issues here:
As Ekkehard mentioned, IsNumeric() returns a boolean. To test if the value is numeric and then store to your variable, use:
If IsNumeric(Request("bid")) Then yourBid = CDbl(Request("bid"))
FormatNumber() returns a string representation of a number. So you're comparing one string against another instead of one number against another. If you need to round your numbers to two decimals, use the Round() function instead:
If Round(yourBid,2) > Round(currentBid,2) Then
Edit: Proof.
MsgBox VarType(4) ' 2 = vbInteger
MsgBox VarType(FormatNumber(4)) ' 8 = vbString
The line
yourBid = isNumeric(Request("bid"))
does not store a valid number into yourBid, but the (booelan) result of the IsNumeric() function applied to Request("bid").
Change the line to
yourBid = CDbl(Request("bid"))
and see if your IF statement works as expected. Then add a proper validation for Request("bid").
I have been putting off asking this questions as it seemed like such a simple problem I could solve but i have be racking my brains and now cant think. I have tried loads of different combinations of code but cant seem to find the correct one.
My 2 porblems:
I need to create a loop that will only fire when a variable is between 2 numbers. Example (this does not work) : Do While SOPickRS.RecordCount >=1 and <=4
I need to a variable that monitors what record the loop is on. A counter could work however is there not an actual ADODB variable for it?
My Current Code:
While Not SOPickRS.EOF
Do Until SOPickRS.RecordCount = 4
'Do Stuff
Loop
Do Until SOPickRS.RecordCount = 8
'Do Stuff
Loop
Do Until SOPickRS.RecordCount = 12
'Do Stuff
Loop
SOPickRS.MoveNext
Wend
GTG has also pointed that RecordCount will not work as it never changes.
Are you trying for something like this :
currentRecord = 1
While Not SOPickRS.EOF
if (currentRecord >=1 and currentRecord <=4)
'Do Stuff
end if
currentRecord = currentRecord + 1
wend
Is it better to use NOT or to use <> when comparing values in VBScript?
is this:
If NOT value1 = value2 Then
or this:
If value1 <> value2 Then
better?
EDIT:
Here is my counterargument.
When looking to logically negate a Boolean value you would use the NOT operator, so this is correct:
If NOT boolValue1 Then
and when a comparison is made in the case of the first example a Boolean value is returned. either the values are equal True, or they are not False. So using the NOT operator would be appropriate, because you are logically negating a Boolean value.
For readability placing the comparison in parenthesis would probably help.
The latter (<>), because the meaning of the former isn't clear unless you have a perfect understanding of the order of operations as it applies to the Not and = operators: a subtlety which is easy to miss.
Because "not ... =" is two operations and "<>" is only one, it is faster to use "<>".Here is a quick experiment to prove it:
StartTime = Timer
For x = 1 to 100000000
If 4 <> 3 Then
End if
Next
WScript.echo Timer-StartTime
StartTime = Timer
For x = 1 to 100000000
If Not (4 = 3) Then
End if
Next
WScript.echo Timer-StartTime
The results I get on my machine:
4.783203
5.552734
Agreed, code readability is very important for others, but more importantly yourself. Imagine how difficult it would be to understand the first example in comparison to the second.
If code takes more than a few seconds to read (understand), perhaps there is a better way to write it. In this case, the second way.
The second example would be the one to go with, not just for readability, but because of the fact that in the first example, If NOT value1 would return a boolean value to be compared against value2. IOW, you need to rewrite that example as
If NOT (value1 = value2)
which just makes the use of the NOT keyword pointless.