+ Reply to Thread
Results 1 to 13 of 13

Macro to delete rows in multiple worksheets within workbook

  1. #1
    Registered User
    Join Date
    02-22-2013
    Location
    IN
    MS-Off Ver
    Excel 2010
    Posts
    38

    Macro to delete rows in multiple worksheets within workbook

    Hey all,

    I would like to have a macro that would allow me to delete all rows, except row 1, in multiple worksheets, but not all worksheets in the workbook. An example of the worksheets I want to delete from are DB, KA, TJ. Like I said I'd like to keep row 1, and then delete everything else when I run it. There will only ever be of max of maybe 150 entries in those worksheets at any given time, just in case that helps lighten the load of the macro. I've seen some similar stuff but not exactly what I'm after. Thanks

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Macro to delete rows in multiple worksheets within workbook

    Since I don't know the rule for if a sheet gets rows deleted or not, I guess you'll just have to hardcode which sheets you want rows deleted. Here's an example for the first sheet, it assumes column A has data in it all the way to the last row you want deleted. Can be adjusted as necessary. Or you could just put in something like "2:150" or whatever.

    Please Login or Register  to view this content.

  3. #3
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Macro to delete rows in multiple worksheets within workbook

    Try:
    Please Login or Register  to view this content.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  4. #4
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Macro to delete rows in multiple worksheets within workbook

    Hello kornstalker,

    Would it be possible for you to confirm that you would prefer to keep certain Sheets intact, and if so, will it always be the same Sheets, or would you prefer to do it the other way round?

    Regards
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  5. #5
    Registered User
    Join Date
    02-22-2013
    Location
    IN
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Macro to delete rows in multiple worksheets within workbook

    Here's the full story if it helps... This is for sorting through our "open orders" spreadsheet. Each job is a row with multiple columns. Each job has been assigned to a toolmaker, denoted by initials such as DB, DV, AJ, in one of the cells. After I run that macro, it copies the rows for the jobs that have DB in a certain cell to the worksheet titled DB. Since the information in open orders changes daily, I would like to run a macro to delete those rows, except row 1 which is the title bar, so that I can then run the other macro again without adding to the old list or deleting manually.


    The same three sheets should be left intact every time, while the other 15 or so sheets would need rows deleted beginning at row 2.

    Doubt you really need it but below is the macro I run to populate the other worksheets. I actually forget what the "A" is for in that code.

    Sub ToolmakerSort()
    Dim i As Long
    Dim lr As Long
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    For i = lr To 2 Step -1

    If Range("H" & i).Value <> "" Then
    On Error Resume Next
    Range("H" & i).EntireRow.Copy Sheets(Range("H" & i).Value).Range("A" & Rows.Count).End(3)(2)
    On Error GoTo 0
    End If

    Next i
    End Sub

  6. #6
    Registered User
    Join Date
    02-22-2013
    Location
    IN
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Macro to delete rows in multiple worksheets within workbook

    I tried the Mumps code. Appeared to work well except that it deleted the top row, row 1, on the worksheets that didn't have any other entries other than the title bar. Not sure what causes that...is it the "& last row" part in the code?

  7. #7
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Macro to delete rows in multiple worksheets within workbook

    Try:
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    02-22-2013
    Location
    IN
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Macro to delete rows in multiple worksheets within workbook

    Thanks...That one seems to work fine. I just realized that the macro I run that copies all those rows to the other tabs, will not do so unless I have selected the main worksheet. It really doesn't matter, but why would the new one work no matter what tab/worksheet I have selected as active, while the other one require I have a certain one selected?

  9. #9
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Macro to delete rows in multiple worksheets within workbook

    If you want the code to work from any worksheet, you have to make sure that each line of code that references a certain worksheet includes the full worksheet name. In your case it would be the name of the main worksheet. for example:
    Please Login or Register  to view this content.
    instead of:
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    02-22-2013
    Location
    IN
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Macro to delete rows in multiple worksheets within workbook

    The main page it pulls from is titled "Open"
    This is the original macro that works but would not unless I ran it while on the "Open" worksheet tab:

    Sub ToolmakerSort()
    Dim i As Long
    Dim lr As Long
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    For i = lr To 2 Step -1

    If Range("H" & i).Value <> "" Then
    On Error Resume Next
    Range("H" & i).EntireRow.Copy Sheets(Range("H" & i).Value).Range("A" & Rows.Count).End(3)(2)
    On Error GoTo 0
    End If

    Next i
    End Sub

    This is what I ended up with after trying, probably incorrectly, to incorporate what you said. All I did was add ---Sheets("Open").--- in the first and third lines in the indented code section. It seems to do the same thing as before...It's really not a big deal but I figure there's a way to run it from whatever worksheet you happen to be on at the time.

    Sub ToolmakerSort()
    Dim i As Long
    Dim lr As Long
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    For i = lr To 2 Step -1

    If Sheets("Open").Range("H" & i).Value <> "" Then
    On Error Resume Next
    Sheets("Open").Range("H" & i).EntireRow.Copy Sheets(Range("H" & i).Value).Range("A" & Rows.Count).End(3)(2)
    On Error GoTo 0
    End If

    Next i
    End Sub

  11. #11
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Macro to delete rows in multiple worksheets within workbook

    Try:
    Please Login or Register  to view this content.
    You also have to define the worksheet that "lr" is on.

  12. #12
    Registered User
    Join Date
    02-22-2013
    Location
    IN
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Macro to delete rows in multiple worksheets within workbook

    No luck on that one either! Oh well. It's really quite insignificant, I just noticed it and wondered if there was an easy fix. I don't want to trouble you with it anymore though ha. Thanks for the help though as the ToolmakerDelete one is just what I needed.

  13. #13
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Macro to delete rows in multiple worksheets within workbook

    My pleasure.

+ 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. [SOLVED] delete rows from multiple worksheets
    By dckrause in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-06-2005, 03:05 AM
  2. [SOLVED] delete rows from multiple worksheets
    By dckrause in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-06-2005, 02:05 AM
  3. delete rows from multiple worksheets
    By dckrause in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-06-2005, 01:05 AM
  4. delete rows from multiple worksheets
    By dckrause in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-06-2005, 12:05 AM
  5. delete rows from multiple worksheets
    By dckrause in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-05-2005, 10:05 PM

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