Loop through an entire column with Excel VBA

This example is similar to the one that looked for a specific term using the WHILE WEND statement (looking for bad donuts rating in a column) but it does loop through every cell in a column in Excel.

Here , we will make a program in Excel VBA that loops through an endless range or entire column (starting in B4). In that example, we will look for specific data in an endless range in column B

We will be using the FOR TO NEXT statement and the ROW.COUNT variable that gives the number of rows in a sheet.

.

The code for this macro looks like this.
Sub entire_rows()

' looking for the bad donuts
bad_donuts = 0

For i = 3 To Rows.Count

donuts_quality = Cells(i, 3) ' looks at the second column

If donuts_quality = "bad" Then bad_donuts = bad_donuts + 1

Next i

' final quality statement
Range("E5").Value = "There were " & bad_donuts & " bad donuts in total"

End Sub



VBA loop entire column range Loop Through Every Cell in a Column

You can see the in the cell "E5", the judgement of how many bad donuts where found by the VBA macro.



 

Congratulation, you made it until the end of this VBA Code Example.