Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 49
Default multi Loop efficiency

Hi

I was looking for a more efficient way to loop through a number of job
codes.

They go from 301 to 310 and 401 to 410. I am repeating the same code
10 times to get the job done and I was hoping someone could help with
a more efficient method. Problem is I have to increment the name
range by 1 each time.

I only included 2 loops for demonstration purposes.

Thanks in advance
chad

Sub Multiloop()

Dim i As Long
Dim LRw As Long


If Range("AAA").Value = "Phase 301" Or Range("AAA").Value = "Phase
401" Then
LRw = Range("Y" & Rows.Count).End(xlUp).Row
For i = LRw To 1 Step -1
mys = Range("Y" & i).Value
Set FoundCell = Range("C:C").Find(What:=mys, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=0)
myint = FoundCell.Row 'Name range increment
Range("G" & myint + 1 & ":" & "U" & myint + 1).Name = "Test" &
"_" & myint + 1
End If
Next i

‘REPEAT CODE * 10 (NOT THAT EFFICIENT).

ElseIf Range("AAA").Value = "Phase 302" Or Range("AAA").Value =
"Phase 402" Then
LRw = Range("Y" & Rows.Count).End(xlUp).Row
For i = LRw To 1 Step -1
mys = Range("Y" & i).Value
Set FoundCell = Range("C:C").Find(What:=mys, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=0)
myint = FoundCell.Row 'Name range increment
Range("G" & myint + 2 & ":" & "U" & myint + 2).Name = "Test" &
"_" & myint + 2
Next i

End If

End sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default multi Loop efficiency

Hi

Try if this should do it:


Dim i As Long
Dim LRw As Long

For X = 1 To 10
TargetVal = 300 + X
TargetVal1 = 400 + X
If Range("AAA").Value = "Phase " & TargetVal Or Range("AAA").Value
= "Phase " & TargetVal1 Then
LRw = Range("Y" & Rows.Count).End(xlUp).Row
For i = LRw To 1 Step -1
mys = Range("Y" & i).Value
Set FoundCell = Range("C:C").Find(What:=mys, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=0)
myint = FoundCell.Row 'Name range increment
Range("G" & myint + X & ":" & "U" & myint + 1).Name = "Test" &
"_" & myint + X
End If
Next i
Next

Regards,
Per

On 20 Nov., 02:17, Chad wrote:
Hi

I was looking for a more efficient way to loop through a number of job
codes.

They go from 301 to 310 and 401 to 410. *I am repeating the same code
10 times to get the job done and I was hoping someone could help with
a more efficient method. *Problem is I have to increment the name
range by 1 each time.

I only included 2 loops for demonstration purposes.

Thanks in advance
chad

Sub Multiloop()

Dim i As Long
Dim LRw As Long

* *If Range("AAA").Value = "Phase 301" Or Range("AAA").Value = "Phase
401" Then
* * *LRw = Range("Y" & Rows.Count).End(xlUp).Row
* * *For i = LRw To 1 Step -1
* * * * mys = Range("Y" & i).Value
* * * * Set FoundCell = Range("C:C").Find(What:=mys, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=0)
* * * myint = FoundCell.Row 'Name range increment
* * * Range("G" & myint + 1 & ":" & "U" & myint + 1).Name = "Test" &
"_" & myint + 1
* * End If
* * Next i

‘REPEAT CODE * 10 (NOT THAT EFFICIENT).

* * ElseIf Range("AAA").Value = "Phase 302" Or Range("AAA").Value =
"Phase 402" Then
* * *LRw = Range("Y" & Rows.Count).End(xlUp).Row
* * *For i = LRw To 1 Step -1
* * * * mys = Range("Y" & i).Value
* * * * Set FoundCell = Range("C:C").Find(What:=mys, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=0)
* * * myint = FoundCell.Row 'Name range increment
* * * Range("G" & myint + 2 & ":" & "U" & myint + 2).Name = "Test" &
"_" & myint + 2
* * Next i

*End If

End sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default multi Loop efficiency

Hi

Just a typo, use this:

Range("G" & myint + X & ":" & "U" & myint + X).Name = "Test" & "_" &
myint + X

regards,
Per

On 20 Nov., 02:34, Per Jessen wrote:
Hi

Try if this should do it:

Dim i As Long
Dim LRw As Long

For X = 1 To 10
* * TargetVal = 300 + X
* * TargetVal1 = 400 + X
* *If Range("AAA").Value = "Phase " & TargetVal Or Range("AAA").Value
= "Phase " & TargetVal1 Then
* * *LRw = Range("Y" & Rows.Count).End(xlUp).Row
* * *For i = LRw To 1 Step -1
* * * * mys = Range("Y" & i).Value
* * * * Set FoundCell = Range("C:C").Find(What:=mys, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=0)
* * * myint = FoundCell.Row 'Name range increment
* * * Range("G" & myint + X & ":" & "U" & myint + 1).Name = "Test" &
"_" & myint + X
* * End If
* * Next i
Next

Regards,
Per

On 20 Nov., 02:17, Chad wrote:



Hi


I was looking for a more efficient way to loop through a number of job
codes.


They go from 301 to 310 and 401 to 410. *I am repeating the same code
10 times to get the job done and I was hoping someone could help with
a more efficient method. *Problem is I have to increment the name
range by 1 each time.


I only included 2 loops for demonstration purposes.


Thanks in advance
chad


Sub Multiloop()


Dim i As Long
Dim LRw As Long


* *If Range("AAA").Value = "Phase 301" Or Range("AAA").Value = "Phase
401" Then
* * *LRw = Range("Y" & Rows.Count).End(xlUp).Row
* * *For i = LRw To 1 Step -1
* * * * mys = Range("Y" & i).Value
* * * * Set FoundCell = Range("C:C").Find(What:=mys, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=0)
* * * myint = FoundCell.Row 'Name range increment
* * * Range("G" & myint + 1 & ":" & "U" & myint + 1).Name = "Test" &
"_" & myint + 1
* * End If
* * Next i


‘REPEAT CODE * 10 (NOT THAT EFFICIENT).


* * ElseIf Range("AAA").Value = "Phase 302" Or Range("AAA").Value =
"Phase 402" Then
* * *LRw = Range("Y" & Rows.Count).End(xlUp).Row
* * *For i = LRw To 1 Step -1
* * * * mys = Range("Y" & i).Value
* * * * Set FoundCell = Range("C:C").Find(What:=mys, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=0)
* * * myint = FoundCell.Row 'Name range increment
* * * Range("G" & myint + 2 & ":" & "U" & myint + 2).Name = "Test" &
"_" & myint + 2
* * Next i


*End If


End sub- Skjul tekst i anførselstegn -


- Vis tekst i anførselstegn -


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 49
Default multi Loop efficiency

Per

Thanks very muchh for your help. It certainly makes the full coding
shorter. Your code works very well with one very small exception. It
makes a named range for each of the scenarios 301 -310 and the same
for the 401-410 series. I was not clear on this so I am very sorry.

I have some code that deletes all the named ranges that start with
"Test" prior to the loop begining.

I need to make one named range if the cell is either 301 or 401. One
range if it is 302 or 402.

I then want to unlock this named range, then protect the sheet so we
can enter data against 301 but no where else on the sheet.

Here is the code in its entirety if this helps.
Chad

Sub Multiloop()

Dim FoundCell As Range
Dim mys As String
Dim myint As Integer
Dim i As Long
Dim LRw As Long
Dim nm As Name

Range("bid_phase").Value = Me.cmbBidPhase.Value
Range("bid_division").Value = Me.cmbBidDivision.Value

'Remove all named ranges starting with Test, to clean sheet.

‘Remove all the names starting with Test
For Each nm In ThisWorkbook.Names
If UCase(Left(nm.Name, 4)) = "TEST" Then
nm.Delete
End If
Next nm
'First part not related to the Per LOOP
LRw = Range("W" & Rows.Count).End(xlUp).Row
If Range("bid_phase").Value = "Phase 900" Then
For i = LRw To 1 Step -1
mys = Range("W" & i).Value
Set FoundCell = Range("B:B").Find(What:=mys, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=0)
If FoundCell Is Nothing Then
MsgBox "Not Found"
Else
myint = FoundCell.Row
' Range("B" & myint & ":" & "C" & myint).Interior.Color = vbBlue
Range("G" & myint & ":" & "U" & myint + 3).Name = "Test" & "_" &
myint
End If
Next i

'Per LOOP part where multiple ranges are created. Only one needed

Else
For X = 1 To 10
TargetVal = 300 + X
TargetVal1 = 400 + X
LRw = Range("Y" & Rows.Count).End(xlUp).Row
For i = LRw To 1 Step -1
mys = Range("Y" & i).Value
Set FoundCell = Range("C:C").Find(What:=mys, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=0)
myint = FoundCell.Row 'Name range increment
Range("G" & myint + X & ":" & "U" & myint + X).Name = "Test" &
"_" & myint + X

Next i
Next
End If

End Sub
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default multi Loop efficiency

Hi Chad

Thanks for your reply.

From what you write, I'm not sure how to determine which series should
be chosen and named.

Or maybe it's just me beeing tired..

Regards,
Per

On 20 Nov., 03:49, Chad wrote:
Per

Thanks very muchh for your help. *It certainly makes the full coding
shorter. *Your code works very well with one very small exception. *It
makes a named range for each of the scenarios 301 -310 and the same
for the 401-410 series. * I was not clear on this so I am very sorry.

I have some code that deletes all the named ranges that start with
"Test" prior to the loop begining.

I need to make one named range if the cell is either 301 or 401. * One
range if it is 302 or 402.

I then want to unlock this named range, then protect the sheet so we
can enter data against 301 but no where else on the sheet.

Here is the code in its entirety if this helps.
Chad

Sub Multiloop()

Dim FoundCell As Range
Dim mys As String
Dim myint As Integer
Dim i As Long
Dim LRw As Long
Dim nm As Name

Range("bid_phase").Value = Me.cmbBidPhase.Value
Range("bid_division").Value = Me.cmbBidDivision.Value

*'Remove all named ranges starting with Test, to clean sheet.

‘Remove all the names starting with Test
For Each nm In ThisWorkbook.Names
* * If UCase(Left(nm.Name, 4)) = "TEST" Then
* * * * nm.Delete
* * End If
*Next nm
*'First part not related to the Per LOOP
LRw = Range("W" & Rows.Count).End(xlUp).Row
If Range("bid_phase").Value = "Phase 900" Then
* For i = LRw To 1 Step -1
* * mys = Range("W" & i).Value
* * Set FoundCell = Range("B:B").Find(What:=mys, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=0)
* * If FoundCell Is Nothing Then
* * * MsgBox "Not Found"
* * Else
* * * myint = FoundCell.Row
* * ' *Range("B" & myint & ":" & "C" & myint).Interior.Color = vbBlue
* * * Range("G" & myint & ":" & "U" & myint + 3).Name = "Test" & "_" &
myint
* * End If
* Next i

* 'Per LOOP part where multiple ranges are created. *Only one needed

*Else
* *For X = 1 To 10
* * TargetVal = 300 + X
* * TargetVal1 = 400 + X
* * *LRw = Range("Y" & Rows.Count).End(xlUp).Row
* * *For i = LRw To 1 Step -1
* * * * mys = Range("Y" & i).Value
* * * * Set FoundCell = Range("C:C").Find(What:=mys, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=0)
* * * myint = FoundCell.Row 'Name range increment
* * * Range("G" & myint + X & ":" & "U" & myint + X).Name = "Test" &
"_" & myint + X

* * Next i
* Next
* End If

End Sub




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 49
Default multi Loop efficiency

Hi Per

Thanks for your help. I will go ahead and do it the long way but your
method would have been so much more elegant. Thanks again for your
help and call it a night now.

Cheers
Chad
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default multi Loop efficiency

A little more concise way to write that statement might be...

Cells(myint + X, "G").Resize(, 15).Name = "Test_" & myint + X

--
Rick (MVP - Excel)


"Per Jessen" wrote in message
...
Hi

Just a typo, use this:

Range("G" & myint + X & ":" & "U" & myint + X).Name = "Test" & "_" &
myint + X

regards,
Per

On 20 Nov., 02:34, Per Jessen wrote:
Hi

Try if this should do it:

Dim i As Long
Dim LRw As Long

For X = 1 To 10
TargetVal = 300 + X
TargetVal1 = 400 + X
If Range("AAA").Value = "Phase " & TargetVal Or Range("AAA").Value
= "Phase " & TargetVal1 Then
LRw = Range("Y" & Rows.Count).End(xlUp).Row
For i = LRw To 1 Step -1
mys = Range("Y" & i).Value
Set FoundCell = Range("C:C").Find(What:=mys, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=0)
myint = FoundCell.Row 'Name range increment
Range("G" & myint + X & ":" & "U" & myint + 1).Name = "Test" &
"_" & myint + X
End If
Next i
Next

Regards,
Per

On 20 Nov., 02:17, Chad wrote:



Hi


I was looking for a more efficient way to loop through a number of job
codes.


They go from 301 to 310 and 401 to 410. I am repeating the same code
10 times to get the job done and I was hoping someone could help with
a more efficient method. Problem is I have to increment the name
range by 1 each time.


I only included 2 loops for demonstration purposes.


Thanks in advance
chad


Sub Multiloop()


Dim i As Long
Dim LRw As Long


If Range("AAA").Value = "Phase 301" Or Range("AAA").Value = "Phase
401" Then
LRw = Range("Y" & Rows.Count).End(xlUp).Row
For i = LRw To 1 Step -1
mys = Range("Y" & i).Value
Set FoundCell = Range("C:C").Find(What:=mys, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=0)
myint = FoundCell.Row 'Name range increment
Range("G" & myint + 1 & ":" & "U" & myint + 1).Name = "Test" &
"_" & myint + 1
End If
Next i


‘REPEAT CODE * 10 (NOT THAT EFFICIENT).


ElseIf Range("AAA").Value = "Phase 302" Or Range("AAA").Value =
"Phase 402" Then
LRw = Range("Y" & Rows.Count).End(xlUp).Row
For i = LRw To 1 Step -1
mys = Range("Y" & i).Value
Set FoundCell = Range("C:C").Find(What:=mys, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=0)
myint = FoundCell.Row 'Name range increment
Range("G" & myint + 2 & ":" & "U" & myint + 2).Name = "Test" &
"_" & myint + 2
Next i


End If


End sub- Skjul tekst i anførselstegn -


- Vis tekst i anførselstegn -


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default multi Loop efficiency

Select Case Range("AAA").Value
Case Is = "Phase 301", "Phase401"
LRw = Range("Y" & Rows.Count).End(xlUp).Row
For i = LRw To 1 Step -1
mys = Range("Y" & i).Value
Set FoundCell = Range("C:C").Find(What:=mys,
LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=0)
myint = FoundCell.Row 'Name range increment
Range("G" & myint + 1 & ":" & "U" & myint + 1).Name = "Test"
& "_" & myint + 1
Next i
Case Is = "Phase 302", "Phase402"
LRw = Range("Y" & Rows.Count).End(xlUp).Row
For i = LRw To 1 Step -1
mys = Range("Y" & i).Value
Set FoundCell = Range("C:C").Find(What:=mys,
LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=0)
myint = FoundCell.Row 'Name range increment
Range("G" & myint + 2 & ":" & "U" & myint + 2).Name = "Test"
& "_" & myint + 2
Next i
End Select
End Sub

"Chad" wrote:

Hi

I was looking for a more efficient way to loop through a number of job
codes.

They go from 301 to 310 and 401 to 410. I am repeating the same code
10 times to get the job done and I was hoping someone could help with
a more efficient method. Problem is I have to increment the name
range by 1 each time.

I only included 2 loops for demonstration purposes.

Thanks in advance
chad

Sub Multiloop()

Dim i As Long
Dim LRw As Long


If Range("AAA").Value = "Phase 301" Or Range("AAA").Value = "Phase
401" Then
LRw = Range("Y" & Rows.Count).End(xlUp).Row
For i = LRw To 1 Step -1
mys = Range("Y" & i).Value
Set FoundCell = Range("C:C").Find(What:=mys, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=0)
myint = FoundCell.Row 'Name range increment
Range("G" & myint + 1 & ":" & "U" & myint + 1).Name = "Test" &
"_" & myint + 1
End If
Next i

€˜REPEAT CODE * 10 (NOT THAT EFFICIENT).

ElseIf Range("AAA").Value = "Phase 302" Or Range("AAA").Value =
"Phase 402" Then
LRw = Range("Y" & Rows.Count).End(xlUp).Row
For i = LRw To 1 Step -1
mys = Range("Y" & i).Value
Set FoundCell = Range("C:C").Find(What:=mys, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=0)
myint = FoundCell.Row 'Name range increment
Range("G" & myint + 2 & ":" & "U" & myint + 2).Name = "Test" &
"_" & myint + 2
Next i

End If

End sub

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 loop through multi-selection listbox? dan dungan Excel Programming 17 April 25th 08 10:14 PM
efficiency Carlee Excel Programming 3 September 24th 07 09:14 AM
Do..Loop in multi sheets norika Excel Programming 7 May 26th 05 07:44 AM
VBA Efficiency Question Brian Excel Programming 4 March 26th 05 04:10 PM
.select efficiency Lawlera Excel Programming 4 February 4th 04 02:38 PM


All times are GMT +1. The time now is 11:10 PM.

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"