Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Ideal Paradigm
Aug 7, 2005
Trouble at the old mill
Is there a way to filter a set of values from one sheet and apply it to two other sheets in the same workbook?

I have an excel workbook that has three sheets that have around 10,000 rows of values. The first column are anonymized identification numbers along with different values attached to those ID numbers in the neighboring columns (same row). For example, there would be an ID number in the first column, and then attached values such as favorite ice cream flavor in the second column, and then favorite animal in the third column (these aren't what are actually in the cells next to them, they're just numerical values).

The problem is that the ID numbers in the first two sheets don't all match the ID numbers in the third sheet. Hundreds to thousands of the ID numbers in the first and second sheet don't match the third sheet.

I would like to take the ID numbers from the third sheet, keep the attached values such as ice cream flavor and favorite animal, and then filter out the first and second sheet so that only ID numbers from the third sheet appear in the first and second sheet. The third sheet will be the "anchor" sheet where those are the identification numbers that matter. The first and second sheet only have some ID numbers that match the third sheet, and the first and second sheet don't necessarily match each other, there may be minimal overlap.

Is there a way to perform this type of filtering from one sheet to another? Thank you!

Adbot
ADBOT LOVES YOU

Ideal Paradigm
Aug 7, 2005
Trouble at the old mill
Thank you all for the help so far, I only know the very basics for Excel. If you're willing to bear with me, I have some other questions.

HootTheOwl posted:

If the id numbers don't match how do you know the row in sheet 1/2 truly match a row in sheet 3?
Because to do this you need to know the relationship between tuples and you've saying the primary key is unreliable

I have looked through the three sheets in the workbook and by randomly searching numbers in the first two sheets, there are some ID numbers from sheets 1 and 2 that match the third sheet.

Ninja.Bob posted:

Yes, but the only method I know uses vba. There may be better solutions than this.

I've assumed that there are 3 sheets each with a table, i.e. Sheet1 > Table1, Sheet2 > Table2, Sheet3 > Table3, and each table has a column named ID.

Firstly, you want to be able to catch an event that fires when the source table's (Sheet3) filter updates. Excel doesn't have a built in event for this, so create a new sheet (Sheet4) that has a single formula in it =SUBTOTAL(2,Table3[ID]) . When Sheet3's table is filtered this formula gets recalculated. This allows us to use Sheet4's Worksheet_Calculate() method to handle the filter update. You can hide this sheet after setting it up.

The rest is just getting the filtered ids from Sheet3 and applying them to sheets 1 & 2. In the vba editor add this code to the Sheet4 object, and update the constant values to suit your object names:

Visual Basic .NET code:
Option Explicit

' source table
Const sSheet As String = "Sheet3"
Const sTable As String = "Table3"
Const sColumn As String = "ID"

' target table 1
Const t1Sheet As String = "Sheet1"
Const t1Table As String = "Table1"
Const t1Column As String = "ID"

' target table 2
Const t2Sheet As String = "Sheet2"
Const t2Table As String = "Table2"
Const t2Column As String = "ID"


' run when Sheet4 is calculated i.e. when Table3 is filtered.
Private Sub Worksheet_Calculate()
    
    ' get an array of filtered ids from table 3.
    Dim ids() As String
    ids = GetFilteredIds
    
    'apply the filter to other tables
    ThisWorkbook.Worksheets(t1Sheet).ListObjects(t1Table).ListColumns(t1Column).DataBodyRange.AutoFilter 1, ids, xlFilterValues
    ThisWorkbook.Worksheets(t2Sheet).ListObjects(t2Table).ListColumns(t2Column).DataBodyRange.AutoFilter 1, ids, xlFilterValues
    
End Sub

Private Function GetFilteredIds() As Variant
    
    Dim sourceRange As Range
    Set sourceRange = ThisWorkbook.Worksheets(sSheet).ListObjects(sTable).ListColumns(sColumn).DataBodyRange
    
    Dim ids() As String
    ReDim ids(0 To sourceRange.Count)
        
    Dim n As Long
    n = 0
    
    Dim c As Range
    For Each c In sourceRange
        If Not c.EntireRow.Hidden Then
            ids(n) = c.Value
            n = n + 1
        End If
    Next
    
    ReDim Preserve ids(0 To n - 1)
    GetFilteredIds = ids

End Function

Apologies, I'm not familiar with the VB code. Should I look this up on YouTube on how to input this?

And yes, each sheet just has the values inputted into the cells with it's respective ID number in the first column, along with attached values in the subsequent columns. The third sheet will sometimes have the same ID numbers in multiple rows (always grouped together), with dates attached to when measurements were taken, along with the quantity of the measurement in the third column that lines up with the respective date.

esquilax posted:

Are there any issues with adding in a hidden dummy column on Sheet 1 and Sheet 2 that reads Y/N based on whether the ID appears in Sheet 3?

You can do this using an If(iserror(vlookup())) formula. Vlookup returns an error, if the value you are trying to find doesn't exist in the lookup table.

No, I can create a copy of the excel workbook and modify as needed apart from the original copy.

The problem is that sometimes on the third sheet, there are multiple entries for the same ID number in different rows (they're always grouped together though), since the columns next to those ID numbers will have a changed value. For instance, let's say the ID number is 1002, there may be three rows with 1002 all grouped together, one row after another, and then the three columns will have different dates since those dates signify when a measurement was taken. Then in the third column the quantity for that measurement is inputted for those ID numbers.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply