ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Walking Through Cells Using a Loop (https://www.excelbanter.com/excel-programming/340221-walking-through-cells-using-loop.html)

Negnog

Walking Through Cells Using a Loop
 
BEGINNER'S QUESTION:
I am creating an application in Excel that will use a block of cells for
display - 9 rows and 9 columns - A1-A9, B1-B9, ..., I1-I9. In several
places, I would like to look at or write to each cell using a FOR loop in VBA:

FOR row=1 to 9
FOR col=1 TO 9
write to cell at (row, col)
NEXT col
NEXT row

How do I create cell references dynamically in a loop based on "row" and
"col"? Is there a better way to programatically cycle through a series of
cells other than using FOR loops?

Thank you.


Dennis W. Bulgrien

Walking Through Cells Using a Loop, Cells()
 
Try ActiveWindow.ActiveSheet.Cells(row, col) or just Cells(row,col).

"Negnog" wrote in message
...

How do I create cell references dynamically in a loop based on "row" and
"col"? Is there a better way to programatically cycle through a series of
cells other than using FOR loops?

Thank you.



Chip Pearson

Walking Through Cells Using a Loop
 
Use

Cells(row,col).Value = 123

As an aside, 'row' is a reserved word in Excel/VBA, and good
programming practice would preclude it from being used as a
variable name. Use 'RowNum' or something similar instead.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Negnog" wrote in message
...
BEGINNER'S QUESTION:
I am creating an application in Excel that will use a block of
cells for
display - 9 rows and 9 columns - A1-A9, B1-B9, ..., I1-I9. In
several
places, I would like to look at or write to each cell using a
FOR loop in VBA:

FOR row=1 to 9
FOR col=1 TO 9
write to cell at (row, col)
NEXT col
NEXT row

How do I create cell references dynamically in a loop based on
"row" and
"col"? Is there a better way to programatically cycle through
a series of
cells other than using FOR loops?

Thank you.




bigwheel

Walking Through Cells Using a Loop
 
Cells(Row, col) = value to write to cell

"Negnog" wrote:

BEGINNER'S QUESTION:
I am creating an application in Excel that will use a block of cells for
display - 9 rows and 9 columns - A1-A9, B1-B9, ..., I1-I9. In several
places, I would like to look at or write to each cell using a FOR loop in VBA:

FOR row=1 to 9
FOR col=1 TO 9
write to cell at (row, col)
NEXT col
NEXT row

How do I create cell references dynamically in a loop based on "row" and
"col"? Is there a better way to programatically cycle through a series of
cells other than using FOR loops?

Thank you.


Dennis W. Bulgrien

Walking Through Cells Using a Loop, For Loops
 
For loops are good. You might look at For Each of a Range object, example:
Dim c
For Each c In ActiveWindow.RangeSelection
Next c

"Negnog" wrote in message
...

"col"? Is there a better way to programatically cycle through a series of
cells other than using FOR loops?




All times are GMT +1. The time now is 02:00 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com