Results 1 to 21 of 21

Unique Values from multiple columns of Table

Threaded View

  1. #7
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,543

    Re: Unique Values from multiple columns of Table

    A slightly different way
    Sub AAA()
        Dim varrData    As Variant
        Dim varrHeader  As Variant
        Dim r As Long, c As Long
        Dim oDic        As Object
        Dim v           As Variant
        Dim i           As Long
    
        Set oDic = CreateObject("Scripting.Dictionary")
    
        varrData = Worksheets("Base Sheet").ListObjects(1).DataBodyRange.Value
        varrHeader = Worksheets("Base Sheet").ListObjects(1).HeaderRowRange.Value
    
        'add teachers (in Keys) and their subjects (in Items) to the Dictionary
        For c = 1 To UBound(varrData, 2)
            For r = 1 To UBound(varrData)
                If Len(varrData(r, c)) > 0 Then
                    If oDic.Exists(varrData(r, c)) Then
                        oDic(varrData(r, c)) = oDic(varrData(r, c)) & "|" & varrHeader(1, c)
                    Else
                        oDic.Add varrData(r, c), varrHeader(1, c)
                    End If
                End If
            Next r
        Next c
    
        
        With Worksheets("Result Sheet")
            'delete rows with old data (if exists)
            If Len(.Range("C7").Value) > 0 Then
                .Range(.Cells(7, "C"), .Cells(.Rows.Count, "C").End(xlUp)).EntireRow.Delete
            End If
            'insert list of teachers
            .Range("C7").Resize(oDic.Count).Value = Application.Transpose(oDic.Keys())
            'fill the range of 5 columns with "Nil"
            .Range("D7").Resize(oDic.Count, 5).Value = "Nil"
    
            'fetch the subjects of the following teachers and put them into the range
            For i = 0 To oDic.Count - 1
                v = Split(oDic.Items()(i), "|")
                .Range("D7").Offset(i).Resize(, UBound(v) + 1).Value = v
            Next i
    
            'insert Sr.No.
            With .Range("B7")
                .Value = 1
                .DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
                            Step:=1, Stop:=oDic.Count, Trend:=False
            End With
        End With
    
    End Sub
    Artik
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Formula to get unique list of values from multiple columns in a table
    By paulma1960 in forum Excel Formulas & Functions
    Replies: 35
    Last Post: 10-31-2021, 07:31 PM
  2. Unique Values from 2 Columns of a table to an array
    By nikhilsharma30 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-07-2020, 11:13 AM
  3. Replies: 8
    Last Post: 08-16-2019, 10:59 AM
  4. Insert blank rows above a table based on unique values in one of the columns in the table
    By carlito2002wgn in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-08-2019, 01:06 PM
  5. Insert blank rows above a table based on unique values in one of the columns in the table
    By carlito2002wgn in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-06-2019, 07:52 PM
  6. Replies: 16
    Last Post: 01-11-2012, 10:51 AM
  7. Replies: 5
    Last Post: 04-21-2011, 05:22 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1