Hey, I'm using vbscript to try and add up a column (zone) of numbers, the column might look something like this...
54 43 54 65 756 78 8 88
How would i add these up to obtain a 'total' number which i can then use to compare to a second figure? I've been looking around for a while now but cant find anything that seems useful. hope someone can help.
Dim nSum : nSum = 0 Dim nIdx For nIdx = 1 To <ZoneName>.LineCount nSum = nSum + CLng( <ZoneName>( nIdx ).Value ) ' assuming data type Long; change to CDbl(..) if appropriate Next
If it isn't a secret, I'd be interested to know more about this "OCR Software" you use - just because I'm working in this area too.
I've tried that but kept getting type mismatch error messages, ive tried other data types but not sure what the problem is, any ideas?
No it's no secret, I'm using a program called AnyDoc http://www.anydocsoftware.com enables you to set up complex templates to ocr multipage images and output it any way you want. What software are you using?
By the way, after I'd adapted your code it looked like this -
Does that look right?
< Message edited by Camus -- 2/11/2008 1:48:48 AM >
shows that CDbl() is the conversion function to use. As my german locale needs a "," instead of a decimal point, the "sTest = Replace( sTest, ".", "," )" line attends to that.
Production code should guard against problems (bad documents/bad OCR results). So a test using IsNumeric() and/or Error handling
On Error Resume Next dblTmp = CDbl( amount.line(nIdx).value If 0 <> Err.Number Then ... End If On Error GoTo 0 dblSum = dblSum + dblTmp
the code in the box should be saved to a .vbs file and executed from the command line
cscript <thenameyousaveditunder>.vbs
it should output: "113,39" (german locale) or "113.39" (english locale(s)). The important point is: if you data are strings like "12.34" CDbl will work provided your locale 'sees' a point (.) as decimal point; if not then you'll have to replace the "." with something appropriate (e.g. "," in my case).
Please post the exact code you use in the AnyDoc script, if CDbl() [+ Replace, if necessary] doesn't work.
We can think about pre checks/error handling (the code snippet in the text) later.
This code is doing something close to what it should do, I put in a msgbox to test the output. What's happening is that it goes down the column adding the value of the line to the previous and producing a cumulative total (each time i click ok on the msgbox it displays a new cumulative total with the next line added to it...the code goes through each line in this fashion but when it gets to the bottom row of the numbers it seems to carry on adding numbers to the cumulative total, it looks as though it starts at the beginning of the list and adds each number a second time. the final total is not equal to exactly double the correct total ( which is what you'd obviously expect if it added the entire column up twice) it is about 0.90 under, so something funny is going on. Any idea why it's doing this?
< Message edited by Camus -- 2/11/2008 3:41:55 AM >
Using that code I'm getting the type mismatch code again...
I'm guessing when the 'on error resume next' code was in there it was skipping over a value that it didnt recognise as numeric and thats why the totals didn't match?
< Message edited by Camus -- 2/11/2008 3:50:01 AM >
I still can't connect the code you posted to your description. E.g. there isn't any output inside the loop. How do you determmine the "starts again at the top" behaviour?. Do you think it's possible, that AnyDoc calls your function more than once?
dblSum = 0.0 For n = 1 To Amount.LineCount On Error Resume Next dblTmp = CDbl(Amount.Line(n)) If 0 <> Err.Number Then MsgBox Err.Description & " |" & Amount.Line(n) & "|" dblTmp = 0.0 End If On Error GoTo 0 dblSum = dblSum + dblTmp Next MsgBox dblSum
< Message edited by ehvbs -- 2/11/2008 3:55:53 AM >