【问题标题】:How to SELECT FROM WHERE in excel? Return rows from database如何在excel中从哪里选择?从数据库返回行
【发布时间】:2019-07-11 12:49:22
【问题描述】:

我想在 excel 中返​​回满足某些条件的表中的行。

ProdGroup   Width (mm)  Diameter (mm)   Date
Prod1           1120            1000   2016-01-11
Prod2           600             1000   2017-10-18
Prod1           930             800    2015-04-11
Prod3           1250            1200   2016-04-18
Prod2           840             1000   2019-06-27
Prod2           840              900   2018-03-21

我想相当于:“SELECT * FROM Table WHERE ProdGroup = "Prod2" AND Diameter = 1000" 在excel中。我的想法是我将值输入到两个单元格中,然后根据我在两个单元格中写入的内容返回行。

我曾尝试使用 =INDEX() 函数,但我只设法搜索仅匹配 1 个条件的行。此外,我只成功返回了一行。

=INDEX(B2:D6,MATCH(A10,A2:A6,0),1)

这只给了我一个 IN 参数。使用 Prod2 只会返回一行。

使用输入“Prod2”和 1000:

ProdGroup         Diameter
Prod2             1000

我想要这样的结果:

Prod2           600             1000            2017-12-18
Prod2           840             1000            2019-06-27

我不知道该怎么做。有人可以帮忙吗?

亲切的问候 小伙子

【问题讨论】:

  • 使用高级过滤器。

标签: excel select excel-formula row where-clause


【解决方案1】:

这是一个选项:

I2中的公式:

=IFERROR(INDEX(A$1:A$7,SMALL(IF((($A$2:$A$7=$F$2)*($C$2:$C$7=$G$2))>0,ROW($A$2:$A$7),""),ROW(1:1))),"")

通过CtrlShiftEnter

确认

向下和向右拖动...

【讨论】:

    【解决方案2】:

    如果您正在寻找自动化示例,您可以使用宏。这将避免您不得不向下拖动公式并提高数据的完整性,因为它降低了出错的可能性并消除了公式不包含整个数据单元格范围或被输入的可能性。

    如果您不熟悉 VBA...

    • 将工作簿另存为 .xlsm 文件(启用宏的工作簿)
    • 按 alt + F11 打开 VBE 窗口
    • 在屏幕左侧的项目资源管理器中右键单击您的文件名
    • 选择插入 > 模块
    • 在新创建的模块中,将此代码粘贴到下方。
    • 您可以通过转到“开发人员”>“宏”或从那里选择“选项”并分配您选择的键盘快捷键来运行宏。

    要为您的特定数据自定义此设置,您只需在“设置变量”部分将引号内的文本替换为您的特定表名称和命名范围。

    在下面的示例中,我为将用作过滤器参数的单元格使用了命名范围。我还将要过滤的数据格式化为表格。表名是 t_ProductSizes。

    此片段显示过滤结果和活动单元格的命名范围。

    Sub FilterTable_ByCellReference()
    
    '~~> Declare the variables
    Dim t As ListObject
    Dim p As String
    Dim w As String
    Dim dm As String
    Dim dt As String
    
    '~~> Set the variables
    '~~> Replace the text inside the quotation marks with your table names and named ranges.
    
    Set t = ActiveSheet.ListObjects("t_ProductSizes")
    p = Range("filter_product") 'named range
    w = Range("filter_width") 'named range
    dm = Range("filter_diameter") 'named range
    dt = Range("filter_date") 'named range
    
    '~~> Check your variables if you like (view the immediate window)
    Debug.Print "t = " & t.Name
    Debug.Print "p = " & p
    Debug.Print "w = " & w
    Debug.Print "dm = " & dm
    Debug.Print "dt = " & dt
    
    '~~> Remove existing filters.
        t.Range.AutoFilter Field:=1
    
    '~~> Filter the table based on the values specified in the filter cells.
    '~~> Only filter the corresponding column if the cell is not blank.
    
        With t.Range
    
            If p <> "" Then
    
                .AutoFilter Field:=1, _
                    Criteria1:=p
    
            End If
    
            If w <> "" Then
    
                .AutoFilter Field:=2, _
                    Criteria1:=w
    
            End If
    
            If dm <> "" Then
    
                .AutoFilter Field:=3, _
                    Criteria1:=dm
    
            End If
    
            If dt <> "" Then
    
                .AutoFilter Field:=4, _
                    Criteria1:=DateValue(dt)
    
            End If
    
        End With
    
    
    '~~> Clear the variables from memory.
    Set t = Nothing
    p = vbNullString
    w = vbNullString
    dm = vbNullString
    dt = vbNullString
    
    End Sub
    

    【讨论】:

    • 你好,珍!非常感谢您的回答。作为一个对 VB 完全陌生的人,我非常感谢您的详尽解释。我在应用你的宏时遇到了一些麻烦,如果你能告诉我我做错了什么,我将不胜感激。尝试使用您的宏时,我不断收到此错误消息:“运行时错误'9'。下标超出范围”我没有更改代码并更改了单元格和表格的名称,以便它们适合代码。这就是我定义名称的方式:imgur.com/a/2D6l8Lz 我做错了什么? :)) KR pbdude
    • @pbdude 不客气。我今晚可以看看再回复。
    • @pbdude 脚本超出范围通常意味着您引用的任何内容都不存在。你能检查你的表名和命名范围的拼写吗?
    • 好的,太好了!对此,我真的非常感激。我检查了拼写,它的拼写方式和你在代码中写的一样。
    猜你喜欢
    • 1970-01-01
    • 2018-09-24
    • 2014-06-29
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多