Do Until Loop

H

Hargrove

I'd like the following loop to exit when there are no additional sheets
availble. Please help.

Do Until ActiveSheet.Next = False

Windows("Primary GBT Prod_Breakdown " & MyValue & ".xls").Activate
Selection.Copy
Windows("Agency Prime Gross Plotting by Placement " & MyValue &
".xls").Activate
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Windows("Primary GBT Prod_Breakdown " & MyValue & ".xls").Activate
ActiveCell.Offset(22, 0).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Agency Prime Gross Plotting by Placement " & MyValue &
".xls").Activate
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
For b = 1 To 11
Windows("Primary GBT Prod_Breakdown " & MyValue & ".xls").Activate
ActiveCell.Offset(0, 1).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Agency Prime Gross Plotting by Placement " & MyValue &
".xls").Activate
ActiveCell.Offset(-1, 2).Range("A1").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Next b
ActiveCell.Offset(64, -23).Range("A1").Select
Windows("Primary GBT Prod_Breakdown " & MyValue & ".xls").Activate
ActiveSheet.Next.Select

Loop
 
D

Doug Glancy

Hargrove,

Instead of a do loop, I think you're better off using a For Each Next.
Here's a simple example that excludes the first sheet:

Sub test()

Dim sh As Worksheet

For Each sh In ThisWorkbook.Worksheets
If sh.Name <> "Sheet1" Then
MsgBox sh.Name
End If
Next sh

End Sub

hth,

Doug Glancy
 
M

Myrna Larson

If you want to keep your existing code, you could use error trapping to exit
the loop. Notice there are no conditions on either the Do or Loop statements.

Do
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Wilcard for Workbooks??? 1
works in 2007 but not in 2010 0
stopping a macro 0
Help merging two VBA codes 2
Help again!!! 1
lenghty code 4
simplify date input 0
Windows error 4

Top