Microsoft Excel

4-character combinations, no repeated characters:-
Code:
[FONT=Fixedsys]Option Explicit[/FONT]
 
[FONT=Fixedsys]Sub CombineAtoZx4_NoR()[/FONT]
 
[FONT=Fixedsys]  Dim ws As Worksheet
  Dim n1 As Integer, n2 As Integer, n3 As Integer, n4 As Integer
  Dim iRow As Long, iColumn As Long, iCombinations As Long
  Dim dStart As Date
  
  Application.StatusBar = ""
  Application.ScreenUpdating = False
  
  dStart = Now()
  
  Set ws = ThisWorkbook.Sheets(1)
  ws.UsedRange.ClearContents
  
  iCombinations = 0
  iRow = 0
  iColumn = 1[/FONT]

[FONT=Fixedsys]  For n1 = Asc("A") To Asc("W")
    For n2 = n1 + 1 To Asc("X")
      For n3 = n2 + 1 To Asc("Y")
        For n4 = n3 + 1 To Asc("Z")
          iRow = iRow + 1
          If iRow > 2000 Then
            ws.Columns(iColumn).EntireColumn.AutoFit
            iColumn = iColumn + 1
            iRow = 1
            Application.StatusBar = Format(iCombinations, "#,###") & " combinations found in " _
                & Format(Now() - dStart, "hh:nn:ss")
            Application.ScreenUpdating = False
            Application.ScreenUpdating = True
         End If
          ws.Cells(iRow, iColumn) = Chr(n1) & Chr(n2) & Chr(n3) & Chr(n4)
          iCombinations = iCombinations + 1
        Next n4
      Next n3
    Next n2
  Next n1
  
  ws.Columns(iColumn).EntireColumn.AutoFit
  
  Application.ScreenUpdating = True
  
  MsgBox Format(iCombinations, "#,###") & " combinations found" & Space(10) & vbCrLf & vbCrLf _
       & "Run time: " & Format(Now() - dStart, "hh:nn:ss"), vbOKOnly + vbInformation[/FONT]
 
[FONT=Fixedsys]End Sub
[/FONT]
I really must be more precise when talking about combinations and permutations. When order matters, they're permutations; when it doesn't they're combinations.

So combination locks should really be called permutation locks!
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hey Mr. Ruddles,

This was exactly what I was looking for; you’ve been a great help. Thank you.

Question: Given that (A-Z) is 26 characters; would any 26 character numbers work in the place of letters. Example (1-26).

Is it is possible; can you send it over so I can take a look at it.
 
Upvote 0
Not with that code because "26" is two characters and the routine works by combining each possible single character from A to Z in the first position with each possible single character from A to Z in the second position with each possible single character from A to Z in the third position with each possible single character from A to Z in the fourth position.

Any 26 single characters would work, for example "°µ»½½¾ÂÇËÏÑÕØÞáæìñ÷þąĎĐÿĘğ", but they'd have to be single characters.

But anything's possible. Describe what you're trying to achieve - what the desired output will look like.
 
Upvote 0
Generally looking for sets Numbers as the need arise for my clients without the exact matching characters. Similar to what we’ve accomplished with the letter.

Range Example 1 : 1 ,2, 3, 4 ,5,6
= (1,2,3,4), (1,2,3,5),(1,2,3,6)
= (2,3,4,5),(2,3,4,6)
= (3, 4 ,5,6),

Range Example 2: 10, 11, 12, 13, 14, 15.
= (10,11,12,13), (10,11,12,14), (10,11,12,15).
= (11,12,13,14), (11,12,13,15).
= (12,13,14,15).
 
Upvote 0
I tried changing A-Z letter range to 1-26 number range since it is the same amount of characters but doing some research I saw that Chr return letters. I thought it was simple as changing Chr; allowing it to generate just as the letter did.
 
Upvote 0
Try this:
Code:
[FONT=Fixedsys]Option Explicit[/FONT]
[FONT=Fixedsys][/FONT] 
[FONT=Fixedsys]Sub CombineNumericX4()[/FONT]
[FONT=Fixedsys][/FONT] 
[FONT=Fixedsys]  Const StartNumber As Long = 10
  Const LastNumber As Long = 26[/FONT]
[FONT=Fixedsys]
  Const MaxRows As Long = 10000
  
  Dim ws As Worksheet
  Dim n1 As Integer, n2 As Integer, n3 As Integer, n4 As Integer
  Dim iRow As Long, iColumn As Long, iCombinations As Long
  Dim dStart As Date
  
  Application.StatusBar = ""
  Application.ScreenUpdating = False
  
  dStart = Now()
  
  Set ws = ThisWorkbook.Sheets(3)
  ws.UsedRange.ClearContents
  
  iCombinations = 0
  iRow = 0
  iColumn = 1
  For n1 = StartNumber To LastNumber - 3
    For n2 = n1 + 1 To LastNumber - 2
      For n3 = n2 + 1 To LastNumber - 1
        For n4 = n3 + 1 To LastNumber
          iRow = iRow + 1
          If iRow > MaxRows Then
            ws.Columns(iColumn).EntireColumn.AutoFit
            iColumn = iColumn + 1
            iRow = 1
            Application.StatusBar = Format(iCombinations, "#,###") & " combinations found in " _
                & Format(Now() - dStart, "hh:nn:ss")
            Application.ScreenUpdating = False
            Application.ScreenUpdating = True
         End If
          ws.Cells(iRow, iColumn) = CStr(n1) & "," & CStr(n2) & "," & CStr(n3) & "," & CStr(n4)
          iCombinations = iCombinations + 1
        Next n4
      Next n3
    Next n2
  Next n1
  
  ws.Columns(iColumn).EntireColumn.AutoFit
  
  Application.ScreenUpdating = True
  
  MsgBox Format(iCombinations, "#,###") & " combinations found" & Space(10) & vbCrLf & vbCrLf _
       & "Run time: " & Format(Now() - dStart, "hh:nn:ss"), vbOKOnly + vbInformation
[/FONT] 
[FONT=Fixedsys]End Sub[/FONT][FONT=Fixedsys]
[/FONT]
Change StartNumber and LastNumber to 1 & 6 or 10 & 15 to generate the sequences you describe.

Is that what you were after?
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,854
Members
452,948
Latest member
UsmanAli786

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