Label specific datapoints "n/a"

cubsfan05

New Member
Joined
Jun 10, 2013
Messages
30
I have the data and the chart shown below and I'm wondering how I can display only certain data labels?
Basically, the first three companies are all n/a for sales (ignore the EBITDA columns) and so i want to label those as n/a. I tried creating a 2nd data set in column D and charting that, but it labeled all data points (D5:D12) as "0" instead of labeling datapoints D5:D7 as "n/a" and leaving the other data points without a label.

Appreciate any ideas / help you guys can offer.
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,012
I would suggest replacing "n/a" values with "#N/A" error values. As such, theses data points will be evaluated as "Empty" instead of "0". Then we can use the following macro...

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]

[COLOR=darkblue]Sub[/COLOR] Insert_NA_Labels()

    [COLOR=darkblue]Dim[/COLOR] MyVals [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    [COLOR=darkblue]With[/COLOR] Worksheets("Sheet1").ChartObjects("Chart 4").Chart [COLOR=green]'change the sheet and chart names accordingly[/COLOR]
        MyVals = .SeriesCollection(1).Values
        [COLOR=darkblue]For[/COLOR] i = [COLOR=darkblue]LBound[/COLOR](MyVals) [COLOR=darkblue]To[/COLOR] [COLOR=darkblue]UBound[/COLOR](MyVals)
            [COLOR=darkblue]If[/COLOR] IsEmpty(MyVals(i)) [COLOR=darkblue]Then[/COLOR]
                [COLOR=darkblue]With[/COLOR] .SeriesCollection(1).Points(i)
                    .HasDataLabel = [COLOR=darkblue]True[/COLOR]
                    .DataLabel.Text = "N/A"
                [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        [COLOR=darkblue]Next[/COLOR] i
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
Hope this helps!
 

cubsfan05

New Member
Joined
Jun 10, 2013
Messages
30
That's perfect. I'm adding a bunch of other error checking and customized your code slightly for a couple extra loops, but thanks man. Massive help!
 

cubsfan05

New Member
Joined
Jun 10, 2013
Messages
30
In case you're curious how I built out the rest code
Code:
Option Explicit

Sub ResetCharts()
    Call ResetProcedure("RevenueChart")
    Call ResetProcedure("EbitdaChart")
End Sub


Sub ResetProcedure(ChartName As String)


    Dim MyVals As Variant
    Dim i As Long
    Dim SeriesNum As Integer
    Dim AdjTotal As Integer
    Dim TransactionRow As Integer
    
    AdjTotal = WorksheetFunction.CountA(Range("B:B")) + 10
    'unhide all rows
    Sheets("Transaction Graphs").Rows("1:" & AdjTotal).EntireRow.Hidden = False
    
    For TransactionRow = 1 To AdjTotal
        If Range("B" & TransactionRow).Value = "(Invalid Identifier)" Then
            Sheets("Transaction Graphs").Range("B" & TransactionRow).EntireRow.Hidden = True
        End If
    Next TransactionRow
    
    'Clear chart labels
    ActiveSheet.ChartObjects(ChartName).Activate
    ActiveChart.SetElement (msoElementDataLabelNone)
    
    For SeriesNum = 1 To 2  'for Lincoln comps & Public transactions
        With Worksheets("Transaction Graphs").ChartObjects(ChartName).Chart
            MyVals = .SeriesCollection(SeriesNum).Values 'set loop for all values
            For i = LBound(MyVals) To UBound(MyVals) 'loop through all datapoints
                If IsEmpty(MyVals(i)) Then
                    With .SeriesCollection(SeriesNum).Points(i) 'if empty datapoint, provide "n/a" label
                        .HasDataLabel = True
                        .DataLabel.Text = "N/A"
                    End With
                End If
            Next i
        End With
    Next SeriesNum
    
    ActiveSheet.Range("A1").Select
    
End Sub
 

Forum statistics

Threads
1,082,246
Messages
5,363,991
Members
400,772
Latest member
solbebe

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top