Hello,
I'm trying to automate some of the updating I do within an excel 2013 workbook. Each month, I refresh the rolling-6 month data for the past 3 years and add a new rolling-6 months of data. to do this, I manually filter and delete the respective periods. As an example, if I'm doing the data update for August 2018, I'll delete the data for August 2015, February 2016, August 2016, February 2017, August 2017, and February 2018. I'll then add new data for August 2015, February 2016, August 2016, February 2017, August 2017, February 2018, and August 2018.
I thought I could automate this by...
- use a UserForm to select the earliest month and year to delete
- generate an array using a for...next loop, adding 6 months to the value with each iteration
- autofilter using the array as criteria and deleting the visible cells.
Unfortunately, it doesn't want to work. Here's the code I'm using...
I've checked the array output, and it contains the strings I think it should contain...
arrFilter(0) = "8/1/2015"
arrFilter(1) = "2/1/2016"
...
arrFilter(5) = "2/1/2018"
Any assistance you can provide will be greatly appreciated.
-Frank
I'm trying to automate some of the updating I do within an excel 2013 workbook. Each month, I refresh the rolling-6 month data for the past 3 years and add a new rolling-6 months of data. to do this, I manually filter and delete the respective periods. As an example, if I'm doing the data update for August 2018, I'll delete the data for August 2015, February 2016, August 2016, February 2017, August 2017, and February 2018. I'll then add new data for August 2015, February 2016, August 2016, February 2017, August 2017, February 2018, and August 2018.
I thought I could automate this by...
- use a UserForm to select the earliest month and year to delete
- generate an array using a for...next loop, adding 6 months to the value with each iteration
- autofilter using the array as criteria and deleting the visible cells.
Unfortunately, it doesn't want to work. Here's the code I'm using...
Code:
Public dtFirst As String
Sub Six_Month_Delete()
'
' Delete most recent 6-month intervals
'
Dim arrFilter(6) As String
Dim iMonthAdd As Integer
Dim iSheet As Integer
Dim iRow As Single
Dim strRange As String
Dim myBook As Workbook
Set myBook = ActiveWorkbook
UserForm1.Show
arrFilter(0) = dtFirst
For iMonthAdd = 1 To 6
arrFilter(iMonthAdd) = Format(DateAdd("m", iMonthAdd * 6, DateValue(dtFirst)), "m/d/yyyy")
Next iMonthAdd
For iSheet = 1 To 3
With myBook.Sheets(iSheet)
.Activate
iRow = .Range("a1").End(xlDown).Row
strRange = "$A$1:$S$" & iRow
.Range(strRange).AutoFilter
.Range(strRange).AutoFilter Field:=2, Operator:=xlFilterValues, Criteria1:=arrFilter()
.Range(strRange).Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
.Range(strRange).AutoFilter
End With
Next iSheet
End Sub
I've checked the array output, and it contains the strings I think it should contain...
arrFilter(0) = "8/1/2015"
arrFilter(1) = "2/1/2016"
...
arrFilter(5) = "2/1/2018"
Any assistance you can provide will be greatly appreciated.
-Frank