I have a filter applied on column F based on a criteria (>1) rng.AutoFilter Field:=6, Criteria1:=">1"
where rng is set for the data via VBA earlier.
Now from the filtered rows I want to apply another filter on Col E (5) and cycle through each of the unique visible values in Col E and perform some comparisons on the data and determine whether to keep it or delete those rows- but I don't know what values would be shown - that depends on the first filter - how do I accomplish this?
Here is the whole code so far:
Sub CashFlowReporting()
Dim Dest, Source As Workbook
Dim DestCell As Range
Dim sh, ws, data As Worksheet
Dim x, y, r, c, m, s As Integer
Dim fname, sname, txt As String
Dim starttime, endtime, dtDate As Date
Dim ans As VbMsgBoxResult
Dim rng, rng1 As Range
Application.ScreenUpdating = False
Application.DisplayAlerts = False
starttime = Now
fname = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls;*.xlsx;*.xlsm), *.xls;*.xlsx;*.xlsm", Title:="Select the Term Changes Query Results file.")
If fname = False Then Exit Sub
ans = MsgBox("Is " & fname & "the Term Changes Query Results excel file?", vbYesNo)
If ans = vbYes Then
Workbooks.Open Filename:=fname
Else
MsgBox ("Please run the cash flow report genrator again and select the query results file.")
Exit Sub
End If
Set Source = ActiveWorkbook
Set sh = ActiveSheet
sh.Range("E:F").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("E1").Value = "Number_Site"
Range("F1").Value = "Count Num_Site"
Range("E2").FormulaR1C1 = "=RC[-3]&RC[-1]"
r = Range("A1").End(xlDown).Row
Range("E2", Cells(r, "E")).FillDown
Columns("E:F").AutoFit
Set rng = Range("A1")
Set rng = Range(rng, rng.End(xlToRight))
Set rng = Range(rng, rng.End(xlDown))
rng.Name = "Data"
Range("A2", Range("A2").End(xlDown)).Name = "Date"
Range("E2", Range("E2").End(xlDown)).Name = "Num_site"
sh.Sort.SortFields.Clear
sh.Sort.SortFields.Add Key:=Range("Num_site") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
sh.Sort.SortFields.Add Key:=Range("Date") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With sh.Sort
.SetRange Range("Data")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("F2").Formula = "=countif($E$2:$E$1000,E2)"
Range("F2", Cells(r, "F")).FillDown
rng.AutoFilter field:=6, Criteria1:=">1"
Set rng1 = rng.Rows.SpecialCells(xlCellTypeVisible)
rng1.Select
I would now want to filter based on field 5 but for each unique value within that field (cycle through it - in this case just 2 - could be more)
Here is a link to the screenshot screenshot of the data with the first filter applied on Col F, now I would like to cycle through the 2 unique values (in this case) in Col E based on this filter:
If there is a more elegant solution than filter then I am open to that - I have tried Pivots and advanced filters but could not figure out a solution.
Thanks in advance and all help is appreciated.