I looking to create an excel table which represents a binary sequence of up to 20 places, i.e. 2^20. I've looked into using the excel formula dec2bin, unfortunately it only produces a binary sequence up to 10 places, i.e. 2^10. I need to produce a binary sequence which is bigger.
I've had a stab at coding this up in vba, and suffer from a couple of problems when trying to solve the problem at small scale. First, my code produces a lot of duplicates. For example, when setting my table to 3 places, I produce 28 results when I should only get 8. Second, my code is pretty slow.
Any hints or tips for how I can produce a more robust table, at a quicker speed would be much appreciated!! And here is the code, at small scale I have been using...
Sub BinarySequence()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim x As Integer
Dim Length As Integer
Application.ScreenUpdating = False
'Define 1st scenario
x = 1
Range("Start").Value = x 'where "Start" is defined as cell A1
'set default range
Length = Range("Sizei") 'where "Sizei" is defined as 3'
For i = 1 To Length
Range("start").Offset(0, i).Value = 1
Next
'code to generate first level binary sequence (i loop)
For i = 1 To Length
'code to generate second level binary sequence (j loop)
For j = 1 To Length
'code to generate third level binary sequence (k loop)
For k = 1 To Length
x = x + 1
Range("Start").Offset(0, i).Value = 0
Range("Start").Offset(0, j).Value = 0
Range("Start").Offset(0, k).Value = 0
'copy and paste scenario number
Range("Start").Offset(x - 1, 0).Value = x
'copy and paste result
Range("Result").Select 'where result is defined as row 1
Selection.Copy
Range("Result").Offset(x - 1, 0).Select
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'reset scenario select for next loop
Range("start").Offset(0, k).Value = 1
Next k
'reset scenario select for next loop
Range("start").Offset(0, j).Value = 1
Next j
'reset scenario select for next loop
Range("Start").Offset(0, i).Value = 1
Next i
Application.ScreenUpdating = True
End Sub