VBA to select multiple row ranges and then delete them all at once

SlinkRN

Well-known Member
Joined
Oct 29, 2002
Messages
715
This seems like it would be so easy but I just can't get it to work yet. I need to do this:
sheets("sheet1").range("4:136,158:198,250:289,316:800").select
selection.delete shift:=xlup

EXCEPT that I need to use variables to find the row numbers.
Row 4 is always row 4 so I can use that number.
Row 136 will actually be the variable DeleteRow
Row 158 will actually be the variables DeleteRow + EndDeleteRow
Row 198 will actually be the variable DeleteRow2
Row 250 will actually be the variables DeleteRow2 + EndDeleteRow2
Row 289 will actually be the variable DeleteRow3
Row 316 will actually be the variables DeleteRow3 + EndDeleteRow3
Row 800 is always row 800 so I can use that number.

I've tried:
sheets("sheet1").range("4:" & DeleteRow,DeleteRow + EndDeleteRow & ":" & DeleteRow2,DeleteRow2 + EndDeleteRow2 & ":" & DeleteRow3,DeleteRow3 + EndDeleteRow3 & ":800").select
selection.delete shift:=xlup
but I get an error saying "Run-time error '450' Wrong number of arguments or invalid property assignment"

What am I missing? Thanks so much! Slink
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Give this a try with a copy of your workbook

VBA Code:
Sheets("sheet1").Range(Replace(Replace(Replace(Replace(Replace(Replace("4:@,#:$,%:^,&:800", _
  "@", DeleteRow), "#", DeleteRow + EndDeleteRow), "$", DeleteRow2), "%", DeleteRow2 + EndDeleteRow2), "^", DeleteRow3), "&", DeleteRow3 + EndDeleteRow3)).Delete
 
Upvote 0
Another option:
VBA Code:
With Sheets("sheet1")
Union(.Range("A4" & ":A" & DeleteRow), .Range("A" & DeleteRow + EndDeleteRow & ":A" & DeleteRow2), _
.Range("A" & DeleteRow2 + EndDeleteRow2 & ":A" & DeleteRow3), .Range("A" & DeleteRow3 + EndDeleteRow3 & ":A800")).EntireRow.Delete
End With
 
Upvote 0
If using the union idea (good idea), to me it makes sense (& shorter code) to eliminate the column A references and go straight to entire rows.

VBA Code:
With Sheets("sheet1")
Union(.Rows("4:" & DeleteRow), .Rows(DeleteRow + EndDeleteRow & ":" & DeleteRow2), _
.Rows(DeleteRow2 + EndDeleteRow2 & ":" & DeleteRow3), .Rows(DeleteRow3 + EndDeleteRow3 & ":800")).Delete
End With
 
Upvote 0
Little late but as the Range function accepts a string this worked me (correct me if I'm I wrong):

VBA Code:
Sheets("Sheet1").Range("4:" & DeleteRow & "," & DeleteRow + EndDeleteRow & ":" & DeleteRow2 & "," & DeleteRow2 + EndDeleteRow2 & ":" & DeleteRow3 & "," & DeleteRow3 + EndDeleteRow3 & ":800").Delete shift:=xlUp

Regards,

Robert
 
Upvote 0
Solution
Thank you Trebor, that does work!! I had written something like that to start with, but after looking at yours I realize now that I was forgetting to put the commas in quotes and using ampersands around them!!! Thanks for solving that mystery for me!! I was glad to learn about the Union thing too though. I love that Excel has so many ways to get things done :)
 
Upvote 0
Thank you Trebor, that does work!! I had written something like that to start with, but after looking at yours I realize now that I was forgetting to put the commas in quotes and using ampersands around them!!! Thanks for solving that mystery for me!!
You're welcome (y)

I was glad to learn about the Union thing too though. I love that Excel has so many ways to get things done
That is true. I suppose the key is to use the most efficient way ;)
 
Upvote 0

Forum statistics

Threads
1,215,170
Messages
6,123,422
Members
449,099
Latest member
COOT

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