Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Lookup / Loop / Help Requested

Works as seen below:

Sub Breakdown()
Dim i As Long
Dim a As Integer
Application.Calculation = xlCalculationManual
'Application.ScreenUpdating = False
a = 0
LastDataRow = Sheets("Data").UsedRange.Rows.Count
LastRow = Sheets("Material").UsedRange.Rows.Count
For a = 2 To LastDataRow
For i = 3 To LastRow
If Sheets("Data").Range("B" & a) = Sheets("Material").Range("D" & i)
And Sheets("Data").Range("C" & a) = Sheets("Material").Range("E2")
Then
Sheets("Material").Range("E" & i) = (Sheets("Data").Range("D" & a) +
Sheets("Data").Range("E" & a)) / Sheets("Data").Range("E" & a)
GoTo NextData:
End If
If Sheets("Data").Range("B" & a) = Sheets("Material").Range("D" & i)
And Sheets("Data").Range("C" & a) = Sheets("Material").Range("F2")
Then
Sheets("Material").Range("F" & i) = (Sheets("Data").Range("D" & a) +
Sheets("Data").Range("E" & a)) / Sheets("Data").Range("E" & a)
GoTo NextData:
End If
Next
NextData:
Next
a = 0
'Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

I am looking for suggestions into how to optimize my loops so I am not
looping through so much data unnecessarily. Range E2:AF2 on the
materials sheet are the dates from 2/1/07 to 2/28/07. I limited what
is seen to two dates only but would like to loop through them all.
The current macro works, just very slowly and when I watch the loops I
can't figure out how to keep from looping when it isn't necessary. I
am trying to make noe pass over the data and fill in the appropriate
spot on the materials sheet. I am considering using an array for the
E2:AF2 range but ran into some trouble implementing so far. Any and
all help or suggestions are appeciated.

Thanks!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Lookup / Loop / Help Requested

I replaced th GOTO statement with Exit For. don't know if the goto are
slowing the execution. Aviod using GOTO.

To get out of a double for loop you can add a boolean

MyExit = False
for i = 1 to 5

for j = 3 to 8

if a < b then
Myexit = True
Exit for
exit if
Next j
if Myexit = true then exit for
Next i


Sub Breakdown()
Dim i As Long
Dim a As Integer
Application.Calculation = xlCalculationManual
'Application.ScreenUpdating = False
a = 0
LastDataRow = Sheets("Data").UsedRange.Rows.Count
LastRow = Sheets("Material").UsedRange.Rows.Count
For a = 2 To LastDataRow
For i = 3 To LastRow
If Sheets("Data").Range("B" & a) = _
Sheets("Material").Range("D" & i) _
And Sheets("Data").Range("C" & a) = _
Sheets("Material").Range("E2") Then

Sheets("Material").Range("E" & i) = (Sheets("Data").Range("D"
& a) + _
Sheets("Data").Range("E" & a)) / Sheets("Data").Range("E" & a)
Exit For
End If
If Sheets("Data").Range("B" & a) = _
Sheets("Material").Range("D" & i) _
And Sheets("Data").Range("C" & a) =
Sheets("Material").Range("F2")
Then
Sheets("Material").Range("F" & i) = (Sheets("Data").Range("D"
& a) + _
Sheets("Data").Range("E" & a)) / Sheets("Data").Range("E" & a)
Exit for
End If
Next
Next
a = 0
'Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

I am looking for suggestions into how to optimize my loops so I am not
looping through so much data unnecessarily. Range E2:AF2 on the
materials sheet are the dates from 2/1/07 to 2/28/07. I limited what
is seen to two dates only but would like to loop through them all.
The current macro works, just very slowly and when I watch the loops I
can't figure out how to keep from looping when it isn't necessary. I
am trying to make noe pass over the data and fill in the appropriate
spot on the materials sheet. I am considering using an array for the
E2:AF2 range but ran into some trouble implementing so far. Any and
all help or suggestions are appeciated.

Thanks!


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
Formula help requested [email protected] Excel Worksheet Functions 5 June 25th 08 07:04 PM
Lookup / Loop / Optimization Help Requested [email protected] Excel Programming 0 March 1st 07 07:52 PM
Help Requested kirkm[_6_] Excel Programming 4 August 15th 06 02:53 AM
A challenge, Serious help requested dps9460 New Users to Excel 11 June 3rd 06 09:46 PM
Conditional Lookup and copy loop between worksheets David S[_3_] Excel Programming 1 September 30th 03 02:10 AM


All times are GMT +1. The time now is 07:12 PM.

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

About Us

"It's about Microsoft Excel"