How To Delete or Add Rows To A Range Using VBA

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,883
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.
 

Some videos you may like

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
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.
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,127,098
Messages
5,622,682
Members
415,920
Latest member
ExcelNoob28

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
Top