How To Generate Pascal Triangle Variant Using VBA

In this tutorial, I will show a similar kind of trick to generate a variant of Pascal Triangle which seems to be a mix of forward and backward triangles. I am going to show how the output will look like.


Sub pascal_variation()
Dim n As Integer
n = InputBox("Enter the Number for Triangle")
Dim i As Integer
Dim j As Integer
For i = 1 To (n + 1) / 2 Step 1
For j = (n + 3 - 2 * i) / 2 To (n + 2 * i - 1) / 2 Step 1
'Print Space((n + 1 - 2 * i) / 2) + String(j, i)
Cells(i, j).Select
Cells(i, j).Value = 2 * i - 1
Next j
Next i
End Sub


This code works perfectly for all numbers which are odd.For example, when you click the Generate Triangle button in the excel workbook provided with this tutorial, you should only input odd numbers, otherwise you won’t be able to make a symmetric triangle.

Apart from this, you need to click the warning button displayed in excel to enable macros. In order to use the code for your work, you need to change the module name and also the file names based on your requirement. A link back to this post will be highly appreciated if you use the code.

Leave a Reply

Your email address will not be published.