Can't get Loop to work

matjusm

New Member
Joined
Sep 29, 2010
Messages
32
I need some help with a relatively simply piece of VBA code.

Basically I'm working on a financial model with certian inputs and outputs.
Currently the NPV of the project is negative but I want to create a Macro that keeps adding 1€ to the price of the output until the NPV is above 0.

However the code I've got now just keeps looping endlessly and doesn't stop when the NPV reaches a positive value.

This is my code:

Sub Macro1()
'
' Macro1 Macro
' Increases price until NPV is positive'
'
Do

Range("F67").Select
Selection.Copy
Range("B67").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Loop Until B43 > 0

End Sub

Explanations:
B67= Price that feeds into the model
F67= B67+1
B43= NPV

What am I doing wrong?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You only endlessly copy in loop. I see no incrementing.
 
Upvote 0
Why would you want to do this in a loop?

If the value is negative, then just add the integer difference between it and 0

Say your value is in A1

if range("A1").value < 0 then range("A1").value + abs(int(range("A1").value

or in vb

Code:
Sub test()
    Dim i As Double
    i = Range("A1").Value
    If i < 0 Then Range("A1").Value = i + Abs(Round(i, 0))
End Sub
 
Last edited:
Upvote 0
There's a glaring error that would show up immediately if you headed your code module with the compiler directive Option Explicit:-
Code:
[B][COLOR=red]Option Explicit[/COLOR][/B]
 
Sub Macro1()
'
' Macro1 Macro
' Increases price until NPV is positive'
'
' ...etc
 
Upvote 0
Got it to work: the problem was that I wrote the condition I wanted to be met incorrectly. I should have written:

Range("B43") > 0 not just B43 > 0
 
Upvote 0
I'm curious, why are you doing this using increments? Is there some other process you're not including in the code?
 
Upvote 0
Got it to work: the problem was that I wrote the condition I wanted to be met incorrectly. I should have written:

Range("B43") > 0 not just B43 > 0

Did you discover that by adding Option Explicit to your module?

I recommend you do that to all your code modules - it can save you a lot of time and bother!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,513
Messages
6,179,214
Members
452,895
Latest member
BILLING GUY

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