+ Reply to Thread
Results 1 to 4 of 4

Formula for 12345

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-15-2014
    Location
    Michigan, USA
    MS-Off Ver
    MS Office 2021
    Posts
    105

    Formula for 12345

    Is there a formula that would give me all the possible combination of 5 digits.
    In column A I have the Area A B C D E. Each area has a number in column B. Column D is not needed but it show the possible #. Column E-I shows the Digit position. And J shows the all the next to each other in one number (I have the formula worked out for J)
    Is there a way to auto figure all the possible number combinations without me sitting and type every possible combination hoping I got them all and didn't miss any.
    Attached Files Attached Files

  2. #2
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Formula for 12345

    You can do this quickly with a little bit of VBA.

    First, let's calculate the actual number of permutations: =PERMUT(5,5) = 120.

    To list them, we use the UDF below.

    We select E2:I121 since we know there are 121 permutations to cover. Next we enter =ListPermut(5) and use Ctrl+Shift+Enter to exit the cell and apply the array formula to all cells simultaneously.


    Function ListPermut(num As Integer)
    
    Dim c As Long, r As Long, p As Long
    Dim rng() As Long, temp As Long, i As Long
    Dim temp1 As Long, y() As Long, d As Long
    
    p = WorksheetFunction.Permut(num, num)
    
    
    ReDim rng(1 To p, 1 To num)
    For c = 1 To num
      rng(1, c) = c
    Next c
    For r = 2 To p
      For c = num To 1 Step -1
        If rng(r - 1, c - 1) < rng(r - 1, c) Then
         temp = c - 1
          Exit For
        End If
      Next c
      For c = num To 1 Step -1
        rng(r, c) = rng(r - 1, c)
      Next c
      For c = num To 1 Step -1
          If rng(r - 1, c) > rng(r - 1, temp) Then
             temp1 = rng(r - 1, temp)
              rng(r, temp) = rng(r - 1, c)
              rng(r, c) = temp1
              ReDim y(num - temp)
              i = 0
              For d = temp + 1 To num
                y(i) = rng(r, d)
                i = i + 1
              Next d
              i = 0
              For d = num To temp + 1 Step -1
                rng(r, d) = y(i)
                i = i + 1
              Next d
              Exit For
          End If
      Next c
    Next r
    ListPermut = rng
    End Function




    How to install your new code
    1. Copy the Excel VBA code
    2. Select the workbook in which you want to store the Excel VBA code
    3. Press Alt+F11 to open the Visual Basic Editor
    4. Choose Insert > Module
    5. Edit > Paste the macro into the module that appeared
    6. Close the VBEditor
    7. Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)
    Attached Files Attached Files
    Make Mom proud: Add to my reputation if I helped out!

    Make the Moderators happy: Mark the Thread as Solved if your question was answered!

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Formula for 12345

    UDF?

    A
    B
    1
    12345
    2
    12345 A2:A121: {=asPermute(A1)}
    3
    12354
    4
    12435
    5
    12453
    6
    12534
    7
    12543
    8
    13245
    115
    53421
    116
    54123
    117
    54132
    118
    54213
    119
    54231
    120
    54312
    121
    54321


    Function asPermute(sInp As String) As String()
      ' shg 2015
      ' UDF wrapper for dicPermute
    
      ' Returns the distinct uppercase permutations of sInp
      ' See combinMississippi.bas for a more robust approach
    
      Dim dic           As Dictionary
      Dim asOut()       As String
      Dim v             As Variant
      Dim i             As Long
    
      Set dic = New Dictionary
      dicPermute "", UCase(sInp), dic, 0
    
      ReDim asOut(1 To dic.Count, 1 To 1)
      For Each v In dic.Keys
        i = i + 1
        asOut(i, 1) = v
      Next v
      asPermute = asOut
    End Function
    
    Function dicPermute(sL As String, sR As String, dic As Dictionary, nOut As Long)
      ' Adapted from http://spreadsheetpage.com/index.php/site/tip/generating_permutations/
    
      Dim i             As Integer
      Dim j             As Integer
    
      j = Len(sR)
      If j <= 1 Then
    
        If Not dic.Exists(sL & sR) Then
          nOut = nOut + 1
          dic.Add Key:=sL & sR, Item:=""
          ' Debug.Print nOut
          DoEvents
        End If
      Else
        For i = 1 To j
          dicPermute sL & Mid(sR, i, 1), Left(sR, i - 1) & Right(sR, j - i), dic, nOut
        Next
      End If
    End Function
    Requires a reference to Microsoft Scripting Runtime.
    Last edited by shg; 11-03-2016 at 08:40 PM.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Formula for 12345

    shg code is best code

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. if a cell in column P contains "12345" then delete entire row
    By justlearning123 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-19-2015, 10:55 AM
  2. How to enter "12345", where 12345 is from a cell, in Excel formula?
    By Ning in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-07-2013, 01:27 PM
  3. urgent! save excel to csv, 012345 will change to 12345!!
    By counterking in forum Excel General
    Replies: 1
    Last Post: 08-07-2012, 09:30 AM
  4. Change 12345 into 123,45
    By Washington in forum Excel General
    Replies: 3
    Last Post: 05-01-2007, 04:42 AM
  5. [SOLVED] import /paste -stop automatic conversion of text 05-12345 to date
    By artech in forum Excel General
    Replies: 1
    Last Post: 08-20-2006, 04:15 PM
  6. formating the number from 12345.1 to 12345.001
    By sr25 in forum Excel General
    Replies: 7
    Last Post: 11-29-2005, 11:55 AM
  7. convert 12345(figure) into word
    By Hirak Chatterjee in forum Excel General
    Replies: 6
    Last Post: 11-06-2005, 03:00 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1