Loop with conditions

phil133

Active Member
Joined
May 5, 2015
Messages
257
Office Version
  1. 365
Platform
  1. Windows
Hi.

I have a macro that put values (from 10 to 15) into 4 independent cells. I would like to have 4 conditions so that only if all conditions are met can the macro run.
Let's say my cells are B2,B3,C2,C3.
The conditions are C2>B2, B2>B3, C2>C3 and C3>B3.
C2,B2,B3,C3 will take integer values from 10 to 15.
Only if the above 4 conditions are met I would like the macro to run.

Hope you understand what I'm trying to do.
Thank you.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
How are you wanting to trigger running the macro? For instance do you want to press a button or do you want it to run when you select another worksheet or you enter a value in a cell of the worksheet?
 
Upvote 0
Thank you for your answer!
Sorry I don't understand your question. I have this macro
Code:
Sub LoopM1startC()
Dim i As Single


For i = 10 To 15 Step 0.5
    Worksheets("random fall").Range("B2").Value = i
    Call LoopM1endC
Next i
    


End Sub

This calls LoopM1endC which is the next macro that loops the next cell (and then another 2 times for the next 2 cells) .
I would like this macro to run according to the 4 conditions C2>B2, B2>B3, C2>C3 and C3>B3.
So when I click it to run it will run only for the values that satisfy the conditions.

My native language is not English so bear with me please!
 
Upvote 0
Try this:-

Code:
Sub LoopM1startC()
Dim i As Single


For i = 10 To 15 Step 0.5
    Worksheets("random fall").Range("B2").Value = i
    if cells(2,3).value > cells(2,2) and cells(2,2).value > cells(3,2).value and cells(2,3).value > cells(3,3).value and cells(3,3).value > cells(3,2).value then
       Call LoopM1endC
    End if
Next i
    


End Sub
 
Upvote 0
Do you mean like this?
Code:
For i = 10 To 15 Step 0.5
    Worksheets("random fall").Range("B2").Value = i
    If (Range("C2") > Range("B2")) And (Range("B2") > Range("B3")) And (Range("C2") > Range("C3")) And (Range("C3") > Range("B3")) Then 
         Call LoopM1endC
    End If
Next i
 
Last edited:
Upvote 0
Thank you for your answers but that's not it.
I'm not explaining it well.
I will try again!

If I don't have the conditions my end result will look like this: (In X will be the value of the cell that uses the 4 inputs)
10 10 10 10 X
11 10 10 10 X
12 10 10 10 X
..
10 11 10 10 X
10 12 10 10 X
...
10 10 11 10 X
10 10 12 10 X
10 10 13 10 X
...
10 10 10 11 X
10 10 10 12 X
...etc
If the first cell is B2, the second B3, the third C2, and the fourth C3 I would like it to print the "results" only if C2>B2, B2>B3, C2>C3 and C3>B3 which means that eg. 10 10 10 10 is off.
11 10 10 10 off etc.
But eg. 11 10 15 14 is good. And a lot more combinations are good.

So the desired "print" will look like this:
11 10 12 11.5 X
11 10 12.5 11.5
11 10 12.5 12
11 10 13 11.5
11 10 13 12
11 10 13 12.5
etc
I know I can run all the combinations and then use IF and INDEX and MATCH but I was wondering if I could use a macro.

Hope I'm explaining it well this time!
Thanks for any help!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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