+ Reply to Thread
Results 1 to 10 of 10

VBA Loop

Hybrid View

  1. #1
    Registered User
    Join Date
    04-22-2014
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    23

    VBA Loop

    I have the code written for one worksheet but how do I write it so that it will loop through all of the worksheets? With the exception of one worksheet. I want to focus on the name of the worksheet so that my macro doesn't run there. Any help at all would be greatly appreciated!

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA Loop

    Like this:

    Sub RunSheets()
        Dim ws As Worksheet
                      
        For Each ws In Worksheets
        If ws.name = "Forbidden_Name" Then GoTo GetNext
        'Your code here
    GetNext:     Next: End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: VBA Loop

    Can you post the code you have?
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    04-22-2014
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: VBA Loop

    Dim ws As Worksheet
    For Each ws In Worksheets
    If ws.Name = "Tested Party" Then GoTo GetNext
    Range("G8").Select
    ActiveCell.FormulaR1C1 = "2014"
    GetNext: Next: End Sub

  5. #5
    Registered User
    Join Date
    04-22-2014
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: VBA Loop

    JoeFoot,

    Thank you for the code, I think I'm close but it's still only running the macro in the sheet i'm currently in. How can I run the macro in every sheet except the sheet named "Tested Party"?

  6. #6
    Registered User
    Join Date
    04-22-2014
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: VBA Loop

    Dim ws As Worksheet
    For Each ws In Worksheets
    If ws.Name = "Tested Party" Then GoTo GetNext
    Range("G8").Select
    ActiveCell.FormulaR1C1 = "2014"
    GetNext: Next: End Sub

  7. #7
    Registered User
    Join Date
    06-12-2015
    Location
    Maryland, USA
    MS-Off Ver
    2010
    Posts
    83

    Re: VBA Loop

    I would use:

    Dim ws As Worksheet
    
    For Each ws In ActiveWorkbook.Worksheets
     
       If ws.Name <> "Tested Party" Then
          Range("G8").Select
          ActiveCell.FormulaR1C1 = "2014"
       End If
    
    Next ws
    that way no need to use the GoTo instruction

  8. #8
    Registered User
    Join Date
    06-12-2015
    Location
    Maryland, USA
    MS-Off Ver
    2010
    Posts
    83

    Re: VBA Loop

    you need to select the sheet at each iteration. This way the code runs on each sheet except "Tested Party"

    Dim ws As Worksheet
    
    For Each ws In ActiveWorkbook.Worksheets
     
       If ws.Name <> "Tested Party" Then
           ws.select
           Range("G8").Select
           ActiveCell.FormulaR1C1 = "2014"
       End If
    
    Next ws

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: VBA Loop

    You don't need to select each sheet, and you don't need to select the cell either.
    Dim ws As Worksheet
    
    For Each ws In ActiveWorkbook.Worksheets
     
       If ws.Name <> "Tested Party" Then
          ws.Range("G8").Value = "2014"
       End If
    
    Next ws

  10. #10
    Registered User
    Join Date
    04-22-2014
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: VBA Loop

    I got it! Thanks so much guys

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Loop through multiple files and call macros (but unable to loop)
    By ryanpetersen in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-23-2014, 12:04 PM
  2. [SOLVED] Copy dynamically changing column and Paste using VBA Loop (Loop within Loop)
    By nixon72 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-12-2013, 12:46 PM
  3. Why did an inner loop variable start overwriting the outer loop range suddenly?
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-16-2012, 03:24 PM
  4. Replies: 0
    Last Post: 07-20-2010, 11:42 AM
  5. Loop a column on Sheet1 and loop a row on Sheet2 to find a match
    By johnnywinter in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-13-2009, 02:09 PM

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