Delete Multiple Rows in Multiple Worksheets - R1

Rumpkin

Board Regular
Joined
Sep 24, 2016
Messages
75
Office Version
  1. 2019
Platform
  1. Windows
I don't have a solution to my original post and am re-posting.
I need to be able to delete multiple rows that are selected consecutively in multiple worksheets.
At this point I would even use some VBA so achieve it.

Goal: select multiple (consecutive) rows within multiple (quantity determined by the sheets selected) work sheets

I have located a VBA solution from (Exceltip) that only deletes one row:
VBA Code:
Sub DeleteRowsAllSheets()

   Dim SheetsArr As Variant
   Dim x As Long
   Dim a As Long
 
      SheetsArr = Array("Sheet4","Sheet5","Sheet6","Sheet7","Sheet8","Sheet9","Sheet10","Sheet11","Sheet12","Sheet13","Sheet14","Sheet15","Sheet28","Sheet29","Sheet30","Sheet31","Sheet32","Sheet33","Sheet34","Sheet35","Sheet36","Sheet37","Sheet38","Sheet39")

    a = Selection.Row

    For x = LBound(SheetsArr) To UBound(SheetsArr)
      Sheets(SheetsArr(x)).Rows(a).EntireRow.Delete
Next x

End Sub

This gives me a subscript out of range error and to Debug, highlights "Sheets(SheetsArr(x)).Rows(a).EntireRow.Delete"

When I try to use: (which indicates the sheets/tabs names)

VBA Code:
Sub DeleteRowsAllSheets()

   Dim SheetsArr As Variant
   Dim x As Long
   Dim a As Long
 
      SheetsArr = Array("PA.01", "PA.02", "PA.03", "PA.04", "PA.05", "PA.06", "PA.07", "PA.08", "PA.09", "PA.10", "PA.11", "PA.12", "PA.13", "PA.14", "PA.15", "PA.16", "PA.17", "PA.18", "PA.19", "PA.20", "PA.21", "PA.22", "PA.23", "PA.24")

    a = Selection.Row

    For x = LBound(SheetsArr) To UBound(SheetsArr)
      Sheets(SheetsArr(x)).Rows(a).EntireRow.Delete
Next x

End SubSub

It will only delete the top row of the rows selected which then leave #ref in to remaining rows that were to be deleted.
 
Last edited by a moderator:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try
VBA Code:
Sub DeleteRowsAllSheets()

   Dim SheetsArr As Variant
   Dim x As Long
   Dim a As String
 
      SheetsArr = Array("PA.01", "PA.02", "PA.03", "PA.04", "PA.05", "PA.06", "PA.07", "PA.08", "PA.09", "PA.10", "PA.11", "PA.12", "PA.13", "PA.14", "PA.15", "PA.16", "PA.17", "PA.18", "PA.19", "PA.20", "PA.21", "PA.22", "PA.23", "PA.24")

    a = Selection.Address

    For x = LBound(SheetsArr) To UBound(SheetsArr)
      Sheets(SheetsArr(x)).Range(a).EntireRow.Delete
Next x

End Sub
 
Upvote 0
Did you change the variable type?
 
Upvote 0
I don't believe so.
I added one sheet

Sub DeleteRowsAllSheets()

Dim SheetsArr As Variant
Dim x As Long
Dim a As Long

SheetsArr = Array("PA.01", "PA.02", "PA.03", "PA.04", "PA.05", "PA.06", "PA.07", "PA.08", "PA.09", "PA.10", "PA.11", "PA.12", "SU.01", "PA.13", "PA.14", "PA.15", "PA.16", "PA.17", "PA.18", "PA.19", "PA.20", "PA.21", "PA.22", "PA.23", "PA.24")

a = Selection.Address

For x = LBound(SheetsArr) To UBound(SheetsArr)
Sheets(SheetsArr(x)).Rows(a).EntireRow.Delete
Next x

End Sub
 
Upvote 0
You need to change a to string as I showed in the code I posted.
 
Upvote 0
Oh, crap. There's that follow directions thing.
My apologies. I just changed the selection.Address.
It works now.
If I delete some of the sheets in the workbook, will it break the code?
Thanks for your help and again, my apologies.
 
Upvote 0
It will if those sheets are still in the array. However you could use
VBA Code:
Sub DeleteRowsAllSheets()

   Dim SheetsArr As Variant
   Dim x As Long
   Dim a As String
 
      SheetsArr = Array("PA.01", "PA.02", "PA.03", "PA.04", "PA.05", "PA.06", "PA.07", "PA.08", "PA.09", "PA.10", "PA.11", "PA.12", "PA.13", "PA.14", "PA.15", "PA.16", "PA.17", "PA.18", "PA.19", "PA.20", "PA.21", "PA.22", "PA.23", "PA.24")

    a = Selection.Address

   For x = LBound(SheetsArr) To UBound(SheetsArr)
      If Evaluate("isref('" & SheetsArr(x) & "'!A1)") Then
         Sheets(SheetsArr(x)).Range(a).EntireRow.Delete
      End If
   Next x

End Sub
 
Upvote 0
Solution
Bingo!
Thank you.
I can't find a shortcut key that will work, though.
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top