+ Reply to Thread
Results 1 to 4 of 4

How to make this code work for cells with formulas in them

  1. #1
    Registered User
    Join Date
    06-05-2013
    Location
    england
    MS-Off Ver
    Excel 2010
    Posts
    2

    How to make this code work for cells with formulas in them

    I currently have a workbook with 3 sheets, in sheet 1 is the matrix and in sheet 3 I have created a kind of search bar using VLOOKUP, I am trying to write a visual basic code so that when a 0 appears from this search bar that the entire column it is in hides itself, the current code I have is

    Option Explicit

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Cell As Range
    Set Target = Intersect(Target, Target.Parent.Rows(4))
    If Not Target Is Nothing Then
    For Each Cell In Target
    Cell.EntireColumn.Hidden = (Cell.Formula = 0)
    Next Cell
    End If
    End Sub

    this works when a 0 is entered in row 4 however it does not work when 0 is given from a formula within a cell, can anybody help??

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,791

    Re: How to make this code work for cells with formulas in them

    A change event only triggers when a change is made manually, as you have discovered, or through code, not as a result of a change of value of a formula.

    You need to monitor the cell(s) the formula depends on and, when one of them changes, check the value in the cell you really want to monitor.


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    06-05-2013
    Location
    england
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: How to make this code work for cells with formulas in them

    What would I need to change in the code to do this? I am quite a novice at VBA

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,791

    Re: How to make this code work for cells with formulas in them

    Could the Target you are monitoring have more than one cell?

    Which cells do the "Target" depend upon?

    Ideally, post a sample workbook.

    Regards, TMS

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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