Tim Hanson
Member
I am trying to run a macro on a filtered sheet, the macro was witten by @shrivallabha and deletes all rows where from a chosen column the cell values are repeated less then a chosen number in this case 2x i.e all rows are deleted where a cell value is not repeated at least 3x
But I am not getting the proper Number of rows
Thank You
But I am not getting the proper Number of rows
Thank You
Code:
Sub DeleteRows_Initial()
Application.DisplayAlerts = False
Dim ws As Excel.Worksheet
Dim DataRng As Range, CopyRng As Range, FilterdRng As Range
Dim lCol As Long, lRow As Long, cNumber As Long
Set ws = ThisWorkbook.Sheets("What I get")
lCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
lRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
With ws
Set DataRng = .Range("A1", .Cells(lRow, lCol))
'##Range To Run Remove Less Then 2 On
Set CopyRng = .Range("D2:D" & lRow)
.AutoFilterMode = False
DataRng.Cells.AutoFilter
DataRng.Cells.AutoFilter Field:=11, Criteria1:="N/A"
'##Get Filterd Range For Running Macro On
Set FilterdRng = CopyRng.SpecialCells(xlCellTypeVisible)
'Remove Less Then 2 i.e keep all rows that are repeated MORE the 2x
'Public Sub DeleteRowsLessThen_n()
Dim vSrc As Variant, vChk() As Variant
'Dim ws As Worksheet
Dim i As Long
Const iCnt As Integer = 3 '\\ Set number of rows to be retained
'Set ws = ThisWorkbook.Sheets("What I get")
'\\ Build up data in arrays so that we can process faster
' With ws.Range("D2:D" & Range("A" & Rows.Count).End(xlUp).Row)
With FilterdRng
ReDim vChk(1 To .Rows.Count, 1 To 1)
vSrc = .Value
End With
'\\ Create dictionary object for keeping count
With CreateObject("Scripting.Dictionary")
For i = LBound(vSrc) To UBound(vSrc)
If .Exists(vSrc(i, 1)) Then
.Item(vSrc(i, 1)) = .Item(vSrc(i, 1)) + 1
Else
.Add vSrc(i, 1), 1
End If
Next i
For i = LBound(vSrc) To UBound(vSrc)
If .Item(vSrc(i, 1)) >= iCnt Then
vChk(i, 1) = 1
End If
Next i
End With
'\\ Clean up the data using check done above
On Error Resume Next
With ws.Range("L2:L" & Range("A" & Rows.Count).End(xlUp).Row)
.Value = vChk
.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
.Clear
End With
'End Sub
.AutoFilterMode = False
End With
Application.DisplayAlerts = True
End Sub