#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default need a loop

hi
I have some code which searches a number of sheets, each
containing 9 names and 2 ranges of working dates. The cell
to the right of the names contains either Workplan1 or
Workplan2 which determines which range on that sheet is
searched.

There are about 17 sheets and I would like some code that
loops through all of them but I can't get it to work.
Instead I have to write a separate sub for each sheet,
calling the next one from the end of the previous.

eg:
Sub WORKSHEETS_A1()
With Worksheets("A1")
Set MYDSHEET = Sheet3
For COUNTER = 1 To 9
If .Cells(COUNTER, 4).Offset(0, 1) = "Work Plan 1"
Then
Set MYDRANGE = Sheet3.Range("A20:H82")
ElseIf .Cells(COUNTER, 4).Offset(0, 1) = "Work Plan
2" Then
Set MYDRANGE = Sheet3.Range("J20:Q82")
End If
MYdNAME = MYDSHEET.Cells(COUNTER, 4).Value
Call FINDDATE_A
Next COUNTER
End With
Call WORKSHEETS_A2
End Sub

As you can imagine, for 17 sheets it's quite a lot of code.
so I was hoping for something more like this

Dim WS As Integer
For WS = 1 To 17
With Worksheets(WS)
Set MYDSHEET = Worksheets(WS)
For COUNTER = 1 To 9
If .Cells(COUNTER, 4).Offset(0, 1) = "Work Plan 1"
Then
Set MYDRANGE = Sheet3.Range("A20:H82")
ElseIf .Cells(COUNTER, 4).Offset(0, 1) = "Work Plan
2" Then
Set MYDRANGE = Sheet3.Range("J20:Q82")
End If
MYdNAME = MYDSHEET.Cells(COUNTER, 4).Value
Call FINDDATE_A
Next COUNTER
End With
Next WS

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default need a loop

Denise,

Haven't tried it, but just re-cutting thye code

Dim WS As Integer
For WS = 1 To 17)
With Worksheets(WS)
For COUNTER = 1 To 9
If Cells(COUNTER, 4).Offset(0, 1) = "Work Plan 1" Then
Set MYDRANGE = .Range("A20:H82")
ElseIf .Cells(COUNTER, 4).Offset(0, 1) = "Work Plan 2"
Then
Set MYDRANGE = .Range("J20:Q82")
End If
MYdNAME = MYDSHEET.Cells(COUNTER, 4).Value
Call FINDDATE_A
Next COUNTER
End With
Next WS.


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"DENISE" wrote in message
...
hi
I have some code which searches a number of sheets, each
containing 9 names and 2 ranges of working dates. The cell
to the right of the names contains either Workplan1 or
Workplan2 which determines which range on that sheet is
searched.

There are about 17 sheets and I would like some code that
loops through all of them but I can't get it to work.
Instead I have to write a separate sub for each sheet,
calling the next one from the end of the previous.

eg:
Sub WORKSHEETS_A1()
With Worksheets("A1")
Set MYDSHEET = Sheet3
For COUNTER = 1 To 9
If .Cells(COUNTER, 4).Offset(0, 1) = "Work Plan 1"
Then
Set MYDRANGE = Sheet3.Range("A20:H82")
ElseIf .Cells(COUNTER, 4).Offset(0, 1) = "Work Plan
2" Then
Set MYDRANGE = Sheet3.Range("J20:Q82")
End If
MYdNAME = MYDSHEET.Cells(COUNTER, 4).Value
Call FINDDATE_A
Next COUNTER
End With
Call WORKSHEETS_A2
End Sub

As you can imagine, for 17 sheets it's quite a lot of code.
so I was hoping for something more like this

Dim WS As Integer
For WS = 1 To 17
With Worksheets(WS)
Set MYDSHEET = Worksheets(WS)
For COUNTER = 1 To 9
If .Cells(COUNTER, 4).Offset(0, 1) = "Work Plan 1"
Then
Set MYDRANGE = Sheet3.Range("A20:H82")
ElseIf .Cells(COUNTER, 4).Offset(0, 1) = "Work Plan
2" Then
Set MYDRANGE = Sheet3.Range("J20:Q82")
End If
MYdNAME = MYDSHEET.Cells(COUNTER, 4).Value
Call FINDDATE_A
Next COUNTER
End With
Next WS



  #3   Report Post  
Posted to microsoft.public.excel.programming
No Name
 
Posts: n/a
Default need a loop

you could try this. It works irrespective of the number of
sheets

myNum = Application.Worksheets.count
Do
. . . .your code here . . . .
myNum = myNum - 1
Loop Until myNum = 0


-----Original Message-----
hi
I have some code which searches a number of sheets, each
containing 9 names and 2 ranges of working dates. The

cell
to the right of the names contains either Workplan1 or
Workplan2 which determines which range on that sheet is
searched.

There are about 17 sheets and I would like some code that
loops through all of them but I can't get it to work.
Instead I have to write a separate sub for each sheet,
calling the next one from the end of the previous.

eg:
Sub WORKSHEETS_A1()
With Worksheets("A1")
Set MYDSHEET = Sheet3
For COUNTER = 1 To 9
If .Cells(COUNTER, 4).Offset(0, 1) = "Work Plan 1"
Then
Set MYDRANGE = Sheet3.Range("A20:H82")
ElseIf .Cells(COUNTER, 4).Offset(0, 1) = "Work Plan
2" Then
Set MYDRANGE = Sheet3.Range("J20:Q82")
End If
MYdNAME = MYDSHEET.Cells(COUNTER, 4).Value
Call FINDDATE_A
Next COUNTER
End With
Call WORKSHEETS_A2
End Sub

As you can imagine, for 17 sheets it's quite a lot of

code.
so I was hoping for something more like this

Dim WS As Integer
For WS = 1 To 17
With Worksheets(WS)
Set MYDSHEET = Worksheets(WS)
For COUNTER = 1 To 9
If .Cells(COUNTER, 4).Offset(0, 1) = "Work Plan 1"
Then
Set MYDRANGE = Sheet3.Range("A20:H82")
ElseIf .Cells(COUNTER, 4).Offset(0, 1) = "Work Plan
2" Then
Set MYDRANGE = Sheet3.Range("J20:Q82")
End If
MYdNAME = MYDSHEET.Cells(COUNTER, 4).Value
Call FINDDATE_A
Next COUNTER
End With
Next WS

.

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
Loop kevcar40 Excel Discussion (Misc queries) 1 June 22nd 11 04:02 PM
Loop Christina Excel Discussion (Misc queries) 6 March 23rd 10 12:04 AM
Find loop doesn't loop JSnow Excel Discussion (Misc queries) 2 June 24th 09 08:28 PM
help with a loop BeJay Excel Discussion (Misc queries) 3 May 19th 06 12:24 PM
HELP!!!! Can't stop a loop (NOT an infinite loop) TBA[_2_] Excel Programming 3 December 14th 03 03:33 PM


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