Consulting

Results 1 to 3 of 3

Thread: Solved: Loop through a range and hide active Column

  1. #1
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location

    Solved: Loop through a range and hide active Column

    I'm trying to loop through a range and hide the column where the active cell is located if the column value is < 1 or "" or $0.00

    This is the code that I have. It does not delete the columns even though the value is 0

    [vba]

    Dim MyCell As Range, MyRange As Range

    Set MyRange = Range("M204:U204")
    For Each MyCell In MyRange
    If MyCell.Value < 1 Or MyCell.Value = "" Then

    ActiveCell.EntireColumn.Hidden = True
    End If
    Next MyCell

    [/vba]

    Can anyone tell me why this code is not hiding the column

    Thanks

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi Simora,

    Change ActiveCell to MyCell:

    Sub exa()
    Dim MyCell As Range, MyRange As Range
     
        Set MyRange = Range("M204:U204")
        For Each MyCell In MyRange
     
            If MyCell.Value < 1 Or MyCell.Value = "" Then
     
                MyCell.EntireColumn.Hidden = True
            End If
        Next MyCell
    End Sub
    ActiveCell is not changing, as the ActiveCell is just whatever cell is selected on the ActiveSheet.

    BTW, you might want to be careful in titling your thread. Deleting columns is of course a bit different than hiding them for a bit.

    Hope that helps,

    Mark

  3. #3
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    Thanks Mark:

    The Delete column bit was a Typo. My worksheet has gremlins living there now.

    Thanks again.
    Last edited by Aussiebear; 06-21-2010 at 02:10 AM. Reason: Edited title to reflect the content of the thread

Posting Permissions

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