Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default For Next Looping (custom variables)

I often use For Next Loops to loop through routines:

For X = 1 to 10

I know that you can also step through the loop based on a constraint:

For X = 1 to 10, Step 2 (you get 2 4 6 8 10)

But is it possible to set up the loop to get say (1,5,6,8,10). That
is I want to predefine the loop based on a custom order. I am
assuming that I have to read the range into some sort of array and
then give the user the option to custom pick. I imagine a userform is
needed.

And is it possible to do this with sheets in Excel. So if I have a
print macro which prints predefined sheets within a loop, can I set up
another loop which reads the number of sheets into my for Next
statement and allows me to custom pick the ones I want to print within
the greater loop? That is I pick the sheets in advance, and my macro
prints these chosen sheets with its loop.

Thanks in advance!

TS
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 55
Default For Next Looping (custom variables)

So, if I understand you correctly, you would like a macro that allows
you to define the order of printing of the sheets and then prints them
in that order. If so, here's what you can do:

1. This code will list all the sheets and ask you to input a print
order number in the cell to the right of that sheet name:

Sub ListSheets()

Dim intSht As Integer

Application.ScreenUpdating = False

' Create new sheet
On Error Resume Next
Sheets("Sheet List").Activate
If Err.Number < 0 Then
Sheets.Add
ActiveSheet.Name = "Sheet List"
Else
Sheets("Sheet List").Cells.Delete
End If
Err.Clear
On Error GoTo 0

' Create headings
Range("A1").Value = "Sheet Name"
Range("B1").Value = "Print Order"

' List sheets (except Sheet List)
intSht = 2
For Each sht In Sheets
If sht.Name < "Sheet List" Then
Range("A" & intSht).Value = sht.Name
intSht = intSht + 1
End If
Next sht

ActiveSheet.Cells.EntireColumn.AutoFit

Application.ScreenUpdating = True

End Sub

2. Once you have determined the print order and typed it in, run this
macro.

Sub PrintInOrder()

Application.ScreenUpdating = False

' Make sure the list is there.
On Error Resume Next
Sheets("Sheet List").Activate
If Err.Number < 0 Then
MsgBox ("Please run the ListSheets subroutine first.")
Exit Sub
End If
' Make sure the print order is input.
If WorksheetFunction.CountA(Range("B:B")) <
WorksheetFunction.CountA(Range("A:A")) Then
MsgBox ("You must input all print orders. Please do so and
re-run this macro.")
Exit Sub
End If
On Error GoTo 0

Range("A:B").Sort Key1:=Range("B:B"), Order1:=xlAscending,
Header:=xlYes

For x = 2 To ActiveSheet.UsedRange.SpecialCells(xlLastCell).Row
Sheets(Range("A" & x).Value).Print
Next x

Application.DisplayAlerts = False
Sheets("Sheet List").Delete
Application.DisplayAlerts = True

Application.ScreenUpdating = True

End Sub

If you have any problems, please let me know.

Mark

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
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
Looping Through Variables NigelShaw Excel Discussion (Misc queries) 6 October 26th 08 10:42 PM
Looping Maggie[_6_] Excel Discussion (Misc queries) 6 October 2nd 08 09:14 PM
Not at all clear on use of variables and/or object variables JMay-Rke Excel Discussion (Misc queries) 11 July 4th 08 06:36 PM
Not Looping Roger Excel Discussion (Misc queries) 0 February 26th 08 05:18 PM
Looping David T Excel Discussion (Misc queries) 2 August 30th 06 10:51 PM


All times are GMT +1. The time now is 06:55 AM.

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"