View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
David David is offline
external usenet poster
 
Posts: 1,560
Default loop through ranges

The code below works fine but i'm guessing there will be a way to use 1 loop
instead of 3. If, say, i could have range names and symbol character names in
a 3 x 2 array and loop through the array.. ?? (i've tried and failed)

Sub PopulateTimeChart()
Dim TimeChart As Range ' grid populated with symbols to show timing of
various events
Dim RefurbDate As Range ' column range populated w/ years: 2012, 2020, etc
Dim ReplaceDate As Range ' ditto
Dim EnhanceDate As Range 'ditto
Dim EndDates As Range 'ditto
Dim DateHeaders As Range 'Timechart column headers populated with years:
2009, 2101, etc (sequentially, step 1 year)
Dim cl_1 As Range, cl_2 As Range, cl_3 As Range
Dim YearsRemaining As Integer, Period As Integer, i As Integer

Application.ScreenUpdating = False

Set TimeChart = Range("TimeChart")
Set RefurbDate = Range("RefurbDate")
Set ReplaceDate = Range("ReplaceDate")
Set EnhanceDate = Range("EnhanceDate")
Set EndDates = Range("EndDate")
Set DateHeaders = TimeChart.Offset(-1).Resize(1)

TimeChart.ClearContents

For Each cl_1 In EnhanceDate
If Not Val(cl_1) = 0 Then
For Each cl_2 In DateHeaders
If cl_2 = cl_1 Then
Set cl_3 = Intersect(cl_1.EntireRow, cl_2.EntireColumn)
Period = Val(cl_1.Offset(, 1)) 'may be zero
YearsRemaining = Intersect(cl_1.EntireRow, EndDates) -
cl_2
If Period = 0 Then
cl_3.Value = "u" 'diamond symbol
Else
For i = 0 To YearsRemaining Step Period
cl_3.Offset(, i).Value = "u" 'diamond symbol
Next i
End If
End If
Next cl_2
End If
Next cl_1

For Each cl_1 In RefurbDate
If Not Val(cl_1) = 0 Then
For Each cl_2 In DateHeaders
If cl_2 = cl_1 Then
Set cl_3 = Intersect(cl_1.EntireRow, cl_2.EntireColumn)
Period = Val(cl_1.Offset(, 1)) 'may be zero
YearsRemaining = Intersect(cl_1.EntireRow, EndDates) -
cl_2
If Period = 0 Then
cl_3.Value = "¬" 'star symbol
Else
For i = 0 To YearsRemaining Step Period
cl_3.Offset(, i).Value = "¬" 'star symbol
Next i
End If
End If
Next cl_2
End If
Next cl_1

For Each cl_1 In ReplaceDate
If Not Val(cl_1) = 0 Then
For Each cl_2 In DateHeaders
If cl_2 = cl_1 Then
Set cl_3 = Intersect(cl_1.EntireRow, cl_2.EntireColumn)
Period = Val(cl_1.Offset(, 1)) 'may be zero
YearsRemaining = Intersect(cl_1.EntireRow, EndDates) -
cl_2
If Period = 0 Then
cl_3.Value = "l" 'bullet symbol
Else
For i = 0 To YearsRemaining Step Period
cl_3.Offset(, i).Value = "l" 'bullet symbol
Next i
End If
End If
Next cl_2
End If
Next cl_1
End Sub

"Jacob Skaria" wrote:

Try the below. If this does not help post back with an example..and explain
what you are expecting..

Dim rngTemp As Range

Set rngTemp = Range("C5:D10")

For Each varRecord In rngTemp.Rows
For Each cell In varRecord.Cells
MsgBox cell.Address
Next
Next



If this post helps click Yes
---------------
Jacob Skaria


"David" wrote:

I'm suffering from brain freeeze at the moment...

Assuming i've already used Dim and Set, i have something like:

For Each cl in myRng1
Do something dependent on this range
Next cl
For Each cl in myRng2
Do something dependent on this range
Next cl

How could i loop through ranges something like:
for i = 1 to 2
for each cl in (what to use here?)
Do something (function of i)
Next cl
Next i

I can sort the (function of i) bit, it's looping through range names where
i'm stuck
Thanks