+ Reply to Thread
Results 1 to 12 of 12

Show / hide chart based on value in cell assigned to chart (multiple charts)

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-26-2009
    Location
    Austin, TX
    MS-Off Ver
    Excel 2016
    Posts
    241

    Show / hide chart based on value in cell assigned to chart (multiple charts)

    Hi Everyone,

    I have 13 charts called Chart1, Chart2, Chart3, etc..

    Essentially, I want each of these charts to appear only if there is a text value in an assigned cell (i.e. if the assigned cell is not empty)

    Thus Chart1 should only appear if cell A1 has text (or alternatively, if cell A1 has the text value, "Chart1")

    Chart 2 should only appear if cell A2 has text.

    Chart 3 should only appear if cell A3 has text. etc...

    I'd prefer to use VBA than some of the other tricks I've seen out there, but I couldn't begin to write this in VBA myself.

    Can anyone help me out? Thanks!

    Jay

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: Show / hide chart based on value in cell assigned to chart (multiple charts)

    Start with this simple test:

    Sub test()
    If Len(Range("A1")) > 0 Then
        ActiveSheet.Shapes("Chart 1").Height = 300
        ActiveSheet.Shapes("Chart 1").Width = 400
    Else
        ActiveSheet.Shapes("Chart 1").Height = 0
        ActiveSheet.Shapes("Chart 1").Width = 0
    End If
    End Sub
    If it works as expected, just do it in a loop.
    Best Regards,

    Kaper

  3. #3
    Forum Contributor
    Join Date
    04-26-2009
    Location
    Austin, TX
    MS-Off Ver
    Excel 2016
    Posts
    241

    Re: Show / hide chart based on value in cell assigned to chart (multiple charts)

    Thanks, Kaper! This looks great, but I don't know how to enter this VBA code.

    Also, I forgot to mention that the chart (actually called, "Group 14") is on a worksheet called, "Charts". But cell A1 is on a worksheet called, "Data".

    I'll attach an image of what I tried to do.

    Can you advise me further on how to revise the code and where to enter it?

    Thanks!!

    Jay
    Attached Images Attached Images

  4. #4
    Forum Contributor
    Join Date
    04-26-2009
    Location
    Austin, TX
    MS-Off Ver
    Excel 2016
    Posts
    241

    Re: Show / hide chart based on value in cell assigned to chart (multiple charts)

    Can anyone else share a tip here? Thanks!

    Jay

  5. #5
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: Show / hide chart based on value in cell assigned to chart (multiple charts)

    Hi,

    Well, it could be in sheet code, but better place for it will be in a general module.
    So:
    * in VBA Editor (VBE) delete code from where it is now
    * From menu bar (in VBE) select Insert -> module
    * Paste in module slightly modified code (refering to sheets):

    Sub test()
    Sheets("Charts").activate
    If Len(Sheets("Data").Range("A1")) > 0 Then
        ActiveSheet.Shapes("Group 14").Height = 300
        ActiveSheet.Shapes("Group 14").Width = 400
    Else
        ActiveSheet.Shapes("Group 14").Height = 0
        ActiveSheet.Shapes("Group 14").Width = 0
    End If
    End Sub
    As for looping - it would be easy if names of graps are related to for instance cell rows
    like A2 - group 15
    A3 Group 16 etc
    then the code could be:
    Sub test()
    dim i as integer
    Sheets("Charts").activate
    for i = 1 to 5 'or more
      If Len(Sheets("Data").Range("A" & i)) > 0 Then
          ActiveSheet.Shapes("Group " & 13+i).Height = 300
          ActiveSheet.Shapes("Group " & 13+i).Width = 400
      Else
          ActiveSheet.Shapes("Group " & 13+i).Height = 0
          ActiveSheet.Shapes("Group " & 13+i).Width = 0
      End If
    next i
    End Sub

  6. #6
    Forum Contributor
    Join Date
    04-26-2009
    Location
    Austin, TX
    MS-Off Ver
    Excel 2016
    Posts
    241

    Re: Show / hide chart based on value in cell assigned to chart (multiple charts)

    Hi Kaper,

    Thanks very very much for the code you suggested. I have 3 more questions, if you don't mind:

    1) How do I have the code run only when the user clicks on the "Charts" tab to view the "Charts" worksheet? Can you take the first code snippet (without the loop) and show how that would be done?

    2) The reference cell (A1) is actually on the "Data" worksheet. How should the code be changed?

    3) The size of my object group (Group 14) isn't the same size (400 x 300) that you used as an example. However, I can't figure out how to determine the size of my object in pixels. In the Format Chart Area panel, it's defined as 6.44 inches (width) x 4.28 inches (height). How can I determine it's dimensions in pixels? Or, alternatively, can I use define the visible size as 100%?

    Thanks so so much for your guidance!

    JayUSA

  7. #7
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: Show / hide chart based on value in cell assigned to chart (multiple charts)

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

  8. #8
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: Show / hide chart based on value in cell assigned to chart (multiple charts)

    0) wow - 3 months delay :-)

    as for 1):

    Insert the code in "Charts" sheet code (right click on "Charts" tab, and from pop-up select View code):


    Private Sub Worksheet_Activate()
    If Len(Sheets("Data").Range("A1")) > 0 Then
        ActiveSheet.Shapes("Group 14").Height = 300
        ActiveSheet.Shapes("Group 14").Width = 400
    Else
        ActiveSheet.Shapes("Group 14").Height = 0
        ActiveSheet.Shapes("Group 14").Width = 0
    End If
    End Sub
    2) It's already there.

    3) You shall determine pixels per inch at screen. As there is no attachment I've not tested the following, but it could work.

    In standard module you can put the following code:
    'Written: June 03, 2008
    'Author:  Leith Ross
    'Summary: Returns the Logical pixels per inch for the active monitor screen.
    '         Both X and Y values are returned as an array with X as element(0).
    
     Public Declare Function GetDeviceCaps _
       Lib "GDI32.dll" _
         (ByVal hDC As Long, _
          ByVal nIndex As Long) As Long
     
     Public Declare Function GetDesktopWindow _
       Lib "User32.dll" () As Long
    
     Public Declare Function GetWindowDC _
       Lib "User32.dll" _
         (ByVal hWnd As Long) As Long
         
     Public Declare Function ReleaseDC _
       Lib "User32.dll" _
         (ByVal hWnd As Long, _
          ByVal hDC As Long) As Long
    
    
     Const LogPixelsX As Long = 88
     Const LogPixelsY As Long = 90
    
    Function GetLogicalPixels()
    
      Dim Desktop As Long
      Dim hDC As Long
      Dim Pixels(1)
      Dim RetVal As Long
      
       'To get Points Per Pixel divide 72/Pixels(n)
        hDC = GetWindowDC(GetDesktopWindow)
          Pixels(0) = GetDeviceCaps(hDC, 88)
          Pixels(1) = GetDeviceCaps(hDC, 90)
        RetVal = ReleaseDC(GetDesktopWindow, hDC)
        GetLogicalPixels = Pixels
      
    End Function
    (from here: https://www.mrexcel.com/forum/excel-...-per-inch.html) and then use it in your code, like:

    Private Sub Worksheet_Activate()
    If Len(Sheets("Data").Range("A1")) > 0 Then
        ActiveSheet.Shapes("Group 14").Height = 4.28*GetLogicalPixels(0)
        ActiveSheet.Shapes("Group 14").Width = 6.44*GetLogicalPixels(1)
    Else
        ActiveSheet.Shapes("Group 14").Height = 0
        ActiveSheet.Shapes("Group 14").Width = 0
    End If
    End Sub

  9. #9
    Forum Contributor
    Join Date
    04-26-2009
    Location
    Austin, TX
    MS-Off Ver
    Excel 2016
    Posts
    241

    Re: Show / hide chart based on value in cell assigned to chart (multiple charts)

    Kaper,

    Thanks for your help. I was curious if there might be a solution that doesn't involve specific dimensions, and I seem to have found one which I combined with your general structure:

        If Len(Sheets("Data").Range("A1")) > 0 Then
        ActiveSheet.Shapes("Group 14").Visible = True
      Else
        ActiveSheet.Shapes("Group 14").Visible = False
      End If
    Thank you for replies to my question. They really really helped!

    JayUSA

  10. #10
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: Show / hide chart based on value in cell assigned to chart (multiple charts)

    Glad to hear so.
    It convinces me that it's good time to mark thread SOLVED.

  11. #11
    Forum Contributor
    Join Date
    04-26-2009
    Location
    Austin, TX
    MS-Off Ver
    Excel 2016
    Posts
    241

    Re: Show / hide chart based on value in cell assigned to chart (multiple charts)

    Hmmm... there's no edit option for original post. So i don't know how to mark as solved. Any ideas?

    JayUAA

  12. #12
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: Show / hide chart based on value in cell assigned to chart (multiple charts)

    Please select Thread Tools from the menu link just above your first post and mark this thread as SOLVED. Thanks.

+ 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. Replies: 9
    Last Post: 12-26-2016, 12:33 PM
  2. VBA to Show/Hide multiple shapes based on cell values
    By Mike_Taylor16 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-22-2013, 03:29 PM
  3. Macro to hide rows based on cell value and update chart
    By umeshbanga in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-03-2012, 02:07 AM
  4. Charts - Line Chart, How to Show data Ranges?
    By chickencoop in forum Excel General
    Replies: 4
    Last Post: 01-06-2009, 08:43 PM
  5. Check box Chart - How to hide/show names
    By pauldaddyadams in forum Excel Charting & Pivots
    Replies: 9
    Last Post: 05-20-2008, 07:03 AM
  6. Hide/show series in chart with checkboxes?
    By Sibilia in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-05-2005, 06:49 AM
  7. [SOLVED] hide column but show chart
    By Svetlana in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 01-14-2005, 06:06 AM

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