Consulting

Results 1 to 4 of 4

Thread: Solved: For Each Loop through Worksheets Collection

  1. #1

    Solved: For Each Loop through Worksheets Collection

    This code is failing.
    Is it because I am doing a For Each loop and then trying to exclude one of the items in the collection?
    [VBA]
    For Each wsSource In wbTarget.Worksheets
    If wsSource <> "AttribTable" Then
    Columns("A:J").AutoFit
    Else
    Exit For
    End If
    Next wsSource

    [/VBA]
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [vba]For Each wsSource In wbTarget.Worksheets
    If wsSource.Name <> "AttribTable" Then 'Get a string to compare to a string
    wsSource.Columns("A:J").AutoFit 'You need to refer to the WS or the
    'ActiveSheet will be changed.
    Else
    Exit For
    End If
    Next wsSource[/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    YLP,

    As we have said before, you have to properly qualify an object/property that you work upon, otherwise it will look at the currently active object/property, if there is one.

    YOu should also use the property of the object variable, Name for wsSource. Even if it were the default, I strongly believe in NOT using property defaults, so you should be explicit in using wsSource.Name, partly for documentary purposes, partly to ensure that anyone else who reads it gets it immediately and doesn't waste time checking defaults (if they know there is such a thing), partly because it may not always be so.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Thanks Malcolm.

    Thanks Bob, I found an example reading later on about completing the line w/ the property, (.Name), after finishing up for the night. It makes proper sense, I cannot recall exactly which post before- but one reason I did not look at this as the reason was you and I were covering something similar and I had the thought in my head that wsSource was the same as wsSource.Name.... obviously that was incorrect. I only explain this to this depth as to point out that I am trying not to be careless in my syntax or logic, just have not put it altogether yet.

    Cheers Gents,
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

Posting Permissions

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