Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


addition in vbscript

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,56485
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> addition in vbscript
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 addition in vbscript - 2/8/2008 12:46:53 AM   
  Camus

 

Posts: 14
Score: 0
Joined: 2/8/2008
Status: offline
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.


thanks
 
 
Post #: 1
 
 RE: addition in vbscript - 2/8/2008 12:55:18 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Camus,

please explain: "column", "zone". How can the values be accessed? Such
information is necessary to go from this pseudocode

sum = 0
for all elements of ?column?
     sum = sum + element

to real VBScript code.

Regards

ehvbs

(in reply to Camus)
 
 
Post #: 2
 
 RE: addition in vbscript - 2/8/2008 1:48:20 AM   
  Camus

 

Posts: 14
Score: 0
Joined: 2/8/2008
Status: offline
hey thanks,

within the software im using vbscript with, a column of data is called a zone, each zone has a name (such as 'clientname' for example).

you refer to lines in the zone by saying "clientname.line(n).value"...


so i could say clientname.line(n).value = "Mr Dave Bob"

that would make every row in the zone contain "Mr Dave Bob"



I could say clientname.line(3).value = "Mr Dave Bob"

that would obviously make the third line in the zone contain "Mr Dave Bob"


the way i apply code to each line in the zone is by using a FOR loop -




      


i generally put all other code within this FOR loop...


hope this is enough info...let me know if you want to know anything else!

im doing this within an OCR package by the way, using vbscript to manipulate the text and numbers.

< Message edited by Camus -- 2/8/2008 1:50:30 AM >

(in reply to Camus)
 
 
Post #: 3
 
 RE: addition in vbscript - 2/8/2008 2:20:03 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Camus,

thanks for the info; try code like this:

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.

Regards

ehvbs

(in reply to Camus)
 
 
Post #: 4
 
 RE: addition in vbscript - 2/11/2008 1:32:53 AM   
  Camus

 

Posts: 14
Score: 0
Joined: 2/8/2008
Status: offline
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 >

(in reply to ehvbs)
 
 
Post #: 5
 
 RE: addition in vbscript - 2/11/2008 1:51:53 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Camus,

could you try something like this

  Dim nIdx
  For nIdx = 1 To <ZoneName>.LineCount
       MsgBox Join( Array( nIdx, TypeName( <ZoneName>( nIdx ).Value ) ), " " )
  Next

to determine the data type of the zone's values?

Most of my VBScript work is to write data validation and support code for
the Auto* applications
   http://www.wiencke-software.de/

Regards

ehvbs

(in reply to Camus)
 
 
Post #: 6
 
 RE: addition in vbscript - 2/11/2008 2:02:25 AM   
  Camus

 

Posts: 14
Score: 0
Joined: 2/8/2008
Status: offline
I used that code and i got a message box saying "1 string" and then the message boxes counted up to my maximum line count as i clicked OK...


One other thing, is that if i say


      


assuming amount.line(1).value = 6 and amount.line(2).value = 5, i get an output of "6 5", rather than a mathematical addition of "11".

so that would suggest my code is recognising the two lines as text rather than numeric?

< Message edited by Camus -- 2/11/2008 2:07:33 AM >

(in reply to ehvbs)
 
 
Post #: 7
 
 RE: addition in vbscript - 2/11/2008 2:11:33 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Camus,

if "Amount.Line(n).Value" returns a string like "1234", then CLng() should
convert it to an long integer than can be added to nSum.

Int() returns the integer part of a number. Applying this function to a string
won't work.

So try CInt(), CLng() or CDbl() according to the range og your data.

Good luck!

ehvbs

(in reply to Camus)
 
 
Post #: 8
 
 RE: addition in vbscript - 2/11/2008 2:30:09 AM   
  Camus

 

Posts: 14
Score: 0
Joined: 2/8/2008
Status: offline
The numbers I'm working with look more like this actually -

67.45
13.40
32.54


What data type would cover that? I'm pretty sure I've tried them all now though.

(in reply to ehvbs)
 
 
Post #: 9
 
 RE: addition in vbscript - 2/11/2008 2:31:12 AM   
  ebgreen


Posts: 4971
Score: 31
Joined: 7/12/2005
Status: offline
CDbl()

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Camus)
 
 
Post #: 10
 
 RE: addition in vbscript - 2/11/2008 2:37:21 AM   
  Camus

 

Posts: 14
Score: 0
Joined: 2/8/2008
Status: offline
I've tried that too, I guess this must be a program specific problem then, thanks alot for your help though, at least i have something to go on now.

(in reply to ebgreen)
 
 
Post #: 11
 
 RE: addition in vbscript - 2/11/2008 2:43:30 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Camus,

this test code


      

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

should be added.

Good luck!

ehvbs

(in reply to Camus)
 
 
Post #: 12
 
 RE: addition in vbscript - 2/11/2008 2:57:38 AM   
  Camus

 

Posts: 14
Score: 0
Joined: 2/8/2008
Status: offline
I didn't completely understand that code, is the code in the code box something i could try and use? or the code underneath it?


Sorry, I'm still learning all of this.

(in reply to ehvbs)
 
 
Post #: 13
 
 RE: addition in vbscript - 2/11/2008 3:00:13 AM   
  ebgreen


Posts: 4971
Score: 31
Joined: 7/12/2005
Status: offline
The code in the box demonstrates the concept. The code below is suggested implementation.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Camus)
 
 
Post #: 14
 
 RE: addition in vbscript - 2/11/2008 3:09:06 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Camus,

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.

(in reply to Camus)
 
 
Post #: 15
 
 RE: addition in vbscript - 2/11/2008 3:38:34 AM   
  Camus

 

Posts: 14
Score: 0
Joined: 2/8/2008
Status: offline
Ok thanks alot
The code I'm using now is this -



      


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 >

(in reply to ehvbs)
 
 
Post #: 16
 
 RE: addition in vbscript - 2/11/2008 3:41:22 AM  1 votes
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
sorry, but the code isn't readable.

Please test this code:

dblSum = 0.0
For n = 1 To Amount.LineCount
   dblTmp = CDbl(Amount.Line(n))
   dblSum = dblSum + dblTmp
Next
MsgBox dblSum

< Message edited by ehvbs -- 2/11/2008 3:44:41 AM >

(in reply to Camus)
 
 
Post #: 17
 
 RE: addition in vbscript - 2/11/2008 3:43:02 AM   
  Camus

 

Posts: 14
Score: 0
Joined: 2/8/2008
Status: offline
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 >

(in reply to ehvbs)
 
 
Post #: 18
 
 RE: addition in vbscript - 2/11/2008 3:51:18 AM  1 votes
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
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?

(in reply to Camus)
 
 
Post #: 19
 
 RE: addition in vbscript - 2/11/2008 3:54:52 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Try this:

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 >

(in reply to ehvbs)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> addition in vbscript Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts