ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Dividing variables (https://www.excelbanter.com/excel-programming/436158-dividing-variables.html)

Merlynsdad

Dividing variables
 
I need to divide two variables by each other to get a percentage.

intRows represents the total rows in the spreadsheet.
intPerf represents the rows left after I remove some rows with code.
intPct = intPerf/intRows.

All are dimensioned as integers.

When I do the calculation the value in the Watch List of intRows is 88,
intPerf is 34. intPct should then be 38.63% but is showing zero. The code for
the division is

intPct=intPerf/intRows

What am I doing wrong?


Matthew Herbert[_3_]

Dividing variables
 
Merlynsdad,

An integer variable takes an integer value. Since .3863 is not an integer
value, VBA will return the next best value which will satisfy an integer
(i.e. 0 in your case). Change intPct to be a different data type, say a
Double or a Single.

Dim dblPct As Double
dblPct = intPerf/intRows

Best,

Matthew Herbert

"Merlynsdad" wrote:

I need to divide two variables by each other to get a percentage.

intRows represents the total rows in the spreadsheet.
intPerf represents the rows left after I remove some rows with code.
intPct = intPerf/intRows.

All are dimensioned as integers.

When I do the calculation the value in the Watch List of intRows is 88,
intPerf is 34. intPct should then be 38.63% but is showing zero. The code for
the division is

intPct=intPerf/intRows

What am I doing wrong?


JLGWhiz[_2_]

Dividing variables
 
If you are dimesioning you variable intPct as an integer, you will get a 0
value. The intPct should be dimensioned as a double to get the decimal
value of a percent.

Dim intPct As Double


"Merlynsdad" wrote in message
...
I need to divide two variables by each other to get a percentage.

intRows represents the total rows in the spreadsheet.
intPerf represents the rows left after I remove some rows with code.
intPct = intPerf/intRows.

All are dimensioned as integers.

When I do the calculation the value in the Watch List of intRows is 88,
intPerf is 34. intPct should then be 38.63% but is showing zero. The code
for
the division is

intPct=intPerf/intRows

What am I doing wrong?





All times are GMT +1. The time now is 09:22 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com