VBA Do While going to 0

BigRuski

New Member
Joined
Feb 1, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
So I am doing a HW assingment for a class where we have to do a logic loop function seen below. I have gotten my VBA to work for every condition except for when x<50. I tried making a do while loop that will subtract 5 until x falls below 50 but instead any number that is 55 and more, it just spits out the answer 0. What am I doing wrong?

1643749747691.png

1643749802947.png
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Welcome to the Board!

It is generally advised NOT to use a lot of GOTO commands in Excel VBA. That is more "old programming methodology" that is not used much anymore. The only time I ever really use them is if I add error handling, and want to send it to my error handling code at the very bottom of my code.

Rather, it is more common to use IF...THEN...ELSE. And then you can really just follow the same flow as your workflow diagram (the "THEN" clause is the TRUE part, and the "ELSE" part is the FALSE part).
And you can nest IF...THEN...ELSE blocks inside of other IF...THEN...ELSE blocks.

So I would advise trying to rewrite your code using IF...THEN...ELSE blocks and getting rid of all the GOTOs.
 
Upvote 0
Here are a few links that show you how to nest IF statements in VBA:
 
Upvote 0
Dante,

They said it was a homework problem. I think they were just looking for tips, not someone to do it for them.
We typically maintain the policy of not doing people's homework or assignments for them.
 
Upvote 0
We typically maintain the policy of not doing people's homework or assignments for them.
Sorry. I usually don't answer home work, but I found it interesting to solve the diagram.
If you can delete my answer from post #4, I have no problem, instead I present a couple of tips:

BigRuski

You must follow the sequence of the diagram.
You have in your code this:
VBA Code:
if x < 10 and x >= 5 then msgbox "Over 5"
Actually the diagram says x >=10, so you could put:
VBA Code:
If x >= 10 Then

---

You are declaring the function as a double, but if you are going to return a text, for example "over 5", then it must be variant.
Function ProbII_2(x As Double) As Double
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,659
Members
449,091
Latest member
peppernaut

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