+ Reply to Thread
Results 1 to 2 of 2

VBA: Certain commands are not looping through all worksheets

  1. #1
    Registered User
    Join Date
    11-20-2020
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    1

    VBA: Certain commands are not looping through all worksheets

    Hi all, I am new to VBA so sorry if this is obvious.

    I have a macro which copies sheets from a Masterfile into a Template output file containing formatting.

    Because the output can have varying column numbers I have written a command which clears formatting from whenever the data finishes.

    The second demand then clears contents of column A (which contains lookup references, hence isn't wanted for the output).



    These commands work as when I run the macro the first sheet is formatted correctly.

    However, it doesn't loop the formatting commands through to the other worksheets.

    My code reads as below.


    ' Clear formatting for all columns after last column
    ' Clear contents of Column A

    Dim ws As Worksheet
    For Each ws In Worksheets

    Set LastCell = Cells.Find(What:="*", After:=Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
    xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
    LastColumn = LastCell.Column

    Range(Columns(LastColumn + 1), Columns(Columns.Count)).ClearFormats
    Columns(1).ClearContents

    Next


    Any help at all would be appreciated!

  2. #2
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,150

    Re: VBA: Certain commands are not looping through all worksheets

    Welcome to the forum,

    You operate with certain commands in a loop, that is, you want to apply them to subsequent sheets in your workbook, but you refer the commands to the current-active sheet, because the commands lack reference to an object, i.e. "ws."
    Please Login or Register  to view this content.

+ 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. VBA Looping Through Worksheets
    By Shahadat65 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-23-2016, 10:48 AM
  2. looping through worksheets except
    By bjcowen9000 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-19-2015, 03:09 PM
  3. why isn't this looping through all worksheets? :(
    By KATIEexcel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-09-2014, 09:12 AM
  4. Macro Recording Chart Format commands missing all important commands!!!!
    By nounours in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-05-2013, 09:20 PM
  5. Looping through worksheets
    By terkel in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-25-2008, 03:54 AM
  6. looping a set of commands for a list
    By johndough185 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-19-2007, 03:43 PM
  7. Looping across worksheets
    By Ramthebuffs in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-19-2005, 12:52 AM

Tags for this Thread

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