+ Reply to Thread
Results 1 to 2 of 2

Macro to loop through all sheets and delete unwanted columns

  1. #1
    Registered User
    Join Date
    07-10-2008
    Location
    Norwich, England
    MS-Off Ver
    Office XP
    Posts
    15

    Macro to loop through all sheets and delete unwanted columns

    I have the following macro which opens all the Excel files within a given folder, loops through them all and deletes columns G to N, then it re-saves each of the files.

    It runs without error, but when I opened the Excel files to check that the columns had been deleted correctly, I noticed that the macro had only worked for one tab per file (Sheet1). However, I need it to loop through all the tabs in each of the files to delete the unwanted columns. The tab names are not the same in all the spreadsheet files.

    I thought that the For Each Worksheet In ActiveWorkbook.Worksheets command would loop through all of the tabs within each of the open files?

    How would I fix the macro so that it works for every tab?


    Please Login or Register  to view this content.
    Last edited by LemonTwist; 02-25-2009 at 10:41 AM. Reason: Problem solved, thanks!

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Macro to loop through all sheets and delete unwanted columns

    You need to tell XL that the columns your deleting relate to the sheet in the Loop else it will fire against the Active sheet only... looping does not activate.

    Use:

    Please Login or Register  to view this content.
    Note you don't need to select the columns to delete them... Selecting will slow your code.

+ 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