Count Odd & Even Numbers from A Loop

S.H.A.D.O.

Well-known Member
Joined
Sep 6, 2005
Messages
1,915
Good evening,

What I am trying to do is to loop through ALL 6 number combinations and count how many Odd & Even numbers there are in each of the 6 positions.
For example, for number TEN I would like the total combinations where number TEN is Odd in position ONE, then number TEN is Even in position ONE, then number TEN is Odd in position TWO, then number TEN is Even in position TWO etc upto and including where number TEN is Odd in position SIX, then number TEN is Even in position SIX.
Obviously number TEN is Even, but out of the TWELVE columns of data for each of the numbers there will be SIX with a figure in it and SIX showing ZERO.
Here is the code I have so far but can't quite get it to work.
Thanks in advance.

Code:
Option Explicit
Option Base 1

Sub Odds_and_Evens_by_Position()
    Dim A As Long, B As Long, C As Long, D As Long, E As Long, F As Long
    Dim OddA As Long, OddB As Long, OddC As Long
    Dim OddD As Long, OddE As Long, OddF As Long
    Dim EvenA As Long, EvenB As Long, EvenC As Long
    Dim EvenD As Long, EvenE As Long, EvenF As Long
    Dim n As Long
    With Sheets("Odd & Even").Select
        Range("B:N").ClearContents
        Range("B2").Select
        For n = 1 To 30
            For A = 1 To 25
                If A Mod 2 = 1 Then OddA = OddA
                If A Mod 2 = 0 Then EvenA = EvenA
                For B = A + 1 To 26
                    If B Mod 2 = 1 Then OddB = OddB
                    If B Mod 2 = 0 Then EvenB = EvenB
                    For C = B + 1 To 27
                        If C Mod 2 = 1 Then OddC = OddC
                        If C Mod 2 = 0 Then EvenC = EvenC
                        For D = C + 1 To 28
                            If D Mod 2 = 1 Then OddD = OddD
                            If D Mod 2 = 0 Then EvenD = EvenD
                            For E = D + 1 To 29
                                If E Mod 2 = 1 Then OddE = OddE
                                If E Mod 2 = 0 Then EvenE = EvenE
                                For F = E + 1 To 30
                                    If F Mod 2 = 1 Then OddF = OddF
                                    If F Mod 2 = 0 Then EvenF = EvenF
                                    OddA = OddA + 1
                                    EvenA = EvenA + 1
                                    OddB = OddB + 1
                                    EvenB = EvenB + 1
                                    OddC = OddC + 1
                                    EvenC = EvenC + 1
                                    OddD = OddD + 1
                                    EvenD = EvenD + 1
                                    OddE = OddE + 1
                                    EvenE = EvenE + 1
                                    OddF = OddF + 1
                                    EvenF = EvenF + 1
                                Next F
                            Next E
                        Next D
                    Next C
                Next B
            Next A
        Next
        For n = 1 To 30
            With ActiveCell
                .Offset(2, 0).Value = n
                .Offset(2, 1).Value = OddA
                .Offset(2, 2).Value = EvenA
                .Offset(2, 3).Value = OddB
                .Offset(2, 4).Value = EvenB
                .Offset(2, 5).Value = OddC
                .Offset(2, 6).Value = EvenC
                .Offset(2, 7).Value = OddD
                .Offset(2, 8).Value = EvenD
                .Offset(2, 9).Value = OddE
                .Offset(2, 10).Value = EvenE
                .Offset(2, 11).Value = OddF
                .Offset(2, 12).Value = EvenF
                .Offset(1, 0).Select
            End With
        Next n
    End With
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I had another go at this yesterday but couldn't seem to get any further than before.
Any help will be appreciated.
Thanks in advance.
 
Upvote 0
Is the logic I am using wrong to achieve this?
Is there a more straight forward way?
Thanks in advance.
 
Upvote 0
Perhaps if someone could tell me if my original code is close or way off the mark that will help me.
Thanks in advance.
 
Upvote 0
Found two mistakes in your code which I made a quick fix for, but the output contains the same number in every cell. Run this and see what it does and if it's what you originally meant, to be honest though, your query is not clear or easy to understand:
Code:
Sub Odds_and_Evens_by_Position()
Dim A As Long, B As Long, C As Long, D As Long, E As Long, F As Long
Dim OddA As Long, OddB As Long, OddC As Long
Dim OddD As Long, OddE As Long, OddF As Long
Dim EvenA As Long, EvenB As Long, EvenC As Long
Dim EvenD As Long, EvenE As Long, EvenF As Long
Dim n As Long
Application.ScreenUpdating = False
range("B:N").ClearContents
range("B2").Select
For n = 1 To 30
    For A = 1 To 25
        If A Mod 2 = 1 Then OddA = OddA
        If A Mod 2 = 0 Then EvenA = EvenA
        For B = A + 1 To 26
            If B Mod 2 = 1 Then OddB = OddB
            If B Mod 2 = 0 Then EvenB = EvenB
            For C = B + 1 To 27
                If C Mod 2 = 1 Then OddC = OddC
                If C Mod 2 = 0 Then EvenC = EvenC
                For D = C + 1 To 28
                    If D Mod 2 = 1 Then OddD = OddD
                    If D Mod 2 = 0 Then EvenD = EvenD
                    For E = D + 1 To 29
                        If E Mod 2 = 1 Then OddE = OddE
                        If E Mod 2 = 0 Then EvenE = EvenE
                        For F = E + 1 To 30
                            If F Mod 2 = 1 Then OddF = OddF
                            If F Mod 2 = 0 Then EvenF = EvenF
                            OddA = OddA + 1
                            EvenA = EvenA + 1
                            OddB = OddB + 1
                            EvenB = EvenB + 1
                            OddC = OddC + 1
                            EvenC = EvenC + 1
                            OddD = OddD + 1
                            EvenD = EvenD + 1
                            OddE = OddE + 1
                            EvenE = EvenE + 1
                            OddF = OddF + 1
                            EvenF = EvenF + 1
                        Next F
                    Next E
                Next D
            Next C
        Next B
    Next A
Next n
For n = 1 To 30
    With ActiveCell
        .Offset(2, 0).Value = n
        .Offset(2, 1).Value = OddA
        .Offset(2, 2).Value = EvenA
        .Offset(2, 3).Value = OddB
        .Offset(2, 4).Value = EvenB
        .Offset(2, 5).Value = OddC
        .Offset(2, 6).Value = EvenC
        .Offset(2, 7).Value = OddD
        .Offset(2, 8).Value = EvenD
        .Offset(2, 9).Value = OddE
        .Offset(2, 10).Value = EvenE
        .Offset(2, 11).Value = OddF
        .Offset(2, 12).Value = EvenF
        .Offset(1, 0).Select
    End With
Next n
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks for the response JackDanIce,

No the code is not giving the correct results.
Basically, I want to run through all the combinations of 6 numbers from 30 and count how many of those combinations have each of the numbers from 1 to 30 that are Odd & Even.
So number ONE can ONLY appear in position ONE, where as number TEN can appear in ALL 6 positions.
So for example, number TEN will show position 1 odd as "x" total, position 1 even as zero, position 2 as "x" total etc upto position 6 odd as "x" total and position 6 even as zero.
I hope this explains it a bit more.
Thanks in advance.
 
Upvote 0
But I haven't done anything to your code other than correct 2 programming mistakes. Your code is giving that output.

It would really help if you posted a screenshot of your expected outcome, as stated above, your query is not clear.

What do you mean combinations of 6 numbers from 30?
 
Upvote 0
The thing is that the first column should only have values of the ODD numbers and the second column should only have values of the EVEN numbers etc.
For the number TEN it should look like this:-

Code:
	ODD	EVEN	ODD	EVEN	ODD	EVEN	ODD	EVEN	ODD	EVEN	ODD	EVEN	*Totals
	1	1	2	2	3	3	4	4	5	5	6	6	
10		575,757		740,259		329,004		62,244		4,914		126	1,712,304

I have just put the headers in to make it easier to read.

Thanks in advance.
 
Upvote 0
Sorry, but it's still not making sense. I don't know what you mean by combinations of numbers, you're not providing a proper output, you're not explaing the logic of what exactly you're trying to do.

Probably best someone else tries this one, good luck.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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