Run piece of code for each line of data
Try using CurrentRegion
Dim MyRng as Range
Dim cell as Range
Dim i as Integer
' This sets MyRng to the entire region or area of cells that contain data
and "touch" cell A5
Set MyRng = Range("A5").CurrentRegion
For Each cell In MyRng.Columns(1).Cells
cell.Select
For i = 0 to MyRng.Columns.Count
MsgBox ActiveCell.OffSet(0, i - 1).Value
Next i
Next cell
This will cycle through all cells in your range, where MyRng represents the
entire range of rows and columns. The outer loop just cycles through the
first cell in each row, and the inner loop cycles through all cells in the
row. The loop
For Each cell In MyRng.Cells
MsgBox cell.Value
Next cell
will cycle through all cells in the entire range also, but distinguishing
between rows is more difficult. The example I listed has a flaw in that if
one of the rows has no data in it whatsoever, then the CurrentRegion method
will fail.
There are lots of ways to loop through a range. Think about setting a range
variable to the entire range of data, and look at the Row.Count,
Column.Count and Offset properties and methods also.
HTH.
-gk-
"Diane Alsing" wrote in message
...
I have a spreadsheet that will have a varying number of rows of data. I
need
to write a macro that will (for each row of data) run my code until there
are
no more rows of data.
Also, my data rows do not start at the top of the sheet, the rows start at
row 5 so I guess I will need some type of offset or something like that.
While there's rows of data
- my code here -
End when there's no more data
Any help would be greatly appreciated.
|