VBA Delete Rows of Previous Year

katekoz

Board Regular
Joined
Jan 20, 2020
Messages
91
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have the following code to delete rows with old data. Below are all the lines pertaining to the functionality I'm asking about, but please note that this is a very small piece of a much larger macro.

The data is considered "old" if it matches the month entered via a user input box (U2) and is from the year before the year entered in a different user box (U1). It's not working, I think because of the "and" statement. Any help with this would be appreciated!

Dim r As Integer
Dim U1 As String
Dim U2 As String

U1 = InputBox("What four-digit year of data are you entering? For example, '2019'")
U2 = InputBox("What two digit month of data are you entering? For example, March would be '03'")

'Delete rows from the previous year of the month of data that was just added.

For r = ThisWorkbook.Sheets("Overall User Data").UsedRange.Rows.Count To 1 Step -1
If Cells(r, "A") = U1 - 1 And Cells(r, "B") = U2 Then
ThisWorkbook.Sheets("Overall User Data").Rows(r).EntireRow.Delete
End If
Next


NOTE: Even better would be if I could skip the input boxes completely. To do that, I would need the macro to look at the year (column A) and month (column B) of data that was entered (from a pulled report) in the "New Data Add" tab before it moves it into the "Overall User Data" tab. Then, it could delete the same month / previous year of the full data set in the "Overall User Data" tab once it's moved.
Another option - it wouldn't need to look at the data while it's still in the "New Data Add" tab. It could simply move that data from the "New Data Add" tab to the "Overall User Data" tab, which it already does, and check that entire data set for any instance of data with the same month and two different years, and delete any line with the older year.
These are a bit too complicated for me (since I couldn't even get the input boxes to work!), so I know they can be done, but don't know how to do them.

Thanks so much for your help!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,224,818
Messages
6,181,150
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