Permutations - Macro

Uncle Bear 0 Reputation points
2025-12-09T17:00:59.5733333+00:00

Hi All,

I need some help composing a Macro.

I'll outline the steps needed to be done and I'll appreciate if anyone can put this into code.

Although for the example given below (2,9,4,7,5,3,6,1,8) there might be more simpler ways to get the results, for the case where this will applied I'll need to follow the steps below.

  • For numbers 1-9
  • Get all permutations for 3 digit combinations using number 1-9 (no duplicates)
  • Filter only those combinations that total 15 (2,9,4,7,5,3,6,1,8)
  • Add three more digits to all possible permutations (no duplicates, including first 3 digits) (2,9,4,7,5,3,6,1,8)
  • Filter out only those that second three digits also equal 15 (2,9,4,7,5,3,6,1,8)
  • Add one more digit to all possible permutations (no duplicates, including first 6 digits) (2,9,4,7,5,3,6,1,8)
  • Filter our only those permutations that 1st, 4th and 7th digit equal 15 (2,9,4,7,5,3,6,1,8)
  • Filter our only those permutations that 3rd, 5th and 7th digit equal 15 (2,9,4,7,5,3,6,1,8)
  • Add one more digit to all possible permutations (no duplicates, including first 7 digits) (2,9,4,7,5,3,6,1,8)
  • Filter our only those permutations that 2nd, 5th and 8th digit equal 15 (2,9,4,7,5,3,6,1,8)
  • Add one more digit to all possible permutations (no duplicates, including first 8 digits) (2,9,4,7,5,3,6,1,8)
  • Filter our only those permutations that 3rd, 6th and 9th digit equal 15 (2,9,4,7,5,3,6,1,8)
  • Filter our only those permutations that 1st, 5th and 9th digit equal 15 (2,9,4,7,5,3,6,1,8)
  • Export all possible permutations to excel

Thanks in advance

[Moderator note: personal information removed]

Microsoft 365 and Office | Excel | For education | Windows
0 comments No comments
{count} votes

7 answers

Sort by: Most helpful
  1. Barry Schwarz 4,871 Reputation points
    2025-12-11T17:11:58.0466667+00:00

    This strikes me as the type of problem an programming instructor would use to convince programmers to analyze the situation before attempting to code.

    First, there are only 9! ways the 9 digits can be selected. That is slightly less than 363K possibilities, not trillions as you suggest. But even that is a trap for the unwary.

    Consider the first three digits. The lowest combination that sums to 15 is 1-5-9. This triplet contains the digits for six (3!) possible values that meet the criteria. If we record the triplets in ascending order of digits, there are only eight that satisfy the criteria: (1-5-9), (1-6-8), (2-4-9), (2-5-8), (2-6-7), (3-4-8), (3-5-7), and (4-5-6). Since each triplet represents six values, There only 48 possible values for the first three digits.

    Considering the second set of three digits, the are also a max of 48 possible values but some of those have duplicate digits with the first set.

    Once we have both sets, there are only three digits left to try an meet the final set of restrictions. There are six (3!) possibilities for those.

    All in all, there are only 13,824 possibilities.

    Now we just have to generate and test the possibilities. One approach could be:

    
    Loop through the triplets.  Call the selected one T1.    
        Loop through the triplets again.  Call the selected one T2.
            If no digits in common then:
                Call the remaining three digits T3.
                Loop through permutations of T1.
                    Loop through permutations of T2.
                        Loop through permutations of T3.
                            If all conditions satisfied then:
                                Export solution.
                        Next T3 permutation.
                    Next T2 permutation.
                Next T1 permutation.
        Next T2 selection.
    Next T1 selection
    

    Since most combinations of T1 and T2 will fail the digits-in-common test, relatively few permutations will actually be generated. The hardest part of the exercise will be writing a permutation routine that can handle multiple triplets and keep track of each one.

    0 comments No comments

  2. Uncle Bear 0 Reputation points
    2025-12-11T21:15:21.4533333+00:00

    Barry,

    I was well aware that this example which I gave only has 362880 possibilities which is why I stated

    "Although for the example given below (2,9,4,7,5,3,6,1,8) there might be more simpler ways to get the results, for the case where this will applied I'll need to follow the steps below."

    Thanks for your insight in how to solve this problem.

    [Moderator note: Personal information removed]

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.