+ Reply to Thread
Results 1 to 3 of 3

Macro to hide columns based on contents of two cells

  1. #1
    Registered User
    Join Date
    09-10-2006
    Posts
    7

    Macro to hide columns based on contents of two cells

    Hi,

    I am having trouble creating this macro:

    I am trying to create a macro that can hide particular columns in all sheets in the work book based on the contents of two different cells in the column. For example, if the macro should be able to hide all columns in the workbook where cells A5:Z5 say "DEC" and cell A6:Z6 says "PY". The macro should loop through all sheets and check for the values of those cells. Can anyone assist me with this?

    Thanks

  2. #2
    Registered User
    Join Date
    09-10-2006
    Posts
    7
    I have created this Macro that does part of the job.

    Cells.Find(What:="PY", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False).Activate
    If ActiveCell.Offset(-1) = "DEC" Then
    ActiveCell.EntireColumn.Hidden = True
    Else
    ActiveCell.EntireColumn.Hidden = False
    End If

    I nee to loop this through the entire workbook. Any advice?

  3. #3
    Forum Contributor
    Join Date
    11-11-2005
    Posts
    267
    Assuming your code works as expected, loop thus:

    Sub LoopWS()
    Dim sh as WorkSheet

    For each sh in WorkSheets

    sh.Cells.Find(What:="PY", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False).Activate
    If sh.ActiveCell.Offset(-1) = "DEC" Then
    sh.ActiveCell.EntireColumn.Hidden = True
    Else
    sh.ActiveCell.EntireColumn.Hidden = False
    End If

    Next

    End Sub
    HTH
    Myles

    ...constantly looking for the smoother pebble while the whole ocean of truth lies before me.

+ 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