Excel VBA For Next Loop Function | Excel Help
Your Programming and Traning Professionals

Excel VBA For Next Loop Function


The For Next Loop in Excel is a favorite function of ours here.  We use this For Nextfunction to loop through data sets and find certain info depending on whether a variable is equal to a certain criteria.

The basics:

Your parameters are created within a For and Next statement, beginning and end respectively.

	For currentrow = 1 to Activesheet.Usedrange.Rows.Count
		Next

Next, set a variable that will allow the loop to cycle through the cells between the first row and the last used row in the sheet, and find out if your criteria is populated by that variable.

For currentrow = 1 to Activesheet.Usedrange.Rows.Count
Fillvariable = activesheet.cells(currentrow, 5).value  (the value 5 is the column number)
Next

Add an IF statement for the logic to determine if the criteria in the variable matches what you are looking for.

For currentrow = 1 to Activesheet.Usedrange.Rows.Count
Fillvariable = activesheet.cells(currentrow, 5).value ('the value 5 is the column number)

IF Fillvariable = "123456" Then
	Msgbox "You found 123456!"
End If

Next

The currentrow acts like your row and will cycle down until your loop hits the last filled value of the spreadsheet.