Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 178
Default converting a for next loop into a for each loop

I've read that a for each loop is more efficient when working with
large tables of data. Below is my for next loop that i've been using
to do some basic things. I am trying to convert it into a for each
loop. The difficulty i'm having is that as you can see i use my i
variable to identify the 'row' value for what cells i'm looking to
modify. How would i go about using the for each loop and get over this
hiccup?

Sub looop()
'define stale value
sstale = Date - 5
With Application
.ScreenUpdating = False
.EnableEvents = False
End With

'build time zone column
cells(1, "j").Value = "Time Zone"
With Range("j1")
.Font.Bold = True
.Borders(xlEdgeBottom).Weight = xlMedium
.Borders(xlEdgeTop).Weight = xlMedium
.Borders(xlEdgeLeft).Weight = xlMedium
.Borders(xlEdgeRight).Weight = xlMedium
End With

For i = 2 To cells(Rows.Count, "e").End(xlUp).Row

'determine which timezone the account is in. Column E holds the value
for state
If Not IsError(Application.Match(cells(i, "f").Value, Array("CA",
"WA", "OR", "NV"), 0)) Then
cells(i, "j").Value = "4-Pacific"
End If

If Not IsError(Application.Match(cells(i, "f").Value, Array("AZ",
"UT", "NM", "CO", "ID", "WY", "MT"), 0)) Then
cells(i, "j").Value = "3-Mountain"
End If

If Not IsError(Application.Match(cells(i, "f").Value, Array("TX",
"LA", "MS", "AL", "IL", "AR", "OK", "KS", "NE", "MO", _
"IA", "SD", "ND", "MN", "WI"), 0)) Then
cells(i, "j").Value = "2-Central"
End If

If Not IsError(Application.Match(cells(i, "f").Value, Array("DC",
"FL", "GA", "TN", "KY", "IN", "MI", "OH", "WV", "SC", "NC", _
"VA", "MD", "PA", "NY", "DE", "NJ", "CT", "RI", "MA", "VT", "NH",
"ME"), 0)) Then
cells(i, "j").Value = "1-East"
End If

If Not IsError(Application.Match(cells(i, "f").Value, Array("HI",
"AK"), 0)) Then
cells(i, "j").Value = "5-Other"
End If

'large bal format
If cells(i, "h") 19999 Then
cells(i, "h").Interior.ColorIndex = 3
cells(i, "h").Font.Bold = True
End If

'stale format
If cells(i, "i") <= sstale Then
cells(i, "i").Interior.ColorIndex = 36
cells(i, "i").Font.Bold = True
End If

'DC format
If cells(i, "k") = Date Then
cells(i, "k").Interior.ColorIndex = 4
cells(i, "k").Font.Bold = True

Else
cells(i, "k").ClearContents
End If

'active repo
If cells(i, "n") < "Y" Then
cells(i, "m").ClearContents
End If

'Early Pay Defualt
If cells(i, "l") < 6 Then
cells(i, "l").Interior.ColorIndex = 7
cells(i, "l").Font.Bold = True
End If

Next i
Columns("n").ClearContents

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default converting a for next loop into a for each loop

Matthew,
I don't recommend you go there in this case scenario. Doing so will
require using object ref syntax, a counter, and typing more code than
what you have already.

As for your assertion that 'For...Each...Next' is more efficient than
'For...Next', I disagree: the former adds to runtime overhead more than
the latter because of the addition 'dot' processing AND required use of
a Variant or Object type for the 'Each' object<IMO.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 178
Default converting a for next loop into a for each loop

On May 3, 11:53*am, GS wrote:
Matthew,
I don't recommend you go there in this case scenario. Doing so will
require using object ref syntax, a counter, and typing more code than
what you have already.

As for your assertion that 'For...Each...Next' is more efficient than
'For...Next', I disagree: the former adds to runtime overhead more than
the latter because of the addition 'dot' processing AND required use of
a Variant or Object type for the 'Each' object<IMO.

--
Garry

Free usenet access athttp://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


Thanks for the help Garry. Glad to know i'm not crazy.
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
returning back to loop check condition without completing the loop ashish128 Excel Programming 13 April 3rd 08 12:53 PM
Loop to Filter, Name Sheets. If Blank, Exit Loop ryguy7272 Excel Programming 3 February 5th 08 03:41 PM
Naming Worksheets - Loop within a loop issue klysell Excel Programming 0 March 27th 07 11:17 PM
(Complex) Loop within loop to create worksheets klysell Excel Programming 1 March 20th 07 12:03 AM
Advancing outer Loop Based on criteria of inner loop ExcelMonkey Excel Programming 1 August 15th 05 05:23 PM


All times are GMT +1. The time now is 09:57 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"