+ Reply to Thread
Results 1 to 7 of 7

Thread: Build an Array of visible worksheets

  1. #1
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    PA
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    926

    Build an Array of visible worksheets

    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.

  2. #2
    Valued Forum Contributor
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    Excel 2010
    Posts
    522

    Re: Build an Array of visible worksheets

    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
    ...

  3. #3
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Build an Array of visible worksheets

    I must be missing something - why do you want to select them, instead of just processing them in the loop?
    Good luck.

  4. #4
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    PA
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    926

    Re: Build an Array of visible worksheets

    @ 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

  5. #5
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Build an Array of visible worksheets

    SendKeys is always flaky. I would recommend using XLM instead - see here - since that will bypass the printer driver slowdown.
    Good luck.

  6. #6
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Build an Array of visible worksheets

    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.



  7. #7
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    PA
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    926

    Re: Build an Array of visible worksheets

    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"
    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
    And the text file is located on my desktop which is named (AddText.txt)

    Here is whats in the text file: This method seems much faster and works so far with my tests.
    PHP Code: 

    Private Sub Worksheet_Activate()

    Application.ScreenUpdating False
    On Error GoTo exits
    :

            
            
    ActiveWindow.View xlPageBreakPreview
            VPageBreaks
    (1).DragOff Direction:=xlToRightRegionIndex:=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:=xlValuesLookAt:= _
        xlPart
    SearchOrder:=xlByRowsSearchDirection:=xlNext).Row
         
        FirstColumn 
    Cells.Find(What:="*"after:=Range("IV65536"), LookIn:=xlValuesLookAt:= _
        xlPart
    SearchOrder:=xlByColumnsSearchDirection:=xlNext).Column
         
        LastRow 
    Cells.Find(What:="*"after:=Range("A1"), LookIn:=xlValuesLookAt:= _
        xlPart
    SearchOrder:=xlByRowsSearchDirection:=xlPrevious).Row
         
        LastColumn 
    Cells.Find(What:="*"after:=Range("A1"), LookIn:=xlValuesLookAt:= _
        xlPart
    SearchOrder:=xlByColumnsSearchDirection:=xlPrevious).Column
         
        Set RealUsedRange 
    Range(Cells(FirstRowFirstColumn), Cells(LastRowLastColumn))
         
        
    On Error GoTo 0
         
    End 
    Function 
    Hope this may help someone else someday.

    Thank You, Mike

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0