Return 列与所选颜色匹配的行 EXCEL

Return rows where the columns match the colours selected EXCEL

我相当确定有一种更优雅的方法可以做到这一点,但我似乎看不到它,或者如果它就在我面前盯着我,我无法理解它。

我在 "NEW DB" 中有一个 table 数据,其中列出了 D4:W 中的数字和空白单元格,然后我有一个名为 "Colour List" 的 sheet,其中您可以 select 您想要查找的颜色,然后我希望在 "Colour List" 的 "Formulas" 部分中发生的是列出包含这些特定颜色的每个公式,即纯黑色和纯白色应该return公式3到20,纯黑和纯红应该Return公式42到60.

我觉得它与 INDEX MATCH MATCH 和 ROWS 以及搜索 headers 有关,但我不知道如何让它们工作,因为我总是得到 #N/A错误。

请注意,这将扩展到四次搜索。

这是一份 link 文件,如果有人能提供帮助,我将不胜感激,因为我花了一些时间才整理这些数据sheet

https://mega.nz/file/Oq4xHDCD#6FBZdIVk0Fn8orIiP-DJ2e-ODD_abHtO6k9WwGfJT5c

谢谢

可爱的练习册。看到这样的创作真的很鼓舞人心也很有趣:)!

我还没有用 excel 公式解决这个问题,因为你似乎有很多变量,我认为添加更多颜色会变得更复杂。请随意使用我的答案或忽略它 ;)

但是,我想给你一个 VBA 解决方案,它可能会加快工作簿的速度,因为大型工作簿的矩阵计算需要时间来执行...

一些反馈:

  • 您在 "New DB" 工作sheet 中进行了单元格设置,这使得工作簿为 12 MB。删除从第 15990 行到最后一行的行,将工作簿从 12 MB 减少到 1.5mb,并且工作簿速度也相当快。
  • PWO,你在 "New DB" sheet.sheet 这个词的前面有一个 space
  • 我强烈建议使用 vlookup 在 sheet "Colour List" 中查找颜色的简称。要维护长公式很痛苦,请参阅我在单元格 A5 中的建议。
  • 我冒昧地重组了你的部分作品sheet,我只是觉得有点混乱......
  • 为 "Sheet1" 中的所有颜色添加了简称。我个人认为您应该将其重命名为 "Mapping sheet" 或将内容移至设置。

最后一句话。了不起的工作:)!!


计算公式代码的代码

Link 到工作簿

Option Explicit

Sub Calculate_Formula_Code()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim Colour1_Col As Long
Dim Colour2_Col As Long
Dim LastRow_New_DB As Long
Dim Result_Start_Row As Long
Dim Colour_Group_Row As Long

Dim i As Long
Dim j As Long

Set ws1 = Sheets("Colour List") 'Define the Colour list sheet
Set ws2 = Sheets("New DB") 'Define the New DB sheet

Result_Start_Row = 10 'Start from row

LastRow_New_DB = ws2.Cells(Rows.Count, 1).End(xlUp).Row 'Find the last row in the New DB Sheet

ws1.Range(ws1.Cells(9, "A"), ws1.Cells(32, "A")).Value = 0 'Clear previous values


If ws1.Cells(5, 2).Value <> "" Then 'If Both Colour 1 and Colour 2 are selected
    Colour1_Col = Application.Match(ws1.Cells(5, 1).Value, ws2.Range("A2:W2"), 0) 'Check which column colour 1 exists in
    Colour2_Col = Application.Match(ws1.Cells(5, 2).Value, ws2.Range("A2:W2"), 0) 'Check which column colour 2 exists in


    For i = 4 To LastRow_New_DB 'Loop from row 4 to last row in the sheet New DB
            If i = 4 And (ws1.Cells(5, 1).Value = "PB" Or ws1.Cells(5, 2).Value = "PB") Then 'Check if any the Colours are black, If yes, then add pure black to 1st row
                    ws1.Cells(Result_Start_Row - 1, "A").Value = ws2.Cells(i, "A").Value
            ElseIf ws2.Cells(i, Colour1_Col).Value <> "" And ws2.Cells(i, Colour2_Col).Value <> "" Then 'Check for the row where there is a value for both of the selected colours
                If i >= 385 Then 'from row 385 the number of rows for a group colour is 19, before it's 18
                    Colour_Group_Row = 18 'How many rows to loop through to add colours
                Else
                    Colour_Group_Row = 19 'How many rows to loop through to add colours
                End If
                For j = i To i + Colour_Group_Row 'For the group, add all the formula code in the Colour list sheet
                    ws1.Cells(Result_Start_Row, "A").Value = ws2.Cells(j, "A").Value 'Copy from New DB sheet to the Colour List sheet
                    Result_Start_Row = Result_Start_Row + 1 'Add one more row for every iteration
                Next j
            Exit Sub
            End If
    Next i

Else 'If only Colour 1 is selected
    Colour1_Col = Application.Match(ws1.Cells(5, 1).Value, ws2.Range("A2:W2"), 0) 'Check which column colour 1 exists in


    For i = 4 To LastRow_New_DB 'Loop from row 4 to last row in the sheet New DB
        If i = 4 And (ws1.Cells(5, 1).Value = "PB" Or ws1.Cells(5, 2).Value = "PB") Then 'Check if any the Colours are black, If yes, then add pure black to 1st row
                ws1.Cells(Result_Start_Row - 1, "A").Value = ws2.Cells(i, "A").Value
        ElseIf ws2.Cells(i, Colour1_Col).Value <> "" Then 'Check for the row where there is a value for both of the selected colours
            If i >= 385 Then 'from row 385 the number of rows for a group colour is 19, before it's 18
                Colour_Group_Row = 18 'How many rows to loop through to add colours
            Else
                Colour_Group_Row = 19 'How many rows to loop through to add colours
            End If
            For j = i To i + Colour_Group_Row 'For the group, add all the formula code in the Colour list sheet
                ws1.Cells(Result_Start_Row, "A").Value = ws2.Cells(j, "A").Value 'Copy from New DB sheet to the Colour List sheet
                Result_Start_Row = Result_Start_Row + 1 'Add one more row for every iteration
            Next j
        Exit Sub
        End If
    Next i
End If

MsgBox ("Complete")

End Sub