vba calcs are acting funny

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a process with 42 pages of code. There are a boatload of calcs in it
that seem to be working.
However there is one that is giving me a hard time and I can't figure it out.
I have declarations: public overallscore as integer, public allscore as
integer, and public totalscore as integer, and public dataset as integer
I do not have any declarations for x.

OverAllScore = totalscore / dataset 'OverAllScore =0
x = totalscore / dataset ' x=-0.5
allscore = x 'allscore =0
If OverAllScore >= 0.5 Then . . .

I am getting a value of -2 for totalscore and 4 for dataset. However
overallscore =0.
On the contrary x=-0.5 which is as expected. Then allscore ends up =0
instead of equal to x.

What am I possibly overlooking?

TIA
 
Papa Jonah said:
I have a process with 42 pages of code. There are a boatload of calcs in it
that seem to be working.
However there is one that is giving me a hard time and I can't figure it out.
I have declarations: public overallscore as integer, public allscore as
integer, and public totalscore as integer, and public dataset as integer
I do not have any declarations for x.

OverAllScore = totalscore / dataset 'OverAllScore =0
x = totalscore / dataset ' x=-0.5
allscore = x 'allscore =0
If OverAllScore >= 0.5 Then . . .

I am getting a value of -2 for totalscore and 4 for dataset. However
overallscore =0.
On the contrary x=-0.5 which is as expected. Then allscore ends up =0
instead of equal to x.

What am I possibly overlooking?

TIA

Use Double rather than integer. And declare x as Double, too. If you divide
an integer value with another integer, the result will be an integer.

/Fredrik
 
I am not sure I understand. Do you want me to declare:
public x as double?
I understand that if I divide an integer by an integer, I get an integer -
so why don't I declare the result as an integer?
 
Papa Jonah said:
I am not sure I understand. Do you want me to declare:
public x as double?

Yes. totalscore and dataset too
I understand that if I divide an integer by an integer, I get an integer -
so why don't I declare the result as an integer?

Becuse it doesn't make sense to compare an integer value with a decimal
value. That's what you do with "If OverAllScore >= 0.5 Then . . . "

/Fredrik
 
I don't understand what "double" is supposed to represent, but it works.
Thanks
 
Double is one of the many data types that VBA and Excel understand. A
variable defined as an integer will only accept whole numbers between -32,762
and 32,767. If you try to assign the number 40,000 to an "interger" variable
you will have a problem. A variable defined as "double" accepts a much
larger range of numbers including decimals. There are many other variable
types also. You can look up VBA Data Types to find more details.

By the way, an integer divided by an integer is not always an integer. 5 /
2 = 2.5 which is not an integer.

Stan Shoemaker
Palo Alto, CA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top