Notice: WC_Cart::get_cart_url is deprecated since version 2.5! Use wc_get_cart_url instead. in /home/swatipaliwal/public_html/excelcrazy.com/wp-includes/functions.php on line 3839
Excel Crazy

HOW TO PRINT PYRAMID PATTERN “*” USING VBA CODE

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
January 1, 2018

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 *

top
 

© LOKESH PALIWAL 1987 - 2017

Reporting Solutions | Data Consulting | Analytics Training