+ Reply to Thread
Results 1 to 15 of 15

Use macro to loop through multiple worksheets to sort data and delete rows based on data

  1. #1
    Registered User
    Join Date
    05-27-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2007
    Posts
    6

    Use macro to loop through multiple worksheets to sort data and delete rows based on data

    I am completely new to VBA and what I know I've learned from Google so please be patient and kind...I'm learning.

    I have several workbooks (one per campus) with multiple worksheets (one per teacher) with data that needs to be sorted by column F (LEP) and then column D (Student Name). Rows in which column F is blank or equal to "LEP" needs to be deleted.

    I have part of this figured out using:

    Please Login or Register  to view this content.
    ______________________________________________________

    It sorts for one worksheet and looks like it is for other worksheets in a workbook (page blinks really fast) but other worksheets are not sorted.

    Also, I have no clue about deleting rows based on certain criteria. Any help would be greatly appreciated.
    Attached Files Attached Files
    Last edited by Richard Buttrey; 05-10-2016 at 12:22 PM. Reason: code tags missing

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Use macro to loop through multiple worksheets to sort data and delete rows based on da

    Hi and welcome to the forum. Unfortunately your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (Since this is your first post I'll add them for you this time but please note for the future.

    Your macro only ever handles the Active Sheet which never changes. You need to process each of the sheets you identify with the sheet variable.

    So try instead

    Please Login or Register  to view this content.
    And just another thought, but if you want to do any serious analysis with your data you should hold all your data on one sheet, not distributed across many sheets one for each teacher.
    Add an extra column to a single sheet database and include the name of the teacher in the column and list all your data in a normalised table.

    And if you ever need to analyse cross campus data you should go a step further and instead of many workbooks include them all in your single sheet database with an additional column to hold the campus name
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Use macro to loop through multiple worksheets to sort data and delete rows based on da

    Hi selixc

    Welcome to the Forum!!!

    Please add Code Tags around your Code...it's a Forum requirement.

    This snippet of Code is always looking at the
    Please Login or Register  to view this content.
    Replace all instances with
    Please Login or Register  to view this content.
    For example...
    Please Login or Register  to view this content.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  4. #4
    Registered User
    Join Date
    05-27-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Use macro to loop through multiple worksheets to sort data and delete rows based on da

    Thank you both for letting me know about the Code rule...it was my first time to post.

    Changing the code from ActiveWorkbook.ActiveSheet. to .sht did the trick; thank you!

    Any idea on how to delete rows based on data?

    Side note: I'm keeping all my campuses and teachers separate for the purpose of sharing these files with others so confidentiality plays a role into this. I usually consolidate the data into a district file for the department's purposes.

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Use macro to loop through multiple worksheets to sort data and delete rows based on da

    Hi selixc

    Try this Code...
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    05-27-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Use macro to loop through multiple worksheets to sort data and delete rows based on da

    @Jaslake

    I'm getting a Run-time error '1004': The command could not be completed by using the range specified. Select a single cell within the range and try the command again.

    When selecting Debug it highlights the line: .Rows("1:1").AutoFilter

    Thoughts?

  7. #7
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Use macro to loop through multiple worksheets to sort data and delete rows based on da

    Hi selixc

    What version of Excel are you running? Your Profile shows 2003.

  8. #8
    Registered User
    Join Date
    05-27-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Use macro to loop through multiple worksheets to sort data and delete rows based on da

    It's 2007 now. I'll have to figure out how to update my profile.

  9. #9
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Use macro to loop through multiple worksheets to sort data and delete rows based on da

    Hi selixc

    I'm going to upload your Sample File with the Code embedded. The Code was developed in 2007 and performs as expected on my platform. Back at you in a few...

  10. #10
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Use macro to loop through multiple worksheets to sort data and delete rows based on da

    Hi selixc

    The attached is your Sample File with the Code embedded. Open the Workbook...CTRL + x will fire the Code. Works on my Platform...what say you???
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    05-27-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Use macro to loop through multiple worksheets to sort data and delete rows based on da

    It gave me another error.

    Run-time error '1004': Cannot use that command on overlapping selections.

    Debug highlights line: .AutoFilter.Range.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete

  12. #12
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Use macro to loop through multiple worksheets to sort data and delete rows based on da

    On the Sample File???

    Quote Originally Posted by selixc View Post
    It gave me another error.

    Run-time error '1004': Cannot use that command on overlapping selections.

    Debug highlights line: .AutoFilter.Range.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete

  13. #13
    Registered User
    Join Date
    05-27-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Use macro to loop through multiple worksheets to sort data and delete rows based on da

    Yes, on the sample file.

  14. #14
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Use macro to loop through multiple worksheets to sort data and delete rows based on da

    Hi selixc

    Honestly don't know...I get the attached after running the Code.
    Attached Files Attached Files

  15. #15
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Use macro to loop through multiple worksheets to sort data and delete rows based on da

    I'd suggest you upload the offending file...I'll see if I can duplicate the issue.

+ 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. Sort and Delete Data Based on Multiple Criteria Using Check Box in Userform
    By Primas in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-04-2016, 01:16 AM
  2. need to loop through worksheets and delete row data
    By AWITCHER in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-08-2015, 04:01 PM
  3. [SOLVED] Delete rows based on the value of two columns using VBA code - Multiple worksheets
    By OmniBlue in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-18-2015, 04:24 PM
  4. [SOLVED] No Macro Experience - Need to run a loop (I think?!) over a set of data for multiple rows
    By vkelly in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-29-2014, 08:11 AM
  5. [SOLVED] Delete rows based on multiple criterias, copy/paste data and formulas
    By BQuek in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-24-2013, 01:36 PM
  6. Loop macro for multiple separate graphs from rows of a large data set
    By GlennToms in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-30-2012, 02:12 PM
  7. Delete multiple rows of data based on criteria in a single cell
    By slaga9 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-12-2011, 03:41 AM

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