5

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

Community
  • 1
  • 1
cwiggo
  • 2,541
  • 9
  • 44
  • 87
  • 1
    Use `AutoFilter` (filter the "Day" column as desired, then also filter the "Time" column) and then use the range `.SpecialCells(xlCellTypeVisible)` method. – David Zemens Sep 05 '13 at 13:43

1 Answers1

4

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
Joe
  • 6,767
  • 1
  • 16
  • 29