+ Reply to Thread
Results 1 to 3 of 3

Looping accross all worksheets in a workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    04-21-2011
    Location
    Columbia, MD
    MS-Off Ver
    Excel 2003
    Posts
    3

    Question Looping accross all worksheets in a workbook

    I can only get my code to execute on the first worksheet in the workbook. I've tried to follow several suggestions that I've found online and in books. I'm stumped. It works great one worksheet at a time, but stops after the first worksheet. Can anyone help? I attached the document I'm working with, if that helps....

    I have the following:

    Sub ProfileFormat6()
    
    Application.ScreenUpdating = False
    Dim rng As Range
    Dim ws As Worksheet
        
        
        For Each ws In Worksheets
            
                Set rng = Range("A1")
                rng.Activate
                
                Columns("E:E").Select
                ActiveWindow.SelectedSheets.VPageBreaks.Add Before:=ActiveCell
        
                    Do While rng.Value <> ""
                        
                        rng.Activate
                        
                        ActiveCell.Offset(1, 0).Range("A1:D1").Select
                            With Selection.Interior
                                .ColorIndex = 15
                                .Pattern = xlSolid
                            End With
                        rng.Activate
                        ActiveCell.Offset(21, 0).Range("A1:D2").Select
                            With Selection.Interior
                                .ColorIndex = 15
                                .Pattern = xlSolid
                            End With
                        rng.Activate
                        ActiveCell.Offset(31, 0).Activate
                        ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
                        
                        rng.Activate
                        Set rng = rng.Offset(31, 0)
                    Loop
                    
        Next ws
    
    End Sub
    Attached Files Attached Files
    Last edited by gspencer; 04-21-2011 at 04:08 PM. Reason: Needed to add code tags... I'm very new here!! thanks!

  2. #2
    Registered User
    Join Date
    04-21-2011
    Location
    Columbia, MD
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Looping accross all worksheets in a workbook

    Thanks for the tip. I think I added the code tags correctly. I'm new to VBA, and this site.. I appreciate the help SO MUCH! Thanks!

  3. #3
    Registered User
    Join Date
    04-21-2011
    Location
    Columbia, MD
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Looping accross all worksheets in a workbook

    I was just reading more past posts. I got an idea from a previous post.

    I added one line to top of my code to select the worksheet.

    ws.Select
    Now my code looks like this and seems to be working:

    Sub ProfileFormat6()
    
    Application.ScreenUpdating = False
    Dim rng As Range
    Dim ws As Worksheet
        
        
        For Each ws In Worksheets
            
                ws.Select
                Set rng = Range("A1")
                rng.Activate
                
                Columns("E:E").Select
                ActiveWindow.SelectedSheets.VPageBreaks.Add Before:=ActiveCell
        
                    Do While rng.Value <> ""
                        
                        rng.Activate
                        
                        ActiveCell.Offset(1, 0).Range("A1:D1").Select
                            With Selection.Interior
                                .ColorIndex = 15
                                .Pattern = xlSolid
                            End With
                        rng.Activate
                        ActiveCell.Offset(21, 0).Range("A1:D2").Select
                            With Selection.Interior
                                .ColorIndex = 15
                                .Pattern = xlSolid
                            End With
                        rng.Activate
                        ActiveCell.Offset(31, 0).Activate
                        ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
                        
                        rng.Activate
                        Set rng = rng.Offset(31, 0)
                    Loop
                    
        Next ws
    
    End Sub
    This is my first day reading through this forum and I think I'm in love. Thanks!

+ 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.6.0 RC 1