Hi, Can someone advise how to loop through all the worksheets and build an array of visible worksheets,
I need to select all the visible worksheets and try to add page setup to all of them.
It takes forever looping through each worksheet one at a time. So I am trying this method below.
Maybe someone has another way,
Thanks again for your help,
Mike
Sub PrintMe() Dim wks As Worksheet For Each wks In Worksheets If wks.Name = "Summary" Or wks.Name = "Raw" Or wks.Visible = xlSheetVeryHidden Then Else 'Add sheet to an array End If Next wks Sheets(Array(myarray)).Select ActiveWindow.View = xlPageBreakPreview ActiveSheet.VPageBreaks(1).DragOff Direction:=xlToRight, RegionIndex:=1 ActiveWindow.View = xlNormalView wks.PageSetup.PrintArea = RealUsedRange.Address With wks.PageSetup .PrintTitleRows = "$21:$21" .LeftMargin = Application.InchesToPoints(0.25) .RightMargin = Application.InchesToPoints(0.25) .TopMargin = Application.InchesToPoints(0.5) .BottomMargin = Application.InchesToPoints(0.5) .HeaderMargin = Application.InchesToPoints(0.5) .FooterMargin = Application.InchesToPoints(0.5) .CenterHorizontally = True .CenterVertically = False .Orientation = xlLandscape .BlackAndWhite = True .Zoom = 95 End With Range("A22").Select: ActiveWindow.FreezePanes = True Range("A1").Select: ActiveWindow.Zoom = True SendKeys "%f", True SendKeys "u" SendKeys "{ENTER}", True Sheets("Summary").Select End Sub
Last edited by realniceguy5000; 02-15-2012 at 10:39 AM.
may be so
Sub PrintMe() Dim myarray$(), i&, wks As Worksheet For Each wks In Worksheets If wks.Name = "Summary" Or wks.Name = "Raw" Or wks.Visible = xlSheetVeryHidden Then Else ReDim Preserve myarray(i) myarray(i) = wks.Name i = i + 1 End If Next wks Sheets(myarray).Select ...
I must be missing something - why do you want to select them, instead of just processing them in the loop?
Good luck.
@ nilem ... Thanks, The Array part works just fine , but the script only sets up the pagebreaks on the first sheet. I'm guessing this is a send key issue?
@ OnErrorGoto0 The reason I dont want to loop through every worksheet and setup the page info is it takes a lot of time compared to just selecting all the worksheets at once. The script is suppose to be able to select the one worksheet and it copies all the page setup info to all the other worksheets.
I'll keep messing with it for the time being, not sure what is wrong at this time.
Thank You,
Mike
SendKeys is always flaky. I would recommend using XLM instead - see here - since that will bypass the printer driver slowdown.
Good luck.
It's rather tricky to use usedrange as printarea.
Probably it's wise to reduce the sheets before doing any pagesetup.
The workbook cleaning will only have any effect after saving & reopening the file. that's why the macro closes the file.
Sub tst() For Each sh In Sheets For Each cl In sh.UsedRange.Columns x0 = Application.Max(x0, sh.Cells(Rows.Count, cl.Column).End(xlUp).Row) Next sh.Rows(x0 + 1).Resize(Rows.Count - x0 - 1).Delete For Each rw In sh.UsedRange.Rows x1 = Application.Max(x1, sh.Cells(rw.Row, Columns.Count).End(xlToLeft).Column) Next sh.Columns(x1 + 1).Resize(, Columns.Count - x1 - 1).Delete Next ActiveWorkbook.Close True End Sub
Last edited by snb; 02-15-2012 at 11:58 AM.
The array part works fine, however nothing but problems with the page setup. I have re-thought my script and removed that portion of the script to update page setup. I now have created a script that will insert a worksheet activate script from a text file. Which goes something like this:
Credit goes to Leith Ross and Shg who helped me out a long time ago with this type of method.
LoadTextFile "C:\Documents and Settings\ME\Desktop\AddText.txt"And the text file is located on my desktop which is named (AddText.txt)Sub LoadTextFile(ByVal TextFile As String, Optional OverWrite As Boolean) Dim Cnt As Long Dim CodeTxt As String Dim FSO As Object Dim vbProj As Object Dim VBComp As Object If Dir(TextFile) = "" Then MsgBox "File Not Found." & vbCrLf & TextFile, vbCritical Exit Sub End If Set vbProj = ActiveWorkbook.VBProject 'Application.VBE.ActiveVBProject Set VBComp = vbProj.VBComponents(wk.CodeName) Cnt = VBComp.CodeModule.CountOfLines 'Clear the Code Module If OverWrite = True Then VBComp.CodeModule.DeleteLines 1, Cnt End If 'Read in the Text file Set FSO = CreateObject("Scripting.FileSystemObject") CodeTxt = FSO.OpenTextFile(TextFile).ReadAll 'Load the Code Module with the file text VBComp.CodeModule.InsertLines Cnt + 1, CodeTxt 'Free objects Set FSO = Nothing Set vbProj = Nothing Set VBComp = Nothing End Sub
Here is whats in the text file: This method seems much faster and works so far with my tests.
Hope this may help someone else someday.PHP Code:
Private Sub Worksheet_Activate()
Application.ScreenUpdating = False
On Error GoTo exits:
ActiveWindow.View = xlPageBreakPreview
VPageBreaks(1).DragOff Direction:=xlToRight, RegionIndex:=1
ActiveWindow.View = xlNormalView
PageSetup.PrintArea = RealUsedRange.Address
With ActiveSheet.PageSetup
.PrintTitleRows = "$21:$21"
.LeftMargin = Application.InchesToPoints(0.25)
.RightMargin = Application.InchesToPoints(0.25)
.TopMargin = Application.InchesToPoints(0.5)
.BottomMargin = Application.InchesToPoints(0.5)
.HeaderMargin = Application.InchesToPoints(0.5)
.FooterMargin = Application.InchesToPoints(0.5)
.CenterHorizontally = True
.CenterVertically = False
.Orientation = xlLandscape
.BlackAndWhite = True
.Zoom = 95
End With
Range("A22").Select: ActiveWindow.FreezePanes = True
Range("A1").Select: ActiveWindow.Zoom = True
exits:
Application.ScreenUpdating = True
End Sub
Public Function RealUsedRange() As Range
Dim FirstRow As Long
Dim LastRow As Long
Dim FirstColumn As Integer
Dim LastColumn As Integer
On Error Resume Next
FirstRow = Cells.Find(What:="*", after:=Range("IV65536"), LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext).Row
FirstColumn = Cells.Find(What:="*", after:=Range("IV65536"), LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext).Column
LastRow = Cells.Find(What:="*", after:=Range("A1"), LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastColumn = Cells.Find(What:="*", after:=Range("A1"), LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Set RealUsedRange = Range(Cells(FirstRow, FirstColumn), Cells(LastRow, LastColumn))
On Error GoTo 0
End Function
Thank You, Mike
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks