HOW TO PRINT PYRAMID PATTERN “*” USING VBA CODE

Share Now


Problem Statement

Now today we are going to create pyramid using VBA. This is not a reporting problem statement but in some of the organizations in an interview they ask this question to understand the programming capabilities
This is an interesting nested loop exercise

Approach

  • Will use two loops here, one to jump rows and second to print “*”.
  • In second section we have also adjusted column width using autofit method
Excel VBA Python Gurgaon

SOLUTION


Sub PrintPyramid()
Dim i As Integer
Dim j As Integer
Application.ScreenUpdating = False
For i = 1 To 10
For j = 10 - i + 1 To 9 + i
Cells(i, j).Value = "*"
Next
Next
'Autofit Columns
Range("j1").CurrentRegion.Select
Selection.Columns.AutoFit
Range("j1").Select
Application.ScreenUpdating = True


End Sub

STEPS TO PERFORM

  • Go to developer Tab else Press Alt + F11
  • Click on Visual Basic icon
  • Go to Insert click on Module
  • Paste below code in standard module


Share Now
November 14, 2019

0 responses on "HOW TO PRINT PYRAMID PATTERN “*” USING VBA CODE"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Tableau Training in Delhi

    All Rights Reserved. EC Analytics Consulting 2014 - 2019.

    NM 23, Sector 14, OLD DLF Colony - Gurgaon (Haryana) India. 9582876837 | Privacy Policy