Simple For Next Loop Giving Wrong result

nmounir

Board Regular
Joined
Oct 16, 2020
Messages
107
Office Version
  1. 365
Platform
  1. Windows
Hello Everyone,

I created a simple for Next loop that could be done with an if formula on excel. however, the result from the for Next loop is different than the excel formula for some reason.

in column J if the Interest amount is >=2 then rating = Good Return otherwise should be Low return. In Cells K5, K7,K10and K17 for next loop (in column K) showing good return and in excel formula (Column L) showing low return. Excel formula is obviously correct but why did the for Next loop is getting it wrong?
below is the code I used.

Your help is much appreciated

1643440976250.png


1643441225329.png
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
You "Dimmed" CDRValue as Integer, so it cannot work with decimals; try
VBA Code:
Dim CDRValue as Single

Bye
 
Upvote 0
Solution
This problem is quite interesting, because it is generally assumed that using option explicit is helpful but this is a case where using it was definitely not helpful. The code would have worked perfectly with no declarations.
 
Upvote 0
Using Dim the programmer takes control of the action, without he rely on vba best guessing
 
Upvote 0
The programmer has just as much control if the programmer understands VBA default declarations. I don't use option explicit and I don't declare variables which are absolutely obvious from context. This means any declarations in my code are there because they have to be there , this means they stand out and are easy to read because there isn't a long list of declarations at top of module. I often see code with a long list of declarations which tell me nothing. It is easy to hide an important one in the middle of the list which I miss!!
 
Upvote 0
In my experience it is easier for a Programmer to make typing errors than conceptual mistakes. Option Explicit will save us in most of the cases with typos.
Whereas when we make "serious" conceptual errors then we can only rely on our abilities.

Dim offer also the chance to save memory and some cpu power, but this is not a keypoint in most of our situations.
But (please) don't take my words too seriously
Bye
 
Upvote 0

Forum statistics

Threads
1,215,016
Messages
6,122,700
Members
449,092
Latest member
snoom82

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top