October 30, 2018

Create Report with Loops in VBA in Hindi

How to create report with loops in VBA in Hindi. We describe how to use a ‘for next’ loop to extract data and place it in a new sheet to create a report. Watch the video below:

Watch this video on YouTube.

Here’s the complete VBA code to create reports using a ‘for next’ loop:

Sub createReportUsingForLoop()

Dim i As Long, lastrow As Long, year As Long
Dim classification As String

lastrow = Sheets(“Sheet1”).Range(“A” & Rows.Count).End(xlUp).Row

year = Application.InputBox(“Enter the year.”, “Enter Year”)
classification = Application.InputBox(“Enter the classification code.”, “Enter Classification Code”)

Application.ScreenUpdating = False
‘Start the loop
For i = 2 To lastrow
‘Look for matching year and classification code
If Cells(i, 1) = year And Cells(i, 12) = classification Then
‘copy and paste data in sheet2 if it contains matching year and classification code

‘Find the first empty row in Sheet2
erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
‘Paste the data here
Worksheets(“Sheet1”).Range(Cells(i, 1), Cells(i, 12)).Copy Destination:=Sheets(“sheet2”).Cells(erow, 1)
End If

‘Loop through the other rows with data
Next i

Application.ScreenUpdating = True
Application.CutCopyMode = False

End Sub

Download a sample file for practice: