Solved

Excel:  VBA target = Combobox.value ?

Posted on 2011-02-11
2
734 Views
Last Modified: 2012-05-11
Experts!

I have a listbox that is chnages my charts.  I would like to add a target2 to show the chart title the complete value of the selection in the listbox.

Here is the code I am adding target2

Thanks

 
Sub UpdateSummaryChartArray(ByVal lbItemCount As Integer)
Dim myRng As Variant
Dim Target As String
Dim Target2 As String  'Chart Title Name
Dim i As Integer
Dim mySeries As Series
Dim myRange As Range

   
   Target2 = ComboBox1.Value




    
    Sheets("Summary Dashboard").ChartObjects("Chart 124").Activate
    With ActiveChart
        i = .SeriesCollection.Count - 1
        For Each mySeries In .SeriesCollection
            If i > 0 Then
                mySeries.Delete
                i = i - 1
            End If
        Next mySeries
    End With
    
    For i = 0 To lbItemCount - 1 'for each item in the listbox that was selected
        Target = lbArray(i) 'keep the code as similar as possible to original combobox routine
        On Error Resume Next
        Err.Clear
        myRng = Application.WorksheetFunction.VLookup(Target, Range("Summary_Delta_Range"), Range("Summary_Delta_Range").Columns.Count, False)
        On Error GoTo 0

        
        If Err.Number = 0 And Not IsEmpty(myRng) Then
            Sheets("Summary Dashboard").ChartObjects("Chart 124").Activate
            With ActiveChart
                If i = 0 Then 'set x-axis labels only once
                    Set myRange = Range(myRng)
                    .SeriesCollection(1).XValues = "'Executive Rollup Data'!" & Range(Cells(5, myRange.Cells(1, 1).Column), Cells(5, myRange.Cells(1, myRange.Columns.Count).Column)).Address
                End If
                    
                If i > 0 Then .SeriesCollection.NewSeries ' don't create the new series till past the first one
                
                .SeriesCollection(i + 1).Values = "'Executive Rollup Data'!" & Range(myRng).Address
                .SeriesCollection(i + 1).Name = Left(Target, InStr(Target, " ") - 1) 'name = initial word before a blank space of the combobox selection
                .HasTitle = True
                .ChartTitle.Text = Target2
                .Axes(xlValue).Select
                Selection.TickLabels.NumberFormat = Range("'Executive Rollup Data'!" & myRange.Cells(1, 1).Address).NumberFormat
            End With
        Else
            'do nothing
            Sheets("Summary Dashboard").ChartObjects("Chart 124").Activate
            With ActiveChart
                .HasTitle = True
                .ChartTitle.Text = "CAN'T FIND : " & Target
            End With
        End If
    Next i
End Sub

Open in new window

0
Comment
Question by:Maliki Hassani
2 Comments
 
LVL 41

Accepted Solution

by:
dlmille earned 500 total points
ID: 34875688
The below code will work, if you create a named range in Executive Rollup Data called ExecutiveIDKEY

it looksup the row of the short key on the name in Executive_List

Sub UpdateSummaryChartArray(ByVal lbItemCount As Integer)
Dim myRng As Variant
Dim Target As String, Target2 As String, Target2row As Integer
Dim i As Integer
Dim mySeries As Series
Dim myRange As Range

   
    
    Sheets("Summary Dashboard").ChartObjects("Chart 124").Activate
    With ActiveChart
        i = .SeriesCollection.Count - 1
        For Each mySeries In .SeriesCollection
            If i > 0 Then
                mySeries.Delete
                i = i - 1
            End If
        Next mySeries
    End With
    
    For i = 0 To lbItemCount - 1 'for each item in the listbox that was selected
        Target = lbArray(i) 'keep the code as similar as possible to original combobox routine
        On Error Resume Next
        Err.Clear
        myRng = Application.WorksheetFunction.VLookup(Target, Range("Summary_Delta_Range"), Range("Summary_Delta_Range").Columns.Count, False)
        On Error GoTo 0
        Target2row = Application.WorksheetFunction.Match(Target, Range("ExecutiveIDKey"), False)
        Target2 = Application.WorksheetFunction.Index(Range("Executive_List"), Target2row, 1)
                
        If Err.Number = 0 And Not IsEmpty(myRng) Then
            Sheets("Summary Dashboard").ChartObjects("Chart 124").Activate
            With ActiveChart
                If i = 0 Then 'set x-axis labels only once
                    Set myRange = Range(myRng)
                    .SeriesCollection(1).XValues = "'Executive Rollup Data'!" & Range(Cells(5, myRange.Cells(1, 1).Column), Cells(5, myRange.Cells(1, myRange.Columns.Count).Column)).Address
                End If
                    
                If i > 0 Then .SeriesCollection.NewSeries ' don't create the new series till past the first one
                
                .SeriesCollection(i + 1).Values = "'Executive Rollup Data'!" & Range(myRng).Address
                .SeriesCollection(i + 1).Name = Left(Target, InStr(Target, " ")) 'name = initial word before a blank space of the combobox selection
                .HasTitle = True
                .ChartTitle.Text = Target2
                .Axes(xlValue).Select
                Selection.TickLabels.NumberFormat = Range("'Executive Rollup Data'!" & myRange.Cells(1, 1).Address).NumberFormat
            End With
        Else
            'do nothing
            Sheets("Summary Dashboard").ChartObjects("Chart 124").Activate
            With ActiveChart
                .HasTitle = True
                .ChartTitle.Text = "CAN'T FIND : " & Target
            End With
        End If
    Next i
End Sub

Open in new window

See attached.

Dave
NOC-Reports-r12.xlsm
0
 

Author Comment

by:Maliki Hassani
ID: 34875722
Great I will implement this!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now