How To Delete or Add Rows To A Range Using VBA

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Using this image as a guide:
copy_data_image.jpg


The code below here is used to find a given date stored in the variable called "findvalue".
Code:
        Dim findvalue As Range, lr&, j&
        Dim exp As Worksheet, c&, i&
        Set exp = Sheets("EXPENSE")
        lr = exp.Cells(Rows.Count, "A").End(xlUp).Row
        If lr < 4 Then lr = 4
        Set findvalue = exp.Range("A4:A" & lr).Find(what:=CDate(cboGetDate), LookIn:=xlValues, lookat:=xlWhole)

Now here are my requests:
1. If I want to delete say two rows between the found date, eg - 26-01-21 and the string "DAILY TOTALS", what are the lines that I need to write?

2. If I want to Add say two rows between the found date, eg - 26-01-21 and the string "DAILY TOTALS", what are the lines that I need to write?

Thanks in advance.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
First, let's clarify that 'findvalue' is a range and not a date value, although it may contain a date value. Being that it is a range, you can use it as a reference point to add or delete rows and columns. For example, if you want to delete 'NEWSPAPER' and 'BANK CHEQUE' rows then
VBA Code:
findvalue.Offset(2).Resize(2).EntireRow.Delete
would delete those two rows and shift everything up (shift up is default).
Similarly, to add rows you need to use the Insert method.
VBA Code:
findvalue.Offset(2).Resize(2).EntireRow.Insert
would insert two rows above "NEWSPAPER'.
The insert function uses the row or column at either the left or top of the specified range as the insertion point and shifts everything either right or downward as the case may be.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,614
Messages
6,120,519
Members
448,968
Latest member
Ajax40

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