ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Cell References and Looping (https://www.excelbanter.com/excel-programming/278759-cell-references-looping.html)

KPaul

Cell References and Looping
 
I have a Looping question.

I have several rows of data to manipulate.. and want an
easy way to increment a cell range. Can I use a variable
in the Cell range name itself..? For instance, on each
pass I copy the values in A1, B1, C1, etc.. someplace else
and do something then increment to A2, B2, C2 etc...

Can I replace the (row component of the cell reference)
e.g. 1, 2, 3 with an integer variable so that I can write
a simple do while or for next loop?

J.E. McGimpsey

Cell References and Looping
 
One way, among many:

Dim i As Long
Dim destRange As Range

Set destRange = Sheets("Sheet2").Range("A1")
For i = 1 To 20
Cells(i, 1).Resize(1, 3).Copy _
Destination:=destRange(i, 1)
Next i

(You can substitute Range("A" & i) for Cells(i, 1) if you want.)

another:

Dim cell As Range
Dim destRange As Range
Set destRange = Sheets("Sheet2").Range("A1")
For Each cell In Range("A1:A20")
cell.Resize(1, 3).Copy destRange
Set destRange = destRange.Offset(1, 0)
Next cell


In article ,
"KPaul" wrote:

I have a Looping question.

I have several rows of data to manipulate.. and want an
easy way to increment a cell range. Can I use a variable
in the Cell range name itself..? For instance, on each
pass I copy the values in A1, B1, C1, etc.. someplace else
and do something then increment to A2, B2, C2 etc...

Can I replace the (row component of the cell reference)
e.g. 1, 2, 3 with an integer variable so that I can write
a simple do while or for next loop?


Tim[_25_]

Cell References and Looping
 
I think somehting like this is what you are looking for:

rownum = 1
do until rownum = 10
Range("A" & rownum) = somevalue
rownum = rownum + 1
loop


-----Original Message-----
I have a Looping question.

I have several rows of data to manipulate.. and want an
easy way to increment a cell range. Can I use a

variable
in the Cell range name itself..? For instance, on each
pass I copy the values in A1, B1, C1, etc.. someplace

else
and do something then increment to A2, B2, C2 etc...

Can I replace the (row component of the cell reference)
e.g. 1, 2, 3 with an integer variable so that I can

write
a simple do while or for next loop?
.



All times are GMT +1. The time now is 01:15 AM.

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