Consulting

Results 1 to 9 of 9

Thread: Auto Filter

  1. #1
    VBAX Newbie
    Joined
    May 2007
    Posts
    4
    Location

    Auto Filter

    I have worksheet with some records. And I filter that records. Now some columns have this "<>" type of data. So what i want is that if that type of data in whole column then it should be hide. for that I am using following codes.

    [VBA]
    Dim Flag As Boolean
    Private Sub cmdProcess_Click()
    Dim i As Integer
    Dim j As Integer
    For j = 4 To CountVisCols
    For i = 2 To CountVisRows
    If ActiveSheet.Cells(i, j).Value <> "<>" Then
    Flag = False
    Exit For
    Else
    Flag = True
    End If
    Next
    If Flag = True Then
    Columns(j).Select
    Selection.EntireColumn.Hidden = True
    Flag = False
    End If
    Next
    End Sub
    Function CountVisRows() As Integer
    Rem By Pranav
    Rem It is use for counting visible rows.

    Dim rng As Range
    Set rng = ActiveSheet.AutoFilter.Range

    CountVisRows = rng.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
    End Function
    Function CountVisCols() As Integer
    Rem By Pranav
    Rem It is use for counting visible rows.

    Dim rng As Range
    Set rng = ActiveSheet.AutoFilter.Range

    CountVisCols = rng.Columns.Count
    End Function
    [/VBA]


    cmdProcess is the button Now problem is that it is counting unvisible(unfiletered) row also. so what is the change that we can do???

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub cmdProcess_Click()
    Dim i As Integer
    Dim j As Integer
    For j = 4 To CountVisCols
    Flag = True
    For i = 2 To CountVisRows
    If ActiveSheet.Rows(i).Hidden = False Then
    If ActiveSheet.Cells(i, j).Value <> "<>" Then
    Flag = False
    Exit For
    End If
    End If
    Next
    If Flag = True Then
    Columns(j).EntireColumn.Hidden = True
    Flag = False
    End If
    Next
    End Sub
    [/vba]

  3. #3
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi milind, I've put code tags around your code to make it readable - go to your post (#1) and click edit to see how it's done
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    And you indented it ... that is above and beyond

  5. #5
    VBAX Newbie
    Joined
    May 2007
    Posts
    4
    Location
    still having problem. Problem is that it is hidding all columns Thing is that after filtering we want to hidden those colums whoes data is like this "<>" in all rows

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You'll need to post an example, because that is what my testing did.

  7. #7
    VBAX Newbie
    Joined
    May 2007
    Posts
    4
    Location
    i have attach file for example
    Last edited by milind; 05-11-2007 at 09:07 AM. Reason: I have not given proper example

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It is your CountVisRows routine that is the proble.

    [vba]

    Private Sub cmdProcess_Click()
    Dim i As Integer
    Dim j As Integer

    For j = 4 To CountVisCols
    Flag = True
    For i = 2 To Me.Cells(Me.Rows.Count, j).End(xlUp).Row
    If Me.Rows(i).Hidden = False Then
    If Me.Cells(i, j).Value "" Then
    Flag = False
    Exit For
    End If
    End If
    Next
    If Flag = True Then
    Me.Columns(j).EntireColumn.Hidden = True
    Flag = False
    End If
    Next
    End Sub
    [/vba]

  9. #9
    VBAX Newbie
    Joined
    May 2007
    Posts
    4
    Location
    Thank you it works out

Posting Permissions

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