Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1
Default Display a chart only if there is data

Does anybody know how I can have a chart show on a spreadsheet only if there
is data? I have a number of charts on a single worksheet representing weeks
of data. If there was no data for a week, I'd rather not show the empty
chart. Is there a way?
--
Steve Duncan,

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,073
Default Display a chart only if there is data

Hi Duncan,

try out this macro on a copy of your sheet with embedded charts

Public Sub HideBlankCharts()
Dim Cht As ChartObject
Dim strSeries As String
For Each Cht In ActiveSheet.ChartObjects
On Error GoTo BLANK_CHART
strSeries = Cht.Chart.SeriesCollection(1).Formula
Cht.Visible = True
GoTo ALL_CHARTS
BLANK_CHART: Cht.Visible = False
ALL_CHARTS: Next Cht
End Sub

If you are not sure what to do with the code then follow these steps to
get it into place in the VB Editor...

1. Copy the 11 lines of code above

2. Go to your open Excel workbook and press Alt + F11. This takes you
to the VB Editor (You can also get there by going Tools|Macro|Visual
Basic Editor)

3. With the main menu at the top of the of the VB Editor go
Insert|Module.

4. Paste the code into the module (white space) that appears, then
Save.

5. Press Alt + F11 to get back to the usual Excel interface (You can
also get there by going File|Close and Return to Microsoft Excel)

Before you can run the macro, your workbook security level must be set
to Medium.
If it is not already set at Medium then...

1. Go Tools|Macro|Security... click on Medium
2. Save
3. Close
4. Open. Click on "Enable Macros" on the "Security Warning" dialog that
appears.


To run the macro you can go...Tools|Macro|Macros...look for
"HideBlankCharts" in the list of macros on the Macro dialog, click its
name in the list then click "Run" (or, you can just double click its
name). This is a tedious way of running a macro, so you might want to
use a keyboard shortcut. To do that go..Tools|Macro|Macros... select
the macro from the list on the Macro dialog, then click Options and
type in an appropriate key combination.

Another way of using the macro is to have it run automatically by Excel
everytime the user changes any values on the sheet with the data and
charts.

If you prefer that, then copy the following code (it's exactly the same
code inside a Worksheet_Change Event Procedure which has to be pasted
into the code module belonging to the sheet with the data and charts),
then in Excel (normal interface) right click the worksheet's sheet tab
then choose "View Code" from the popup menu. This takes you straight to
the code module that detects certain events that occur on the
worksheet. Paste the code, Save, then go Alt + F11 to return to Excel.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cht As ChartObject
Dim strSeries As String
For Each Cht In ActiveSheet.ChartObjects
On Error GoTo BLANK_CHART
strSeries = Cht.Chart.SeriesCollection(1).Formula
Cht.Visible = True
GoTo ALL_CHARTS
BLANK_CHART: Cht.Visible = False
ALL_CHARTS: Next Cht
End Sub

Ken Johnson

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,073
Default Display a chart only if there is data


Ken Johnson wrote:
Hi Duncan,

When he should have written Hi Steve:-/

Ken Johnson

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,073
Default Display a chart only if there is data


Hi Steve,

Sadly, further testing of my solution has revealed a flaw. An error
message results when more than one of the charts is lacking data.
Clicking "End" on the Error Message dialog gets rid of the dialog and
all charts with no data are invisible, however it is far from
satisfactory having to put up with that. I'm trying to resolve the
problem and will reply back here when I have succeeded.

Ken Johnson

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,073
Default Display a chart only if there is data

Hi Steve,

Good news!

Andy Pope has revised my code and fixed it up...

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cht As ChartObject
Dim strSeries As String
Dim blnSeeChart As Boolean

On Error GoTo BLANK_CHART
For Each Cht In ActiveSheet.ChartObjects
blnSeeChart = True
strSeries = Cht.Chart.SeriesCollection(1).Formula
Cht.Visible = blnSeeChart
Next Cht

Exit Sub

BLANK_CHART:
blnSeeChart = False
Resume Next

End Sub

Next time you have a question about charts try
microsoft.public.excel.charting where charting genius Andy Pope will
find your solution in almost an instant.

Ken Johnson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I want chart source data to be relative references, not absolute. Bob Mc Charts and Charting in Excel 1 April 7th 06 02:53 PM
Can't get data from a web page to chart in excel. Any solutions? goody444 Excel Discussion (Misc queries) 1 March 1st 06 06:09 AM
Lock Chart Display to last 14 days (or data points) toilinthedark Charts and Charting in Excel 1 September 21st 05 05:19 AM
allow user to disable annoying warning dialog on log charts Roger Charts and Charting in Excel 3 September 13th 05 01:26 PM
Extending a Chart Data Series from an Array - Can it be done? Jon Peltier Charts and Charting in Excel 4 November 30th 04 03:30 AM


All times are GMT +1. The time now is 03:14 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"