Search Filter Mapping Matrix - via VBA code

ShuStar

Board Regular
Joined
Sep 9, 2015
Messages
67
Office Version
2010
Platform
Windows
Hi, I've realised I need to place the ID whenever the matrix is populated, which will come from D3 in the Keywords tab (it will always be numeric ID - not sure of length). The ID in D3 will be inputted manually and no VBA code needed to generate it.

This ID sitting in D3 within the Keywords Tab, how can the code be amended so that the table populated in the Matrix sheet looks something as below:


IDNameCountApplesBananaNectarineStrawberry
12345678910John3YYY
12345678910Barry4YYY
12345678910Tom4YYYY
12345678910Sarah2YY
 

Some videos you may like

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,669
Office Version
365
Platform
Windows
How about
VBA Code:
Sub ShuStar()
   Dim KeyAry As Variant, DataAry As Variant, OutAry As Variant
   Dim r As Long, nr As Long, c As Long, Cnt As Long, IdNum As Long
   Dim nc As Variant
   Dim Flg As Boolean
   
   With Sheets("Keywords")
      If .Range("B3").Value = "" Then Exit Sub
      IdNum = .Range("D3").Value
      KeyAry = Application.Transpose(.Range("B3", .Range("B" & Rows.Count).End(xlUp).Offset(1)).Value2)
   End With
   With Sheets("Raw Data")
      DataAry = .Range("A2", .Range("A" & Rows.Count).End(xlUp)).Resize(, .Cells(2, .Columns.Count).End(xlToLeft).Column).Value2
   End With
   ReDim OutAry(1 To UBound(DataAry), 1 To UBound(KeyAry) + 3)
   
   For r = 2 To UBound(DataAry)
      For c = 2 To UBound(DataAry, 2)
         nc = Application.Match(DataAry(1, c), KeyAry, 0)
         If Not IsError(nc) And DataAry(r, c) = "Y" Then
            If Not Flg Then
               nr = nr + 1
               Flg = True
               OutAry(nr, 1) = IdNum
               OutAry(nr, 2) = DataAry(r, 1)
            End If
            Cnt = Cnt + 1
            OutAry(nr, nc + 3) = DataAry(r, c)
         End If
      Next c
      If Flg Then OutAry(nr, 2) = Cnt
      Cnt = 0
      Flg = False
   Next r
   With Sheets("Matrix")
      .UsedRange.ClearContents
      .Range("A2:B2").Value = Array("Names", "Count")
      .Range("C2").Resize(, UBound(KeyAry) - 1).Value = KeyAry
      .Range("A3").Resize(nr, UBound(OutAry, 2)).Value = OutAry
   End With
End Sub
 

ShuStar

Board Regular
Joined
Sep 9, 2015
Messages
67
Office Version
2010
Platform
Windows
Thanks for this but unfortunately that has not worked - the output in the matrix tab becomes skewed (names is overwritten with the ID and the plotting of Ys is misaligned).

Please see images below:

Results below: when an ID is placed in Keywords tab cell D3

1595768965052.png



Results below: when cell D3 is blank in Keywords tab (side note: if D3 is blank, instead of a 0 being plot, is it possible to input "No ID" instead?)

1595769045240.png
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,669
Office Version
365
Platform
Windows
Try
VBA Code:
Sub ShuStar()
   Dim KeyAry As Variant, DataAry As Variant, OutAry As Variant
   Dim r As Long, nr As Long, c As Long, Cnt As Long, IdNum As Long
   Dim nc As Variant
   Dim Flg As Boolean
   
   With Sheets("Keywords")
      If .Range("B3").Value = "" Then Exit Sub
      IdNum = .Range("D3").Value
      KeyAry = Application.Transpose(.Range("B3", .Range("B" & Rows.Count).End(xlUp).Offset(1)).Value2)
   End With
   With Sheets("Raw Data")
      DataAry = .Range("A2", .Range("A" & Rows.Count).End(xlUp)).Resize(, .Cells(2, .Columns.Count).End(xlToLeft).Column).Value2
   End With
   ReDim OutAry(1 To UBound(DataAry), 1 To UBound(KeyAry) + 3)
   
   For r = 2 To UBound(DataAry)
      For c = 2 To UBound(DataAry, 2)
         nc = Application.Match(DataAry(1, c), KeyAry, 0)
         If Not IsError(nc) And DataAry(r, c) = "Y" Then
            If Not Flg Then
               nr = nr + 1
               Flg = True
               OutAry(nr, 1) = IdNum
               OutAry(nr, 2) = DataAry(r, 1)
            End If
            Cnt = Cnt + 1
            OutAry(nr, nc + 3) = DataAry(r, c)
         End If
      Next c
      If Flg Then OutAry(nr, 3) = Cnt
      Cnt = 0
      Flg = False
   Next r
   With Sheets("Matrix")
      .UsedRange.ClearContents
      .Range("A2:c2").Value = Array("ID", "Names", "Count")
      .Range("d2").Resize(, UBound(KeyAry) - 1).Value = KeyAry
      .Range("A3").Resize(nr, UBound(OutAry, 2)).Value = OutAry
   End With
End Sub
 

ShuStar

Board Regular
Joined
Sep 9, 2015
Messages
67
Office Version
2010
Platform
Windows
Thanks for this it has now resolved it (plots properly in matrix tab), except when I placed a larger ID number it throws an error (the LEN of the ID below is 17) - please see image below.

Is there a way it can bypass this and not throw an error?


1595770762168.png
 

Attachments

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,669
Office Version
365
Platform
Windows
In that case change IDNum to variant.
 

ShuStar

Board Regular
Joined
Sep 9, 2015
Messages
67
Office Version
2010
Platform
Windows
Thank you, yes thats resolved it!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,669
Office Version
365
Platform
Windows
My pleasure.
 

ShuStar

Board Regular
Joined
Sep 9, 2015
Messages
67
Office Version
2010
Platform
Windows
Hi Fluff,

Separate the above code - but linked to it, i wanted to create a new Sub to try do the following:

The Ask: For each column, starting from D3 to Column X, replace the values of Y in each row reflect that of the column header.
ie if D3 Column header is Banana and D4 has Y, then it should replace D4 to be Banana, but dynamically do this as the Columns and Rows always change when we execute original code.

Current view:

IDNameCountBananaApplesNectarine
12345John1Y
12345Sara3YYY
12345Alex2YY


New Sub to amend to:

IDNameCountBananaApplesNectarine
12345John1Banana
12345Sara3BananaApplesNectarine
12345Alex2BananaApples



My code where I can't seem to get passed Range(startcolumn, "D"&lastrow).select as it keeps erroring out here:

Sub changeYtorespectiveColumnName()

Dim lastrow As Long
Dim lastcolumn As Long
Dim startcolumn As Variant
Dim startingRange As Range
Dim InbtColumn As Long
Dim selectrange As Long
Dim i As Integer

Dim sht As Worksheet

Set sht = Sheets("Matrix")

lastrow = sht.Cells.SpecialCells(xlCellTypeLastCell).Row - 1
lastcolumn = sht.Cells.SpecialCells(xlCellTypeLastCell).Column
startcolumn = Cells(3, 4).Range
Range(startcolumn, "D" & lastrow).Select
startingRange.Select

'For each column
For j = 1 To lastcolumn

'scan through each row and if the value equals Y, then replace it with the title of the column header
For i = 1 To lastrow


Next i

Next j



End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,669
Office Version
365
Platform
Windows
You will need to start a new thread for this. When you do please ensure that you post the code using code tags.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,350
Messages
5,486,345
Members
407,541
Latest member
Emilybuhman

This Week's Hot Topics

Top