Creating a triangle pattern using VBA in Excel can be a fun and educational exercise. Below is a simple guide on how to do it, along with the VBA code required to create a triangle pattern with the *
sign in an Excel worksheet.
Step 1: Open Excel and Access the VBA Editor
Alt + F11
to open the VBA Editor.Insert > Module
Step 2: Write the VBA Code
Use the following VBA code into the module:
Sub CreateTriangle()
Dim ws As Worksheet
Dim i As Integer, j As Integer
Dim rowCount As Integer
' Set the worksheet where the triangle will be created
Set ws = ThisWorkbook.Sheets("Sheet1")
' Clear the worksheet
ws.Cells.Clear
' Define the number of rows for the triangle
rowCount = 10
' Loop through rows and columns to create the triangle pattern
For i = 1 To rowCount
For j = 1 To i
ws.Cells(i, j).Value = "*"
Next j
Next i
ws.Columns.AutoFit
End Sub
Copy this entire VBA code and paste into your Excel worksheet.
- Press
F5
to run the code.
Step 4: View the Triangle Pattern
*
signs, starting from cell A1.Explanation of the Code
*
sign in the appropriate columns to form a triangle.rowCount
variable.*
, replace the *
in the line ws.Cells(i, j).Value = "*"
with your desired character.