简体   繁体   English

如何在两个工作表之间进行交叉引用,并在第三工作表上按发生次数对总和/组进行交叉引用?

[英]How do I cross-reference between two worksheets and sum/group by occurances on 3rd sheet?

I'm extremely new to excel worksheets and need some help figuring this issue out. 我对excel工作表非常陌生,需要一些帮助来解决此问题。 The first sheet Cookbook lists the departments and their respective recipes. 第一本Cookbook列出了各部门及其各自的食谱。 There's a many-to-many relationship between the departments and products. 部门和产品之间存在多对多关系。

在此处输入图片说明

I have another sheet Ingredients that lists the Ingedients and what Recipes they belong to: 我有另一片Ingredients ,其中列出了Ingedients什么Recipes他们属于:

在此处输入图片说明

I want to get the total of each category by ingredient in a third sheet titled Category x Ingredient Totals : 我想在标题为“ Category x Ingredient Totals ”的第三张表中按成分获取每个类别的Category x Ingredient Totals

在此处输入图片说明

The X in the Ingredients table represents 1 implicitly, but I'm not sure how to convert that (among other things). 成分表中的X隐式表示1,但是我不确定如何转换(除其他外)。

This will work for you but there may be someone out there that can do it better. 这将为您工作,但可能会有人可以做得更好。 I like my code to be quite verbose but strip it down as you need. 我希望我的代码很冗长,但可以根据需要将其精简。

With the first table, create a range over the data (not including the header) called " rngCategoriesToRecipes " 在第一个表中,在名为“ rngCategoriesToRecipes ”的数据(不包括标题)上创建一个范围

Next, create a range over the second table (this time including the headers) called " rngRecipetoIngredients ". 接下来,在第二个表(这次包括标题)上创建一个范围,称为“ rngRecipetoIngredients ”。

Thirdly, add the following code into a new module within the VBA editor ... 第三,将以下代码添加到VBA编辑器中的新模块中...

Public Function CalculateCategoryToIngredients( _
        ByVal rngCategoriesToRecipesMapping As Range, _
        ByVal rngRecipesToIngredientsMapping As Range, _
        ByVal strCategory As String, _
        ByVal strIngredient As String) As Long

    Application.Volatile

    Dim strThisCategory As String, strThisRecipe As String, strThisIngredient As String, strRecipes As String, objCell As Range
    Dim lngRow As Long, lngCol As Long, arrRecipes, bIsRelevant As Boolean, strThisValue As String, lngCount As Long

    ' Process each of the different categories and they're mappings to recipes.
    For i = 1 To rngCategoriesToRecipesMapping.Rows.Count
        strThisCategory = rngCategoriesToRecipesMapping.Cells(i, 1)
        strThisRecipe = rngCategoriesToRecipesMapping.Cells(i, 2)

        ' Get all of the recipes related to the category passed in.
        If strThisCategory = strCategory Then
            strRecipes = strRecipes + "," + strThisRecipe
        End If
    Next

    arrRecipes = Split(Mid(strRecipes, 2), ",")

    ' Now process the mapping from the recipes to the ingredients.
    For lngRow = 2 To rngRecipesToIngredientsMapping.Rows.Count
        For lngCol = 2 To rngRecipesToIngredientsMapping.Columns.Count
            strThisValue = Trim(rngRecipesToIngredientsMapping.Cells(lngRow, lngCol))
            strThisRecipe = rngRecipesToIngredientsMapping.Cells(lngRow, 1)
            strThisIngredient = rngRecipesToIngredientsMapping.Cells(1, lngCol)

            bIsRelevant = False

            For i = 0 To UBound(arrRecipes)
                If arrRecipes(i) = strThisRecipe Then
                    bIsRelevant = True
                    Exit For
                End If
            Next

            If bIsRelevant And strThisValue <> "" And strIngredient = strThisIngredient Then
                lngCount = lngCount + 1
            End If
        Next
    Next

    CalculateCategoryToIngredients = lngCount
End Function

Finally, add this formula in the first cell within the range that you want to calculate and fill down and across. 最后,将此公式添加到要计算并向下填充的范围内的第一个单元格中。

=CalculateCategoryToIngredients(rngCategoriesToRecipes,rngRecipetoIngredients,$A16,B$15)

Naturally, you'll need to replace the last 2 parameters ($A16 = "Category 1" and B$15 = "Ingredient 1") with the actual cells you need to reference, they're currently relative to my worksheet and where I placed my values. 自然,您需要用需要引用的实际单元格替换最后两个参数($ A16 =“类别1”和B $ 15 =“成分1”),它们当前相对于我的工作表以及我放置的位置我的价值观。

I hope it works for you. 我希望这个对你有用。 I think it did, it highlighted that your matrix intersection "Category 3", "Ingredient 3" is actually 2, not 1 ... as you illustrated. 我认为确实如此,它突出显示了矩阵交集“类别3”,“成分3”实际上是2,而不是1...。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM