Query my excel worksheet with VBA

cwiggo picture cwiggo · Sep 5, 2013 · Viewed 24.9k times · Source

Is it possible to query a worksheet using 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

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

Thanks

Answer

Joe picture Joe · Sep 5, 2013

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