使用 VBA 查询我的 Excel 工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18637376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:33:41  来源:igfitidea点击:

Query my excel worksheet with VBA

vbaexcel-vbaworksheetexcel

提问by cwiggo

Is it possible to query a worksheet using VBA?

是否可以使用 VBA 查询工作表?

data table

数据表

I want to be able to select all the values in the time column i.e. (00:00) WHERE the day equals for example: Saturday

我希望能够选择时间列中的所有值,即 (00:00) WHERE the day equals for example:Saturday

I there any way to do this, a tutorial would be really helpful.

我有什么方法可以做到这一点,教程会非常有帮助。

Thanks

谢谢

采纳答案by Joe

You can programmtically create an AutoFilter, then select the matching values:

您可以以编程方式创建自动筛选器,然后选择匹配的值:

Dim ws As Worksheet: Set ws = ActiveSheet

With ws
    .AutoFilterMode = False
    .Range("1:1").AutoFilter
    .Range("1:1").AutoFilter field:=2, Criteria1:="=Saturday", Operator:=xlAnd
    With .AutoFilter.Range
        On Error Resume Next ' if none selected
        .Offset(1).Resize(.Rows.Count - 1).Columns(2).SpecialCells(xlCellTypeVisible).Select
        On Error GoTo 0
    End With
    .AutoFilterMode = False
End With