Sunday, 28 July 2024

How to Create a Triangle in Excel Using VBA: Step-by-Step Guide

 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.


See in the image, we want to create this kind of Triangle in Excel with the help of VBA.

Use the following steps to go there.

Step 1: Open Excel and Access the VBA Editor

  • 1.  Open Excel.
  • 2.  Press Alt + F11 to open the VBA Editor.
  • 3. In the VBA Editor, insert a new module by clicking 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.

    1. Press F5 to run the code.

    Step 4: View the Triangle Pattern


    Go back to your Excel worksheet (usually named "Sheet1"). You should now see a triangle pattern made of * signs, starting from cell A1.


    Explanation of the Code

  • Set ws: Defines the worksheet where the triangle will be created.
  • ws.Cells.Clear: Clears any existing content on the worksheet to start fresh.
  • rowCount = 10: Sets the number of rows for the triangle. You can change this value to create a l arger or smaller triangle.
  • Nested For Loops: The outer loop runs through each row, and the inner loop places the * sign in the appropriate columns to form a triangle.
  • ws.Columns.AutoFit: Automatically adjusts the width of all columns to fit the content.

  • Note:- To change the size of the triangle, modify the rowCount variable.

    If you want to use a different character instead of *, replace the * in the line ws.Cells(i, j).Value = "*" with your desired character.


    With these steps, you can easily create a triangle pattern in Excel using VBA and ensure the columns are autofit to display the pattern neatly. Feel free to experiment with different shapes and patterns to enhance your VBA skills!







    Sunday, 21 August 2022

    VBA Macro to Split Text Into Multiple Columns in Excel (In Hindi)

    Splitting text into multiple columns into one of the best methods is Text to Columns.
    This feature has multiple options to split our text into columns.
    In this video, you will learn about 'VBA Macro to Split Text Into Multiple Columns in Excel.
    We can have our data with space or semi-colon or many more characters to decide our first or second or last cell value in a single column.



    Featured post

    Pivot Tables