New Excel Forum

This forum has been moved to TeachExcel.com

Ask all future questions in the New Excel Forum.

ExcelKey

Loop Through Every Cell in a Column in Excel

Free Excel Macros

Loop Through Every Cell in a Column in Excel

Postby Don » Tue Jan 31, 2012 10:25 pm

This Excel macro allows you to easily loop through every cell in a column within a worksheet. This will only loop through a column if there is data in the column.

The benefit of doing this is that it makes it very easy to check the contents of each cell in a column. All you have to do is to reference the value of a cell using something like "Cells(i, 1).Value" The "i" is a variable used in the For loop that will increment up by one every time. And, the "1" is the number of the column being looped-through, in this case column A. If you changed this to work on column B, this number would be "2" and column C "3" and so on.

If you want to change the below macro to work on another column, follow the instructions in the last paragraph and also change the letter "A" in this part of the code

For i = 1 To Range("A" & Rows.Count).End(xlUp).Row

to the letter of the column through which you want to run the loop.


Where to install the macro: Module


Code: Select all
Sub Loop_Through_Column()

For i = 1 To Range("A" & Rows.Count).End(xlUp).Row

    'put your code here
   
    'if you want to reference the value of each cell within the column/loop, do something like this:
    '
    '   Cells(i, 1).Value
    '
   
Next i

End Sub
Don
Moderator
 
Posts: 733
Joined: Dec 4, 2011
Reputation: 2
Excel Version: 2010

Return to Excel Macros

Who is online

Users browsing this forum: No registered users and 24 guests