Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create Dynamic Array from one column based on autofilter criteria

I am trying to create a dashboard where:

  • It takes column A and creates an array of filtered names (autofilter) based off columns C, D, or E
  • C, D, and E only have Yes or No
  • I will only use one of the columns (C,D,E) to filter the names.
  • I then use the stored array to manipulate data.

The problem I am running into though is that when I use the autofilter feature and then use my code, I am unable to capture just the filtered data along with the last row.
So far I have been manually filtering the data and then testing my code.

Sub tester()

  Dim LastRow As Integer

  LastRow = ActiveWorkbook.Sheets("employees").UsedRange.Rows.count
  vasWksNames = UniquesFromRange(ActiveWorkbook.Sheets("employees").Range("A2:A" & LastRow))

  If UBound(arr) = -1 Then
      Debug.Print "no values found"
  Else
      Debug.Print "got array of unique values"
  End If

End Sub

Function UniquesFromRange(rng As Range)

  Dim d As Object, c As Range, tmp
  Set d = CreateObject("scripting.dictionary")
  For Each c In rng.Cells
     tmp = Trim(c.Value)
     If Len(tmp) > 0 Then
        If Not d.Exists(tmp) Then d.Add tmp, 1
     End If
  Next c
  UniquesFromRange = d.keys

End Function

Unfiltered data -> Filtered data -> Create Array

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Anything helps!

Unfiltered data

Filtered data

>Solution :

SpecialCells(xlCellTypeVisible) gets all visible cells.

Please try.

Function UniquesFromRange(rng As Range)
  Dim d As Object, c As Range, tmp
  Set d = CreateObject("scripting.dictionary")
  For Each c In rng.SpecialCells(xlCellTypeVisible).Cells
     tmp = Trim(c.Value)
     If Len(tmp) > 0 Then
        If Not d.Exists(tmp) Then d.Add tmp, 1
     End If
  Next c
  UniquesFromRange = d.keys
End Function

Please refers to Microsoft document.

Range.SpecialCells method (Excel)

XlCellType enumeration (Excel)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading