VBA code to delete certain rows (until the end) of a table

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I've been asked to write code which deletes everything from row 9 downwards in a table.

The table goes from row 7 (which has the titles) and row 8 has some formulas which should be retained.

I'd written this code, which selects a cell in the table, then highlights the region.

Range("D7").CurrentRegion.select

However, I want to offset the region by two rows, so wrote this:

Range("D7").CurrentRegion.Offset(-2,0).Select

However, it won't let me offset the current region by 2 rows (from row 7 to row 9).

Does anyone know how to do this?

I'd then want to delete the new range.

Thanks in advance.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try
Code:
Range("D7").CurrentRegion.Offset(2,0).Select
 
Upvote 0
Just note that your Offset will move the whole range down two rows. So, if you original Current Region ended on row 14, it will move down to row 16.
If there isn't anything under that table, it is probably not a big deal (just selecting/deleting blank rows). However, if you may have more data after a single blank row, it might select/delete stuff in row 16 that you do not want selected or deleted!

If that is the case, you can use Resize to shrink the size of the range by the same number of rows you are moving it down (so it ends in row 14 again).
That code would look something like this:
Code:
    Dim myRange As Range
    Set myRange = Range("D7").CurrentRegion
    myRange.Offset(2, 0).Resize(myRange.Rows.Count - 2).Select
 
Upvote 0
Thanks Fluff!

Ever so helpful!!

Do you know how I can delete that range after it's been selected?

I'd like to do it in a two step process, so instead of just doing



Range("D7").CurrentRegion.Offset(2, 0).delete

I'd like to select it in the way you have, then delete the selected area.

Thanks in advance!
 
Upvote 0
Thanks for the tip, Joe4 - that's much appreciated!

I won't need to worry about any of the rows below row 8, as the spreadsheet will be populated with new data.

But this is a good tip, in case I need to refer to it in future.
 
Upvote 0
Like this
Code:
Range("D7").CurrentRegion.Offset(2,0).Select
Selection.Delete
But you need to take into account what @Joe4 said in post#3
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
Woo hoo!!

I used what both you and Joe4 said.

I initially got a run time error without resizing, but amended the code with Joe's resize tip and it works like clockwork!

If I wasn't in an office right now, I'd get up and dance!!

Thank you both!!
 
Upvote 0
I just wanted to say thanks for this tip, Joe, in case you don't see my note of gratitude at the bottom of the thread.

I needed to use it now.

Thanks again! It was very useful!
 
Upvote 0

Forum statistics

Threads
1,223,970
Messages
6,175,703
Members
452,667
Latest member
vanessavalentino83

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