How do I create a chart title dependant on an Auto Filter

J

Jayne

Hi,
I have created a chart where the info changes depending on
the autofilter results. Is there a way of having the chart
title change to reflect what is in Column A of the
filtered data. I cannot use a cell reference as the cell
reference changes depending on the result of the filter.
Hope I am making sense :)
Any help you can give would be greatly appreciated
 
D

Don Guillett

This could probably be better but it works where your title row is a2
Sub colaval()
mt = Range("a3:a100").SpecialCells(xlCellTypeVisible).Range("a1")
ActiveSheet.ChartObjects("Chart 3").Activate
ActiveChart.ChartTitle.Characters.Text = mt
ActiveChart.Deselect
End Sub
 
D

Don Guillett

this is better. 2 lines (correct word wrap)

Sub colaval()
mt = Range("a3:a100").SpecialCells(xlCellTypeVisible).Range("a1")
ActiveSheet.ChartObjects(1).Chart.ChartTitle.Text = mt
End Sub
 
D

Debra Dalgleish

To show the value that has been selected in the AutoFilter dropdown, you
can create a User Defined Function. Tom Ogilvy posted the following
function, that returns the criteria from a column in an autofiltered
table. It will show both criteria if there are two, and includes the
operator.

Paste the code (it's at the end of this message) onto a regular module
in your workbook. There are instructions here:

http://www.contextures.com/xlvba01.html

Use the function in a cell on your worksheet. To make it respond to a
filter change, tie it to the subtotal command. For example,
=showfilter(B2)&CHAR(SUBTOTAL(9,B3)*0+32)
would show the criteria for column B

Then, refer to this cell from your chart --
Select the chart
Click in the Formula bar, and type an equal sign
Click on the cell that contains the ShowFilter function, and it will be
referenced in the formula, e.g.:
='ChartData'!$H$2
Press the Enter key to complete the formula.
Point to the border of the text box that was created, and drag it to the
top of the chart. Format it with the buttons on the formatting toolbar.


'============================================
Public Function ShowFilter(rng As Range)
'UDF that displays the filter criteria.
'posted by Tom Ogilvy 1/17/02
'To make it respond to a filter change, tie it to the subtotal command.
'=showfilter(B2)&CHAR(SUBTOTAL(9,B3)*0+32)
'So the above would show the criteria for column B

Dim filt As Filter
Dim sCrit1 As String
Dim sCrit2 As String
Dim sop As String
Dim lngOp As Long
Dim lngOff As Long
Dim frng As Range
Dim sh As Worksheet
Set sh = rng.Parent
If sh.FilterMode = False Then
ShowFilter = "No Active Filter"
Exit Function
End If
Set frng = sh.AutoFilter.Range

If Intersect(rng.EntireColumn, frng) Is Nothing Then
ShowFilter = CVErr(xlErrRef)
Else
lngOff = rng.Column - frng.Columns(1).Column + 1
If Not sh.AutoFilter.Filters(lngOff).On Then
ShowFilter = "No Conditions"
Else
Set filt = sh.AutoFilter.Filters(lngOff)
On Error Resume Next
sCrit1 = filt.Criteria1
sCrit2 = filt.Criteria2
lngOp = filt.Operator
If lngOp = xlAnd Then
sop = " And "
ElseIf lngOp = xlOr Then
sop = " or "
Else
sop = ""
End If
ShowFilter = sCrit1 & sop & sCrit2
End If
End If
End Function
'==============================================
 
G

Guest

many thanks for your responses - they nearly worked but
would not update automatically when I changed the auto
filter.

I spoke to one of our engineers who came up with an
alternative solution which I thought you may be interested
in. It is a roundabout way but it works !!

He inserted a new column A and inserted line numbers
against each row. Then in Column A2 inserted the following
=SUBTOTAL(4,A4:A2269) to get the maximum line number in
the autofiltered section.
He then used the following formula in Column C2
=VLOOKUP(A2,$A$4:$C$2269,3)
to show the result of that maximum cell in column 3 which
is the column that has the name of the project that I
wanted to display on my chart.
then just a matter of using the =C2 formula on the chart
to display the result.

May be a bit crude but it is updating automatically and is
doing the job.

Again thanks for your responses - I have no doubt it will
help me in the future.

thanks
Jayne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top