Consulting

Results 1 to 6 of 6

Thread: Need to show and hide named ranges based on cell value

  1. #1
    VBAX Newbie
    Joined
    Mar 2020
    Posts
    4
    Location

    Need to show and hide named ranges based on cell value

    I have a spreadsheet with 10 named ranges. It contains a cell that corresponds to one of the named ranges and will change, based on input by the user. I want to show the named range based on the value of that cell, and hide the other ranges. Then when the value changes, I want to show THAT named range and hide the others. How do I do this?
    TIA
    Attached Files Attached Files

  2. #2
    VBAX Newbie
    Joined
    Mar 2020
    Posts
    4
    Location
    I have created 10 separate macros, one for each named range. Is there a way to call the correct code depending on the value of the cell H1? And have it call the correct sub when H1 changes?

  3. #3
    VBAX Newbie
    Joined
    Mar 2020
    Posts
    4
    Location
    I've added this to the Change declarations, but I get the error Sub or Function not defined. Forgive the newbie question, but how do I call the macro?
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$B$4:$B$499" Then
    Select Case Target.Value
    Case "PCNLK"
    Call Macro 'Sub PCNLK'
    
    
    Case "PNP"
    Call Macro 'Sub PNP'
    
    
    Case "ONP"
    Call Macro 'Sub ONP'
    
    
    Case "ODkNLK"
    Call Macro 'Sub ODkNLK'
    
    
    Case "ODkP"
    Call Macro 'Sub ODkP'
    
    
    Case "ONNLK"
    Call Macro 'Sub ONNLK'
    
    
    Case "PCP"
    Call Macro 'Sub PCP'
    
    
    Case "PDkNLK"
    Call Macro 'Sub PDkNLK'
    
    
    Case "PDkP"
    Call Macro 'Sub PDkP'
    
    
    Case "PNNLK"
    Call Macro 'Sub PNNLK'
    End Select
    End If
    End Sub
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    
    End Sub
    Last edited by Paul_Hossler; 03-27-2020 at 05:06 PM.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    I added CODE tags to your macro above. My sig has instructions and a link to our FAQs. Take a minute are read through

    Not tested, but something like this might work. It's in the QC worksheet code module

    Option Explicit
    
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    
        If Intersect(Target, Range("B4:B499")) Is Nothing Then Exit Sub
    
    
        Application.EnableEvents = False '   <<<<<<<<<< don't  want to keep triggering this event
        
        Select Case Target.Cells(1, 1).Value 
            Case "PCNLK"
                Call PCNLK
            Case "PNP"
                Call PNP
            Case "ONP"
                Call ONP
            Case "ODkNLK"
                Call ODkNLK
            Case "ODkP"
                Call ODkP
            Case "ONNLK"
                Call ONNLK
            Case "PCP"
                Call PCP
            Case "PDkNLK"
                Call PDkNLK
            Case "PDkP"
                Call PDkP
            Case "PNNLK"
                Call PNNLK
        End Select
        
        Application.EnableEvents = True
        
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    Look in the attachment. In this solution, G1: H1 cells are unnecessary, unless you use them for other purposes. You must correct the names of some ranges.

    Artik
    Attached Files Attached Files

  6. #6
    VBAX Newbie
    Joined
    Mar 2020
    Posts
    4
    Location
    Thank you! This worked perfectly after I corrected the rangenames!

Posting Permissions

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