Consulting

Results 1 to 6 of 6

Thread: Help -needed call named range based on cell value

  1. #1
    VBAX Regular
    Joined
    Dec 2015
    Posts
    11
    Location

    Question Help -needed call named range based on cell value

    Hi all,

    I have a problem and since I am not that great with VBA hope that someone can help me .

    I would like to have a code that calls specific named range (e.g. "UK" or range "AM:BA") based on the cell value. So if e.g. $L$5=1 then show AM:BA in that sheet and hide everything else?

    Any help is much appreciated!

    Thank you!

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address <> "$A$1" Then Exit Sub
    On Error GoTo Exits
    Application.EnableEvents = False
    'unhide all columns
    Columns("A:XFD").Hidden = False
    Select Case Target
    Case 1
        Columns("B:H").Hidden = True
        Columns("Z:XFD").Hidden = True
    Case 2
        Columns("B:D").Hidden = True
        Columns("K:Z").Hidden = True
    Case 3
        Columns("B:XFD").Hidden = False
    End Select
    Exits:
    Application.EnableEvents = True
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Dec 2015
    Posts
    11
    Location
    Thank you! I have tried adding it into my sheet but sth still seems missing. So my range cell is L5 and I tried copying your code and adding "Select Case Range("L5")" instead of "Select Case Target ", do you know what is wrong in my code?

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Please post your code
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Regular
    Joined
    Dec 2015
    Posts
    11
    Location
    So I want show/hide columns based on the value of the cell L5. It is your code, I have just tried to change range.

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address <> "$A$1" Then Exit Sub
        On Error GoTo Exits
        Application.EnableEvents = False
         'unhide all columns
        Columns("A:XFD").Hidden = False
        Select Case Range ("L5")
        Case 1
            Columns("B:H").Hidden = True
            Columns("Z:XFD").Hidden = True
        Case 2
            Columns("B:D").Hidden = True
            Columns("K:Z").Hidden = True
        Case 3
            Columns("B:XFD").Hidden = False
        End Select
    Exits:
        Application.EnableEvents = True
    End Sub
    Last edited by SamT; 05-28-2016 at 05:10 PM.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If you step through the code you'll find that this is the issue
    If Target.Address <> "$A$1" Then Exit Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Tags for this 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
  •