m,

The macro below will generate - exactly - the matrices that you posted. I think that the pattern is
followed for larger n, but that is something that you can test easily enough. Simply run the macro,
and respond to the query with the size. Note that you cannot create a matrix larger than n = 11, or
you will run out of rows on the sheet.

Also note that while the macro places the values onto the spreadsheet, the code could be re-written
to either place the values into an array within VBA, or to read the cells into the array after the
matrix has been generated. The code was written to allow you to call it easily from a different
subroutine - see the test macro for details.

HTH,
Bernie
MS Excel MVP

Option Explicit

Sub test()
Dim mySize As Integer

mySize = Application.InputBox("What n do you want to do?", _
"Matrix Creation", , , , , , 1)
MakeMatrix mySize
End Sub

Sub MakeMatrix(n As Integer)
Dim i As Integer
Dim j As Integer
Dim myRow As Long
Dim myCol As Integer
Dim myCell As Range
Dim NotDone As Boolean
Dim myMax As Integer
Dim myMaxC As Integer

Set myCell = ActiveCell

'Create First 2 Rows
For myCol = 1 To n
myCell(1, myCol).Value = 1
myCell(2, myCol).Value = 1
Next myCol
myCell(2, n).Value = 2

myRow = 2
NotDone = False

For myCol = 1 To n - 1
If myCell(myRow, myCol).Value <> _
myCell(myRow, myCol + 1).Value - 1 Then
NotDone = True
End If
Next myCol

While NotDone

myCell(myRow, 1).Resize(1, n).Copy myCell(myRow + 1, 1)
myRow = myRow + 1

myMax = Application.Max(myCell(myRow, 1).Resize(1, n))
myMaxC = Application.CountIf(myCell(myRow, 1).Resize(1, n), myMax)

If myMaxC = 1 And myCell(myRow, n).Value = myMax Then
For i = n - 1 To 2 Step -1
If myCell(myRow, i).Value < myCell(myRow, i + 1).Value - 1 Then
myCell(myRow, i).Value = myCell(myRow, i).Value + 1
For j = i + 1 To n
myCell(myRow, j).Value = 1
Next j
GoTo Changed:
End If

If myCell(myRow, i).Value = myCell(myRow, i - 1).Value Then
myCell(myRow, i).Value = myCell(myRow, i).Value + 1
For j = i + 1 To n
myCell(myRow, j).Value = 1
Next j
GoTo Changed:
End If
Next i
End If

If myCell(myRow, n).Value <> myMax And myMaxC = 1 Then
myCell(myRow, n).Value = myCell(myRow, n).Value + 1
GoTo Changed:
End If

If myCell(myRow, n).Value <> myMax Then
myCell(myRow, n).Value = myCell(myRow, n).Value + 1
GoTo Changed:
End If

If myCell(myRow, n).Value = myMax And myMaxC <> 1 Then
myCell(myRow, n).Value = myCell(myRow, n).Value + 1
GoTo Changed:
End If

If myCell(myRow, n).Value = myMax And myMaxC = 1 Then
myCell(myRow, n - 1).Value = myCell(myRow, n - 1).Value + 1
myCell(myRow, n).Value = 1
GoTo Changed:
End If

Changed:

'Check Again
NotDone = False
For myCol = 1 To n - 1
If myCell(myRow, myCol).Value <> _
myCell(myRow, myCol + 1).Value - 1 Then
NotDone = True
End If
Next myCol

Wend

End Sub




"jiyed, m" <mjiyed@gmail.com> wrote in message
news:8ED2BEDE-5B1F-4B0E-9541-667D53C1AD99@microsoft.com...
> many thanks bernie for interest
> examples:
> For n = 2, matrix is:
> 1 1
> 1 2
>
>
> For n = 3, matrix is:
> 1 1 1
> 1 1 2
> 1 2 1
> 1 2 2
> 1 2 3
>
>
> For n = 4, matrix is:
> 1 1 1 1
> 1 1 1 2
> 1 1 2 1
> 1 1 2 2
> 1 1 2 3
> 1 2 1 1
> 1 2 1 2
> 1 2 1 3
> 1 2 2 1
> 1 2 2 2
> 1 2 2 3
> 1 2 3 1
> 1 2 3 2
> 1 2 3 3
> 1 2 3 4
>
>
> For n = 5, matrix is :
> 1 1 1 1 1
> 1 1 1 1 2
> 1 1 1 2 1
> 1 1 1 2 2
> 1 1 1 2 3
> 1 1 2 1 1
> 1 1 2 1 2
> 1 1 2 1 3
> 1 1 2 2 1
> 1 1 2 2 2
> 1 1 2 2 3
> 1 1 2 3 1
> 1 1 2 3 2
> 1 1 2 3 3
> 1 1 2 3 4
> 1 2 1 1 1
> 1 2 1 1 2
> 1 2 1 1 3
> 1 2 1 2 1
> 1 2 1 2 2
> 1 2 1 2 3
> 1 2 1 3 1
> 1 2 1 3 2
> 1 2 1 3 3
> 1 2 1 3 4
> 1 2 2 1 1
> 1 2 2 1 2
> 1 2 2 1 3
> 1 2 2 2 1
> 1 2 2 2 2
> 1 2 2 2 3
> 1 2 2 3 1
> 1 2 2 3 2
> 1 2 2 3 3
> 1 2 2 3 4
> 1 2 3 1 1
> 1 2 3 1 2
> 1 2 3 1 3
> 1 2 3 1 4
> 1 2 3 2 1
> 1 2 3 2 2
> 1 2 3 2 3
> 1 2 3 2 4
> 1 2 3 3 1
> 1 2 3 3 2
> 1 2 3 3 3
> 1 2 3 3 4
> 1 2 3 4 1
> 1 2 3 4 2
> 1 2 3 4 3
> 1 2 3 4 4
> 1 2 3 4 5
>