+ Reply to Thread
Results 1 to 9 of 9

Error when no cells are filtered for selection

Hybrid View

josnah Error when no cells are... 01-04-2008, 12:08 AM
royUK Using SpecialCells will throw... 01-04-2008, 03:00 AM
josnah hi RoyUK, Many thanks for... 01-06-2008, 02:37 AM
royUK You can add criteria, use the... 01-06-2008, 03:58 AM
josnah Hi this is the code i need to... 01-06-2008, 08:38 AM
  1. #1
    Registered User
    Join Date
    02-19-2004
    Posts
    57

    Question Error when no cells are filtered for selection

    Hi I'm faced with an error message when no cells are filtered for selection.
    How can I correct the error by getting it to continue with the next worksheet?

    Thanks in advance!

    Private Sub FormatFilteredRows()
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        Set MySheets = ActiveWindow.SelectedSheets
        For Each Worksheet In MySheets
            Worksheet.Select
            ActiveSheet.UsedRange
            LastRow = ActiveCell.SpecialCells(xlLastCell).Row
            Rows("3:3").Select
            Selection.AutoFilter
            Selection.AutoFilter Field:=15, Criteria1:="=*Reclass*", Operator:=xlOr, Criteria2:="=*Contra*"
            Range(Range("C4"), Range("C3").End(xlToRight).Offset(LastRow - 4, -2)).Select
            Selection.SpecialCells(xlCellTypeVisible).Select
            Selection.SpecialCells(xlCellTypeConstants, 23).Select
            With Selection.Interior
                .ColorIndex = 24
                .Pattern = xlSolid
            End With
            Selection.AutoFilter
        Next
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
    End Sub

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Using SpecialCells will throw an error if there are no cells meeting the type specified you need to include error handling. You also need to declare your variables. Try this, note it doesn't select any sheets or ranges so should run more efficiently.
    Option Explicit
    
    Private Sub FormatFilteredRows()
        Dim ws     As Worksheet
        Dim uRng   As Range
        Dim rng    As Range
        Dim LastRow As Long
        Dim MySheets
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            Set MySheets = ActiveWindow.SelectedSheets
            For Each ws In MySheets
                On Error Resume Next
                LastRow = ws.Cells(Rows.Count, 3).End(xlUp)
                Set uRng = ws.Cells(4, 3).CurrentRegion
                uRng.AutoFilter
                uRng.AutoFilter Field:=15, Criteria1:="=*Reclass*",Operator:=xlOr, Criteria2:="=*Contra*"
                'I don't know what you intend with this line
                'if you want to exclude the header row, then use Resize
                'Range(Range("C4"), Range("C3").End(xlToRight).Offset(LastRow - 4, -2)).Select
                Set rng = uRng.Offset(1, 0).Resize(uRng.Rows.Count - 1, _
                                                   uRng.Columns.Count)
                Set rng = rng.SpecialCells(xlCellTypeVisible)
                Set rng = rng.SpecialCells(xlCellTypeConstants, 23)
                With rng.Interior
                    .ColorIndex = 24
                    .Pattern = xlSolid
                End With
                uRng.AutoFilter
                On Error GoTo 0
            Next
            .ScreenUpdating = True
            .DisplayAlerts = True
        End With
    End Sub
    Last edited by royUK; 01-04-2008 at 05:06 AM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    02-19-2004
    Posts
    57
    hi RoyUK,

    Many thanks for your response!

    I've figured out how to resize the range with your help.
    I can't post the table here (data sensitivity), but just to explain this is how the table structure is.
    Col A & B: Two levels of Column Headings
    Col C onwards: Jan to Dec Data (this depends on which month we are at)
    2nd Last Col: Grand total
    Last Col: Remarks Col (this is where I apply the filters and format the Data columns accordingly.)
    The table starts at Row 3.

    The code you posted works without any error... I've another Qn...

    How can I filter with more criteria and format the corresponding cells accordingly?

    Sub FormatFilteredRows()
        Dim ws     As Worksheet
        Dim uRng   As Range
        Dim rng    As Range
        Dim LastRow As Long
        Dim MySheets
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            Set MySheets = ActiveWindow.SelectedSheets
            For Each ws In MySheets
                On Error Resume Next
                LastRow = ws.Cells(Rows.Count, 3).End(xlUp)
                
                Set uRng = ws.Cells(4, 3).CurrentRegion
                uRng.AutoFilter Field:=16, Criteria1:="=*Reclass*", Operator:=xlOr, Criteria2:="=*Contra*"
                Set rng = uRng.Offset(1, 2).Resize(uRng.Rows.Count - 2, _
                                                   uRng.Columns.Count - 3)
                Set rng = rng.SpecialCells(xlCellTypeVisible)
                Set rng = rng.SpecialCells(xlCellTypeConstants, 23)
                With rng.Interior
                    .ColorIndex = 24
                    .Pattern = xlSolid
                End With
                
    '            uRng.AutoFilter Field:=16, Criteria1:="=*Pending*"
    '            Set rng = uRng.Offset(1, 2).Resize(uRng.Rows.Count - 2, _
    '                                               uRng.Columns.Count - 3)
    '            Set rng = rng.SpecialCells(xlCellTypeVisible)
    '            Set rng = rng.SpecialCells(xlCellTypeConstants, 23)
    '            With rng.Interior
    '                .ColorIndex = 34
    '                .Pattern = xlSolid
    '            End With
    '
    '            uRng.AutoFilter Field:=16, Criteria1:="=*Completed*"
    '            Set rng = uRng.Offset(1, 2).Resize(uRng.Rows.Count - 2, _
    '                                               uRng.Columns.Count - 3)
    '            Set rng = rng.SpecialCells(xlCellTypeVisible)
    '            Set rng = rng.SpecialCells(xlCellTypeConstants, 23)
    '            With rng.Interior
    '                .ColorIndex = 40
    '                .Pattern = xlSolid
    '            End With
    
                uRng.AutoFilter
                On Error GoTo 0
            Next
            .ScreenUpdating = True
            .DisplayAlerts = True
        End With
    End Sub

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    You can add criteria, use the macro recorder to get sample code whilst filtering the data. Post back for help adapting the generated code.

  5. #5
    Registered User
    Join Date
    02-19-2004
    Posts
    57
    Hi this is the code i need to be adapted to the macro...

    Sub Macro1()
    '
    ' Macro1 Macro
    ' Macro recorded 06/01/2008 by Josnah
    '
    
    '
        Range("A3").Select
        Selection.AutoFilter Field:=16, Criteria1:="=*Reclass*", Operator:=xlOr, Criteria2:="=*Contra*"
        Selection.CurrentRegion.Select
        Selection.SpecialCells(xlCellTypeVisible).Select
        Selection.SpecialCells(xlCellTypeConstants, 1).Select
        With Selection.Interior
            .ColorIndex = 24
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
        End With
        ActiveSheet.ShowAllData
        Selection.AutoFilter Field:=16, Criteria1:="=*Pending*"
        Range("A3").Select
        Selection.CurrentRegion.Select
        Selection.SpecialCells(xlCellTypeVisible).Select
        Selection.SpecialCells(xlCellTypeConstants, 1).Select
        With Selection.Interior
            .ColorIndex = 34
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
        End With
        ActiveSheet.ShowAllData
        Selection.AutoFilter Field:=16, Criteria1:="=*Completed*", Operator:= _
            xlAnd
        Range("A3").Select
        Selection.CurrentRegion.Select
        Selection.SpecialCells(xlCellTypeVisible).Select
        Selection.SpecialCells(xlCellTypeConstants, 1).Select
        Selection.Interior.ColorIndex = 40
        ActiveSheet.ShowAllData
    End Sub

  6. #6
    Registered User
    Join Date
    02-19-2004
    Posts
    57
    Hi RoyUK,

    There seems to be sth wrong when I run the macro.
    For worksheets with no cells filtered at all, all the cells which are hidden by the filter still get formatted with the cell colour.
    I've tried but am not able to understand why that's happening...
    pls help

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1