counting for 3 values decrementing

SVKBaki

New Member
Joined
Feb 9, 2011
Messages
6
Hi, I would kindly like to ask for help, I don´t know how to write following thing.

I have 3 values A,B,C (A+B+C = 100 always) A,B,C dim as integer obviously.

Value A at first step will be set to 100, B = 0, C= 0.

I need to create loop with step decrementation by 1, so if A=99,B=1,C=0,
Next step A = 98, B = 1, C = 1 but I need every possible of combination so if A=97, B can be 0 or 1 or 2 or 3 and C can therefore be C = 3 or 2 or 1 or 0

It first I thought using something like:

For A = 100 to 0
For B = (100-A) to 0
For C = (100-A-B) to 0

like 3 For cycles, one in another, but I am not sure if this is the way, as I have very little experience with VBA and programming at all.

Every combination of numbers has to be inserted to formula and the result compared with other (This I can easily manage with if then statement). So the problem lies in writing this loop, for correctly assesing all the possible combinations).

If anybody could help me with that, I would be very grateful.

Thanks in advance
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
This is working for me:

Code:
Sub counts()
Dim A As Integer
Dim B As Integer
Dim C As Integer
Dim R As Long
R = 1
For A = 100 To 0 Step -1
      For B = (100 - A) To 0 Step -1
            For C = (100 - A - B) To 0 Step -1
                  If A + B + C = 100 Then
                        Cells(R, 1) = A
                        Cells(R, 2) = B
                        Cells(R, 3) = C
                        R = R + 1
                  End If
            Next C
      Next B
Next A
End Sub

The line

Code:
If A + B + C = 100 Then

prevents it from showing
99, 0, 0
98, 0, 0
etc.

Let me know if it works for you.
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,472
Members
452,915
Latest member
hannnahheileen

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