+ Reply to Thread
Results 1 to 2 of 2

How to activate the chart/object next to a button?

Hybrid View

  1. #1
    Registered User
    Join Date
    08-12-2007
    Location
    United Sates
    MS-Off Ver
    Office 365, PC
    Posts
    29

    How to activate the chart/object next to a button?

    I have a dashboard with multiple pivot charts. A macro controls chart formatting based on numerous criteria. The dataset is huge and frequently changes, so I want to update manually, as needed, on a per-chart basis rather than running on Worksheet_Calculate/_PivotTableUpdate/_PivotTableAfterValueChange/etc. (which would make everything intolerably slow).

    The current macro iterates over ActiveChart, so I have to manually activate each chart before clicking the button. My question is: How can I activate the chart above/below/adjacent to the clicked button? I realize that I can make a separate macro for each chart that first activates the chart and then calls the main macro, but I'd much rather just have one script that can figure out the chart of interest based on the button location so it dynamically adapts to new charts/moved charts/changing chart names.

    Sub UpdateChartAboveClickedButton ()
    
        Dim wb as Workbook
        Dim sht As Worksheet
        Dim btn_clicked As String
        Dim btn_location As Range
        Dim cht_to_update As ChartObject
    
    
        Set wb = myworkbook
        Set sht = wb.myworksheet
    
        btn_clicked = Application.Caller                                'find clicked button
        btn_location = sht.Shapes(btn_clicked).TopLeftCell.Address      'locate clicked button
    
    
    
        ---------------------------------------
        cht_to_update = ???                                             'find chart object directly above the clicked button
        ----------------------------------------
    
    
    
        With cht_to_update 
            With .Format
               [code...]
            End With
        End With
    
    End Sub
    (Also, if there's a better option than using the ApplicationCaller's .Address property, please say so. It was just the most straightforward thing that popped to mind.)
    Attached Images Attached Images

  2. #2
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,525

    Re: How to activate the chart/object next to a button?

    Sub UpdateChartAboveClickedButton()
    
        Dim wb          As Workbook
        Dim sht         As Worksheet
        Dim btn_clicked As Shape
        Dim btn_location As Range
        Dim cht_to_update As ChartObject
        Dim sngChartBottomEdge As Single
        Dim sngTopClickedBtn As Single
    
        Set wb = ThisWorkbook    'myworkbook
        Set sht = ActiveSheet    'wb.myworksheet
    
        Set btn_clicked = sht.Shapes(Application.Caller) 'find clicked button
        sngTopClickedBtn = btn_clicked.Top
    
        For Each cht_to_update In sht.ChartObjects
            sngChartBottomEdge = cht_to_update.Top + cht_to_update.Height
    
            'Find chart object directly above the clicked button.
            'The margin of inaccuracy of the button placement up to 20% of the button height.
            If sngTopClickedBtn <= sngChartBottomEdge + btn_clicked.Height * 0.2 And _
               sngTopClickedBtn >= sngChartBottomEdge - btn_clicked.Height * 0.2 Then
                Exit For
            End If
        Next cht_to_update
    
    
        If cht_to_update Is Nothing Then
            MsgBox "No chart found that meets the assumptions!", vbExclamation
            Exit Sub
        End If
    
    
        With cht_to_update
            MsgBox .Name & vbLf & .TopLeftCell.Address & ":" & .BottomRightCell.Address
        End With
    
    End Sub
    The top edge of the button does not have to be exactly on the bottom edge of the chart. In this code, you can have button location inaccuracy up to 20% of the button height.

    Artik

+ 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. How to activate Sound Object in Userform, Buttons/Macros
    By X myth in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-09-2019, 01:51 PM
  2. moving chart object and keep object ability for chart events?
    By tdrose01 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-12-2014, 06:43 AM
  3. [SOLVED] Activate chart on worksheet, without knowing chart name or index number
    By d.sanchez in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 10-18-2013, 12:51 PM
  4. Activate method of chart object failed
    By nigelog in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-30-2008, 06:37 AM
  5. Cannot Activate Chart Area in Chart. Chart Object Failed
    By ahperez@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-07-2006, 09:45 PM
  6. Activate web link via image object
    By MIKESX in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-01-2006, 03:25 AM
  7. [SOLVED] Option button object proeprties or object not found in vba
    By Pete Straman S via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-31-2005, 01:05 PM

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