Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 208
Default using For each loop to populate a range

i'm trying to use a For Each loop but I can't get it to work. I don't think
that the counter is necessary but i'm not sure how this should be written.
Any help streamlining this would be appreciated.

Static counter As Integer
For Each Row In Range("TotalData")
ImplementPlace = Worksheets("Implementation
Summary").Range("ImplementPlace").Cells(2, 1)
counter = counter + 1
If Worksheets("Post-Assessment").Range("TotalData").Cells(counter,
15) = "True" Then
If Worksheets("Post-Assessment").Range("TotalData").Cells(counter,
10) = "True" Then
Worksheets("Implementation
Summary").Range("ImplementMeth").Cells(ImplementPl ace, 2) = "S"
Else
Worksheets("Implementation
Summary").Range("ImplementMeth").Cells(ImplementPl ace, 2) = "IR"
End If
Worksheets("Implementation
Summary").Range("ImplementMeth").Cells(ImplementPl ace, 3) =
Worksheets("Post-Assessment").Range("TotalData").Cells(counter, 2)
Worksheets("Implementation
Summary").Range("ImplementMeth").Cells(ImplementPl ace, 4) =
Worksheets("Post-Assessment").Range("TotalData").Cells(counter, 7)
End If

Next Row
  #2   Report Post  
Posted to microsoft.public.excel.programming
MDW MDW is offline
external usenet poster
 
Posts: 117
Default using For each loop to populate a range

Try something like this:

lngTotalRows = Range("TotalData").Rows.Count

For counter = 1 To lngTotalRows

' Your code here

Next

No need to include the line "counter = counter + 1"

"Sean" wrote:

i'm trying to use a For Each loop but I can't get it to work. I don't think
that the counter is necessary but i'm not sure how this should be written.
Any help streamlining this would be appreciated.

Static counter As Integer
For Each Row In Range("TotalData")
ImplementPlace = Worksheets("Implementation
Summary").Range("ImplementPlace").Cells(2, 1)
counter = counter + 1
If Worksheets("Post-Assessment").Range("TotalData").Cells(counter,
15) = "True" Then
If Worksheets("Post-Assessment").Range("TotalData").Cells(counter,
10) = "True" Then
Worksheets("Implementation
Summary").Range("ImplementMeth").Cells(ImplementPl ace, 2) = "S"
Else
Worksheets("Implementation
Summary").Range("ImplementMeth").Cells(ImplementPl ace, 2) = "IR"
End If
Worksheets("Implementation
Summary").Range("ImplementMeth").Cells(ImplementPl ace, 3) =
Worksheets("Post-Assessment").Range("TotalData").Cells(counter, 2)
Worksheets("Implementation
Summary").Range("ImplementMeth").Cells(ImplementPl ace, 4) =
Worksheets("Post-Assessment").Range("TotalData").Cells(counter, 7)
End If

Next Row

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default using For each loop to populate a range

Hi,

I don't think the main problem is your use of the "For Each" loop, I think
there are other elements that will not allow your code to work.
1. Why do you declare the counter variable as a class variable?
2. I don't understand your use of the Range object. Usually you would have
an expression like "Range("e1")" to refer to the cell E1, or "Range("e1,
f2")" to refer to the group of cells that spans from E1 to F2, or even
"Range(rngName)" if you have written the coordinates into a string variable
(i.e. rngName="e2"), but not a string that is not the reference to a cell or
cell range.
3. And of course if you're using "For Each" you do not need any counter that
specifies the next cell, as that is done automatically. The idea behind the
"For Each" statement is to do away with using a counter, increasing it and
checking whether the last item has been reached. In your code you are using
both forms at the same time.

Regards,

Alex


"Sean" wrote:

i'm trying to use a For Each loop but I can't get it to work. I don't think
that the counter is necessary but i'm not sure how this should be written.
Any help streamlining this would be appreciated.

Static counter As Integer
For Each Row In Range("TotalData")
ImplementPlace = Worksheets("Implementation
Summary").Range("ImplementPlace").Cells(2, 1)
counter = counter + 1
If Worksheets("Post-Assessment").Range("TotalData").Cells(counter,
15) = "True" Then
If Worksheets("Post-Assessment").Range("TotalData").Cells(counter,
10) = "True" Then
Worksheets("Implementation
Summary").Range("ImplementMeth").Cells(ImplementPl ace, 2) = "S"
Else
Worksheets("Implementation
Summary").Range("ImplementMeth").Cells(ImplementPl ace, 2) = "IR"
End If
Worksheets("Implementation
Summary").Range("ImplementMeth").Cells(ImplementPl ace, 3) =
Worksheets("Post-Assessment").Range("TotalData").Cells(counter, 2)
Worksheets("Implementation
Summary").Range("ImplementMeth").Cells(ImplementPl ace, 4) =
Worksheets("Post-Assessment").Range("TotalData").Cells(counter, 7)
End If

Next Row

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Delete blanks between a range and populate only the names inthe given range Yuvraj Excel Discussion (Misc queries) 2 November 4th 09 08:32 PM
Populate cell range lost and confused in excel-land[_2_] Excel Worksheet Functions 1 July 13th 08 07:07 AM
Populate a table with a dynamic range Jeff Excel Worksheet Functions 3 February 22nd 07 06:47 AM
Increasing a range in a loop ric Excel Programming 4 April 26th 04 07:59 PM
Need Code to Populate Cell Range Kevin Beckham Excel Programming 0 April 14th 04 03:13 PM


All times are GMT +1. The time now is 12:31 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"