Advertisement
Guest User

Excel Tool for analyzing Data Redundancy

a guest
Oct 27th, 2016
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub UniqueAnalysis()
  2.  
  3. 'Select FIRST cell in vertical (Column) Range BEFORE running program.
  4. 'starts with the current ActiveCell, and grabs entire data set from the bottom of sheet
  5.    '(so that gaps in the data / NULL values aren't a problem)
  6.    
  7. Dim lngRow As Long
  8. Dim lngRows As Long
  9.  
  10. Dim wksCurr As Excel.Worksheet
  11.  
  12.     'get handle for current worksheet, find bottom row of range.
  13.    Set wksCurr = ActiveSheet
  14.     lngRow = wksCurr.Cells(wksCurr.Rows.Count, ActiveCell.Column()).End(xlUp).Row
  15.     lngRows = lngRow - ActiveCell.Row + 1
  16.    
  17.     'Copy the data Range
  18.    wksCurr.Range(ActiveCell, wksCurr.Cells(lngRow, ActiveCell.Column())).Copy
  19.    
  20.     'add new worksheet
  21.    ActiveWorkbook.Worksheets.Add After:=wksCurr
  22.    
  23.     'paste in values
  24.    ActiveSheet.Range("A2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
  25.         SkipBlanks:=False, Transpose:=False
  26.    
  27.     'copy and paste for unique list
  28.    Selection.Copy ActiveSheet.Range("B2")
  29.    
  30.     'remove dupes from unique list
  31.    ActiveSheet.Range("B2:B" & lngRows + 1).RemoveDuplicates Columns:=1, Header:=xlNo
  32.    
  33.     'enter count values
  34.    For lngRow = 2 To Cells(Rows.Count, 2).End(xlUp).Row
  35.    
  36.         Cells(lngRow, 3).Value = WorksheetFunction.CountIf(Range("A2:A" & lngRows + 1), Cells(lngRow, 2).Value)
  37.    
  38.     Next lngRow
  39.    
  40.     'sort to bring most common to top
  41.    With ActiveSheet.Sort
  42.         .SortFields.Clear
  43.         .SortFields.Add Key:=Range("C2:C" & Cells(Rows.Count, 2).End(xlUp).Row), _
  44.             SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
  45.         .SortFields.Add Key:=Range("B2:B" & Cells(Rows.Count, 2).End(xlUp).Row), _
  46.             SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
  47.        
  48.         .SetRange Range("B2:C" & Cells(Rows.Count, 2).End(xlUp).Row)
  49.         .Header = False
  50.         .MatchCase = False
  51.         .Orientation = xlTopToBottom
  52.        
  53.         .Apply
  54.    
  55.     End With
  56.  
  57.    
  58.     'Headers and formtting
  59.    Range("A1").Value = "All Values"
  60.     Range("B1").Value = "Uniques"
  61.     Range("C1").Value = "Count"
  62.     Range("A1:C1").Font.Bold = True
  63.     Columns("A:B").AutoFit
  64.    
  65.     'Summary Values
  66.    Range("E1").Value = "Value Count"
  67.     Range("E2").Value = "Unique Count"
  68.     Range("E3").Value = "Dupe Count"
  69.     Range("E1:E3").Font.Bold = True
  70.     Columns("E:E").AutoFit
  71.    
  72.     'punch in Count values
  73.    Range("F1").Formula = "=COUNTA(A:A) - 1"
  74.     Range("F2").Formula = "=COUNTA(B:B) - 1"
  75.     Range("F3").Formula = "=F1-F2"
  76.     Range("F1:F3").NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
  77.  
  78.  
  79. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement