Results 1 to 5 of 5

Thread: Looping through worksheets

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    19

    Looping through worksheets

    Hi,
    I was wondering if someone can post a code... taht would essentially loop through all the worksheets in an Excel file.
    thanks.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Looping through worksheets

    this should be in Office section..

    but
    VB Code:
    1. For x = 1 to ActiveWorkbook.Sheets.Count
    2.      Msgbox Sheets(x).name
    3. Next
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    19

    Re: Looping through worksheets

    THe essentially automatically places footers to all documents in a directory... So what your code does is loop through the files and and visually shows that. I need the program to loop throuhg the worksheets and automatically put in the footer. This is the code I have:

    ub MacroUpdateFooter()
    '
    ' MacroUpdateFooter Macro
    ' Macro recorded 7/28/2006 by Blue2man
    '

    Dim tmp As String
    Dim ext As String
    Dim XLS As Variant
    Dim sPath As String

    sPath = "c:\baseline\"

    tmp = Dir(sPath & "*.*")
    Do While tmp > ""
    ext = LCase(Right(tmp, 3))
    Select Case ext
    Case "xls"
    Set XLS = New Application
    XLS.Workbooks.Open sPath & tmp
    XLS.ActiveSheet.PageSetup.PrintArea = ""
    With XLS.ActiveSheet.PageSetup
    .LeftFooter = "&D"
    .CenterFooter = "&Z&F"
    .RightFooter = "&P"
    End With
    XLS.ActiveWorkbook.Save
    XLS.Quit
    Set XLS = Nothing
    For x = 1 To ActiveWorkbook.Sheets.Count
    MsgBox Sheets(x).Name
    Next

    Case "doc"
    'Set WRD = New Word.Application
    'WRD.Documents.Open sPath & tmp
    'etc
    'etc
    'WRD.ActiveDocument.Save
    'RD.Quit
    'Set WRD = Nothing
    Case "ppt"

    Case "???" ' visio

    End Select
    tmp = Dir
    Loop
    MsgBox "Job Complete"

    End Sub

    Ignore the Case DOc...

  4. #4
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Looping through worksheets

    Use a For...Each loop. The other thing you should probably do is avoid references to ActiveBook and ActiveSheet. These can be disasterous if the user accidentally hits an Excel instance on the taskbar when your code is running.
    VB Code:
    1. Dim oSheet As Excel.Worksheet, oBook As Excel.Workbook
    2.     '....
    3.     Case "xls"
    4.         Set XLS = New Application
    5.         Set oBook = XLS.Workbooks.Open(sPath & tmp)
    6.         For Each oSheet In oBook.Worksheets
    7.             With oSheet.PageSetup
    8.                 .PrintArea = ""
    9.                 .LeftFooter = "&D"
    10.                 .CenterFooter = "&Z&F"
    11.                 .RightFooter = "&P"
    12.             End With
    13.         Next oSheet
    14.         oBook.Save
    15.         Set oBook = Nothing
    16.         XLS.Quit
    17.         Set XLS = Nothing
    18.         'Etc

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Looping through worksheets

    Moved to Office Development

Posting Permissions

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



Click Here to Expand Forum to Full Width