+ Reply to Thread
Results 1 to 4 of 4

Worksheet_Change that considers several cell values

  1. #1
    Registered User
    Join Date
    11-25-2019
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2010
    Posts
    6

    Worksheet_Change that considers several cell values

    Hi experts,

    I have a sheet that is divided into days and weeks, with data populated below. I'd like to be able to hide weeks (columns) that does not correspond to user-specified cell values in G18 and G19 - i.e. so the user in my case will have the option to only see two weeks at a time. To do this, I have the following formula:

    Private Sub Worksheet_Change(ByVal Target As Range)


    Dim cel As Range, Headers As Range
    Dim s As String

    Set Headers = Range("i1:ABJ1")

    If Not Intersect(Target, Range("G18:G19")) Is Nothing Then
    s = Target.Value
    Application.ScreenUpdating = False
    If s = "" Then
    Headers.EntireColumn.Hidden = False

    Else

    For Each cel In Headers
    cel.EntireColumn.Hidden = Not cel.Value = s
    Next cel
    End If
    Application.ScreenUpdating = True

    End If

    End Sub

    The problem is that it will ONLY consider the range where a change has been made when executing the command. So if I want to see, say, weeks 3 and 4, I'll enter 3 in range C18, which will then hide all columns (weeks) that isn't week 3. However, when I then type 4 in C19, it will take it as I now want to only see columns for week 4 instead and hide everything else, including week 3.

    Is there a way to work around that, so that the macro will consider BOTH C18 and C19 whenever a change is made to either one of them?

    Thanks in advance!

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Worksheet_Change that considers several cell values

    @JPM91, welcome ... but ... take some minutes to read the forum's rules, particularly the one concerning code tags ...!
    - Battle without fear gives no glory - Just try

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Worksheet_Change that considers several cell values

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 01-15-2020 at 11:29 AM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    11-25-2019
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2010
    Posts
    6

    Re: Worksheet_Change that considers several cell values

    Quote Originally Posted by AlphaFrog View Post
    Please Login or Register  to view this content.
    Thank you so much, Alpha - that's exactly what I was looking for!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Need a COUNT formula that considers critera
    By netgyver in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-21-2016, 09:37 PM
  2. SUM Formula that considers specific Text
    By LeithKaouas in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-14-2014, 04:51 AM
  3. Replies: 5
    Last Post: 07-15-2013, 01:24 AM
  4. [SOLVED] need calculation that considers a blank cell to be '100'
    By greyscale in forum Excel General
    Replies: 7
    Last Post: 07-16-2012, 09:48 PM
  5. Replies: 3
    Last Post: 06-12-2012, 04:01 PM
  6. Replies: 3
    Last Post: 11-15-2008, 03:32 PM
  7. [SOLVED] Excel considers my formulas as plain text
    By Kimmo Kallio in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 07-03-2006, 10:40 PM

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