For Each Worksheet in... not moving to next worksheet.

  • Hi,I'm having trouble with the code below, it moves through the first worksheet without any trouble but it doesn't move to the next ws after executing the Next ws line. Any ideas on what could be going wrong or am i missing something?

    Code
    For Each ws In WorksheetsSelect Case UCase(ws.Name)Case "Index", "Master", "Data", "Summary", "Charts"'Do nothingCase Else        ActiveSheet.Range("A1").Select                    Do While Not IsEmpty(ActiveCell)                        ActiveCell.Offset(0, 1).Select                    Loop                    ActiveCell.Formula = "=SUBTOTAL(103, F2:F12000)"                    ActiveCell.Offset(0, 1).Formula = "=COUNTIF(G:G,""Met"")"                    ActiveSheet.Range("A1").Select                    End SelectNext ws

    Thanks, it's driving me mad!!

  • Re: For Each Worksheet in... not moving to next worksheet.


    - If you convert to upper case (UCase), how can you then end up with Index, Master, or other names?
    - What is the activeworkbook at the moment of executing the code?

    Regards,


    Wigi


    Excel MVP 2011-2014


    For more Excel memes: visit http://www.wimgielis.com ==> English articles ==> Excel memes


    -- Topics without [CODE] tags or a meaningful topic title, will be neglected by me (and probably many others as well) --

  • Re: For Each Worksheet in... not moving to next worksheet.


    Try this:


    Note: your check on the sheet names would never work as you were comparing an upper case word against proper case, so I changed that too.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Re: For Each Worksheet in... not moving to next worksheet.


    Because you never select or activate any worksheet, you simply use [vb] ActiveSheet.Range("A1").Select
    [/vb] in the Select Case statement so it defaults to whatever is the activesheet when you start the procedure.


    I don't know what you did to start with, but please do something about the formatting of your post - it's a pain to read.

  • Re: For Each Worksheet in... not moving to next worksheet.


    Hi,thanks for the quick reply!! I've used rory's code and it works well except it overrights the header in column 'H'.The code starts off in Index, using the data from within Master and Data sheets and creates new sheets from the information contained within them.also the sheet names are in mixed case i.e. 'Master' but rory's code works fine, can anyone explain why?ThanksDaren

  • Re: For Each Worksheet in... not moving to next worksheet.


    Sorry about the format of my code and text, it's to do with the restrictions of my broswer and where i work.

  • Re: For Each Worksheet in... not moving to next worksheet.


    Sorry - forgot to offset:

    Code
    With .Cells(1, .Columns.Count).End(xlToLeft).offset(, 1)

    The code converts the sheet name to lower case for the comparison using LCase.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!