mardi 28 avril 2020

VBA Question: Running iterations using multiple for loops and exit once a certain condition has been made

I need help with my code, please.

I am running through a list defined in range M9:M53 using a VBA command button. I am Copying the first value in the list into a different cell in the same sheet, then I have formulas that generate calculations and output a value in cell K24.

This is a linear relationship, the more I go through the range the closer I get to exceed number 5. The idea is that I want the Loop to get as close to 5 before exceeding it. So a part of the missing component would be checking through the list, and copying the right number. So if M15 is the first cell value to be greater than 5, then I would want to copy the previous cell number M14 and leave it on the cell I6

The next part of the code that is missing would be a loop that must occur before I pass from each value in range M9 to M54.

Grab Value in Cell F5, and start subtracting 1. For example, it will the cell has input value 10, then it will check K24 if K24 is greater than 5, then it will subtract 1 again, and so on. if the K24 value is finally less than 5, then it will exit the LOOP completely and the program would have completed its function. ******

This number directly affects the result in K24 which checks for being less than 5. It should stop the closest that it can to generating a result that K24 than is less than 5, without exceeding it.

An Additional condition to step out of this loop and go onto the next value in list M9:M54 Range would be

Checking that the value in Cell A7 is greater than 13.5. Going lower would mean skipping to the next value in range M9:M54

One additional and final condition would be to check for Cell Value G5 being less than or equal to 120.

For example. If copying M15 yields and answer for K24 that is less than 5, but cell G5 still yields over 120, then it should still skip to next value in the M range, M16.

Below a portion of the loop which needs a lot of work.


Private Sub CommandButton1_Click()

Dim Cell As Range

For i = 9 To 53 Range("M" & i).Copy Range("I6")

If Range("K24").Value < 5 Then

Exit Sub

End If Next Application.CutCopyMode = False

End Sub

Aucun commentaire:

Enregistrer un commentaire