Excel charts - setting series end dynamically - excel

I've got a spreadsheet with plenty of graphs in it and one sheet with loads of data feeding those graphs.
I've plotted the data on each graph using
=Sheet1!$C5:$C$3000
This basically just plots the values in C5 to C3000 on a graph.
Regularly though I just want to look at a subset of the data i.e. I might just want to look at the first 1000 rows for example. Currently to do this I have to modify the formula in each of my graphs which takes time.
Would you know a way to simplify this? Ideally if I could just have a cell on single sheet that it reads in the row number from and plots all the graphs from C5 to C 'row number' would be best.
Any help would be much appreciated.

OK, I had to do a little more research, here's how to make it work,
completely within the spreadsheet (without VBA):
Using A1 as the end of your desired range,
and the chart being on the same sheet as the data:
Name the first cell of the data (C5) as a named range, say TESTRANGE.
Created a named range MYDATA as the following formula:
=OFFSET(TESTRANGE, 0, 0, Sheet1!$A$1, 1)
Now, go to the SERIES tab of the chart SOURCE DATA dialog,
and change your VALUES statement to:
=Sheet1!MYDATA
Now everytime you change the A1 cell value, it'll change the chart.
Thanks to Robert Mearns for catching the flaws in my previous answer.

This can be achieved in two steps:
Create a dynamic named range
Add some VBA code to update the charts data source to the named range
Create a dynamic named Range
Enter the number of rows in your data range into a cell on your data sheet.
Create a named range on your data sheet (Insert - Name - Define) called MyRange that has a formula similar this:
=OFFSET(Sheet1!$A$1,0,0,Sheet1!$D$1,3)
Update the formula to match your layout
Sheet1!$A$1 set this to the top left hand side of your data range
Sheet1!$D$1 set this to the cell containing the number of rows
3 set this value to the number of columns
Test that the named range is working:
Select the dropdown menus Edit - Go To, type MyRange into the reference field.
Your data area for the chart should be selected.
Add some VBA code
Open the VBA IDE (Alt-F11)
Select Sheet1 in the VBAProject window and insert this code
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$D$1" Then Exit Sub
'Change $D$1 to the cell where you have entered the number of rows
'When the sheet changes, code checks to see if the cell $D$1 has changed
ThisWorkbook.Sheets("Sheet1").ChartObjects(1).Chart.SetSourceData _
Source:=ThisWorkbook.Sheets("Sheet1").Range("MyRange")
' ThisWorkbook.Sheets("Chart1").SetSourceData _
Source:=ThisWorkbook.Sheets("Sheet1").Range("MyRange")
'The first line of code assumes that chart is embedded into Sheet1
'The second line assumes that the chart is in its own chart sheet
'Uncomment and change as required
'Add more code here to update all the other charts
End Sub
Things to watch for
Do not directly use the named range as the data source for the chart. If you enter the named range "MyRange" as the Source Data - Data Range for the chart, Excel will automatically convert the named range into an actual range. Any future changes to your named range will therefore not update your chart.
Performance might be impacted by the approaches listed above.
The OFFSET function in the named range is "volatile" which means that it recalculates whenever any cell in the workbook calculates. If performance is an issue, replace it with the INDEX formula.
=Sheet1!$A$1:INDEX(Sheet1!$1:$65536,Sheet1!$D$1,2)
The code fires everytime data is changed on Sheet1. If performance is an issue, change the code to run only when requested (i.e. via a button or menu).

You could look at dynamic ranges. If you use the OFFSET function, you can specify a starting cell and the number of rows and columns to select. This site has some useful information about assigning a name to an OFFSET range.

You can set the range for a chart dynamically in Excel. You can use something like the following VBA code to do it:
Private Sub Worksheet_Change(ByVal Target as Range)
Select Case Target
Case Cells(14, 2)
Sheet1.ChartObjects(1).Chart.SetSourceData Range("$C5:$C$" & Cells(14,2))
...
End Select
End Sub
In this case, the cell containing the number of the last row to include is B14 (remember row first when referring to the Cells object). You could also use a variable instead of the Cells reference if you wanted to do this entirely in code. (This works in both 2007 and 2003.) You can assign this procedure to a button and click it to refresh your chart once you update the cell containing the last row.
However, this may not be precisely what you want to do ... I am not aware of a way to use a formula directly within a chart to specify source data.
Edit: And as PConroy points out in a comment, you could put this code in the Change event for that worksheet, so that neither a button nor a key combination is necessary to run the code. You can also add code so that it updates each chart only when the matching cell is edited.
I've updated the example above to reflect this.

+1s for the name solution.
Note that names don't really really reference ranges, they reference formulae. That's why you can set a name to something like "=OFFSET(...)" or "=COUNT(...)". You can create named constants, just make the name reference something like "=42".
Named formulae and array formulae are the two worksheet techniques that I find myself applying to not-quite-power-user worksheets over and over again.

An easy way to do this is to just hide the rows/columns you don't want included - when you go to the graph it automatically excludes the hidden rows/columns

Enhancing the answer of #Robert Mearns, here's how to use dynamic cells ranges for graphs using only the Excel's formulas (no VBA required):
Create a dynamic named Range
Say you have 3 columns like:
A5 | Time | Data1 | Data2 |
A6 | 00:00 | 123123 | 234234 |
...
A3000 | 16:54 | 678678 | 987987 |
Now, the range of your data may change according to the data you may have, like you have 20 rows of data, 3000 rows of data or even 25000 rows of data. You want to have a graph that will be updated automatically without the need to re-set the range of your data every time you update the data itself.
Here's how to do it simply:
Define another cell that it's value will have the number of the occupied cells with data, and put the formula =COUNTIF(A:A,"<>"&"") in it. For example, this will be in cell D1.
Go to "Formulas" tab -> "Define Name" to define a name range.
In the "New Name" window:
i. Give your data range a name, like DataRange for example.
ii. In the "Refers to" set the formula to: =OFFSET(Sheet1!$A$1, 0, 0,Sheet1!$D$1,3),
where:
Sheet1!$A$1 => Reference: is the Reference from which you want to base the offset.
0 => Rows: is the number of rows, up or down, that you want the upper-left cell of the results to refer to.
0 => Columns: is the number of columns, to the left or right, that you want the upper-left cell of the results to refer to.
Sheet1!$D$1 => Height: is the height, in number of rows, that you want the result to be.
3 => Width: is the width, in number of columns, that you want the result to be.
Add a Graph, and in the "Select Data Source" window, in the Chart data range, insert the formula as you created. For the example: =Sheet1!DataRange
The Cons: If you directly use the named range as the data source for the chart, Excel will automatically convert the named range into an actual range. Any future changes to your named range will therefore not update your chart.
For that you need to edit the chart and re-set the range to =Sheet1!DataRange every time. This may not be so usable, but it's better than editing the range manually...

Related

Index/Match returning wrong value

Summary tab:
Source tab:
I'm having some trouble using index match combo and getting the data to reference correctly.
what I am trying to achieve is in a tab called "Summary" I have a calendar with range D5:O6. the first cell in the calendar should change everyday to reflect the current date which will change the rest of the row making a rolling calendar. I have a data reference sheet called "PFS" from which the data needs to be referenced.
The thing I'm having trouble with is that with the formula
=INDEX(PFS, MATCH(A8,Snames,0),MATCH(D5,Sdates,0))
is returning the top left intersection (B8 of the source tab) of A8 and D5 values smith, bob & 4-Aug. a cell that i have entered the value "Wrong Corner".
Snames A8:A9, Sdates D5:O5 are the rows and columns containing the dates and names from the Summary tab
what I need help with is why its returning the wrong intersection and what I can do different to get the data I'm looking for.
The correct return would be the number one, which I have conditionally formatted to turn the cell blue and blank the text
You don't show what PFS references to. It seems to start in A1.
You can either use a range that starts in the correct cell, or add the required offset to the Match results.
=INDEX(PFS, MATCH(A8,Snames,0)+2,MATCH(D5,Sdates,0)+2)
Also make sure that Sname and Sdates refer to the ranges in the Source tab, not the Summary tab.

Don't show #N/A in Excel cell but also don't treat it as a 0 value in a chart

I have a column whose content is conditioned upon another column:
=IF(B8<>"";B8/($J$2 * $J$2);"")
This column is the line data for a chart. It works fine when the IF condition is met. However, any ELSE "" value is considered as 0 in the chart and the line crashes down to the bottom.
The alternative is to use
=IF(B8<>"";B8/($J$2 * $J$2);NA())
But then it looks ugly in the spreadsheet with loads of #N/A in the column.
Any solution aside from putting a conditional formatting on top which makes the font color white when the value is #N/A?
You can plot with a Named Range which will evaluate 0 to #NA
If the data you're plotting is in C3:C8:
Create a Name for that Range which evaluates 0 as #NA (I've called the Range "PLOT", but you can give it any name you want): =IF(Sheet1!$C$3:$C$8=0,NA(),Sheet1!$C$3:$C$8)
Use the Named range in the Chart: =SERIES(,,Book1!PLOT,1)
This will allow you to keep the ELSE-cells as "" in your sheet, whilst still showing up as #NA to the Chart and hence not collapsing the line to zero
NB: See here for notes on how to use Named Ranges in charts, particularly:
If your references are Names (Named Ranges), you need to qualify the Name with the scope of the Name, that is, either its parent worksheet or the parent workbook.
=SERIES(Sheet1!TheSeriesName,Sheet1!TheXValues,Sheet1!TheYValues,1)
You can enter the Name qualified by the worksheet, and if the Name is scoped to the workbook, Excel will fix it for you.

Excel code to link range cells' colour into another

I am building a residential stacking plan in which each cell = unit type with specific color given conditional formatting.
2 tables follow below for each unit reflect a sqm size and a $ value.
I need to reflect only the cells' color into the following tables.
I need a dynamic solution and would prefer avoiding vba (since I'm not proficient), but will use if necessary. Thanks in advance!
Find Image HERE
Set up your Conditional formatting as normal on the first table like this:
Note my table starts at cell C4 but yours is in a different place and should be adjusted accordingly. make sure you DON'T have the $ symbol on the formula in the rule but you do have it on the 'Applies to' section
Now copy and paste this formatting onto the second table.
Finally edit the formulas in the conditional formatting so that they point to the starting cell of the FIRST table. It should look like this:
Note that the formatting 'Applies to' the second table but refers in the Formula to the values in the first table.
The result is this:
You can repeat this for other tables if you need to.
As you are working in Excel 2003(!), follow the following steps:
Select the cells in the second table.
In the menu, choose Format - Conditional Formatting.
In the Conditional Formatting box, choose Formula Is.
In the text box, enter the cell reference of the FIRST table (eg C4="4+"), do not enter any $ symbols.
Click the Format button and select the background fill to match the one in the first table.
Add the other conditions in the same way by clicking the Add>> button.
If you change the color code values (B21:B26) from 2 rooms to 2 (to match your second table), the following should do the trick. Basically, this code is not using conditional formating. Getting the color from conditional formating can be somewhat laborious and tricky (google "excel vba find color conditional formatting"). Instead, the present code reads the color in your Color Code cells, and apply it to the other two ranges.
Private Sub BckgndColor()
Dim ColorCodeRange As Range
Dim NoOfRooms As Range
Dim CellColorIndex As Integer
Dim c As Range
Dim d As Object
Set ColorCodeRange = Worksheets("Sheet1").Range("B21:B26")
Set d = CreateObject("scripting.dictionary")
'Add the pairs (value, color) to dictionary
For Each c In ColorCodeRange.Cells
d.Add c.Value, c.Interior.ColorIndex
Next
Set NoOfRooms = Worksheets("Sheet1").Range("M25:V36") 'Here the range of Table 2 (M25:V36 in your example)
'Scan range, and assign color
For Each c In NoOfRooms.Cells
If d.Exists(c.Value) Then
c.Interior.ColorIndex = d(c.Value)
c.Offset(16, 0).Interior.ColorIndex = d(c.Value) 'If Table 3 is always 16 rows down, this shoud work
End If
Next
Set d = Nothing
End Sub
I gave the option data validation list in sheet 2, while I selecting the option by list the cells will change . for that cells I want to get color also from source table in sheet1 to sheet2 .

Excel Bar/Column graph showing blank cells

Starting with screenshot:
http://i.imgur.com/Isj9MER.png
(I'm a new user, can't post images)
Working for a call center. We have a program that tracks our time spent in various phone states (so when we're on calls, out to lunch, etc) that can export data for a given team and date range as a CSV.
I'm working on automating this report. The way it works is that the team lead will pull the CSV, copy-paste into another tab, and then I've got a bunch of array formula If functions and Indirect references to pull all the data as shown. The data analysis and everything is working great.
My problem is the graph. Right now, I've got column B with an If function that either outputs the agent's email (which is how the system tracks it) or "" if all emails have been used. The rest of the columns have If(B2="","", [relevant formula]). That way, we can have all the team leads with various (and fluctuating) team sizes use the same report with a simple copy-paste.
My problem is the stupid bar chart. It pulls data from rows 2-32 (A2:A32). Our current largest team is 28, and I left room for new hires showing up soon. My problem happens when I use data from one of our smaller teams. As you can see, even though the blank rows are filled with "" in every cell, it's still displaying those rows. In the chart. This means that with the smallest team (shown), the chart is half wasted whitespace.
Is there a way to make the column chart only show rows that have actual data in them?
One thing I tried was putting an Indirect reference for Series Values. So I had a cell (AA1) with {=MAX(IF(B2:B31="","",ROW(B2:B31)))}. That outputs the row number of the last non-blank row. Then for the Series Values I put =Indirect("Report!A2:A"&AA1), but Excel gave me an error saying the function was not valid. I guess you can only have an actual range (and not a formula) in the data input for a chart.
Excel 2016, by the way.
I come up with three possible solutions for this problem.
Convert your data table to a pivot table and use a pivot chart (currently only available in Windows version, sorry if you are using a Mac).
Use a bit of VBA to hide the empty rows.
Use a bit of VBA to modify the data displayed on the chart.
Sample Data Setup
Although you provided a screen shot of your data, it is not simple to convert that into a test case to demonstrate the three solutions. Therefore, I threw together this very simple set up.
Column A contains a list of possible "users".
Cell D1 is a user entry to change the contents of column B and C.
Column B contains the actual "users". It is calculated with =IF(A2<=$D$1,A2,"")
Column C contains the data that goes with the "users". It is calculated with =IF(B2<>"",10,"")
A chart is added to the sheet.
Below is a screenshot of the sample setup, where all potential users are included. (Note: It is Sheet1)
Below is a screenshot of the sample setup, where only potential users A through E are included.
The white space in the second image is the problem we are trying to address.
SOLUTION 1: Make a pivot table of the data
Select all of the pertinent data, in this case B1:C12.
Select Insert -> Pivot Table
In the Create Pivot Table dialog, make sure "New Worksheet" is selected, and click OK.
On the Pivot Table, place "User" field in Rows, and "Total" field in Values. Select the Value Field Settings... for the "Total" field and make sure it uses Sum.
In the Pivot Table, select the Row Labels drop down, Label Filters, Greater Than... . Type "" into the dialog and select OK.
From Pivot Table Tools -> Analyze, select PivotChart. Choose Bar Chart from the dialog.
Below is a screen shot of the Pivot Table.
On the tab with the data, change the last potential user from E to G. On the Pivot Table, refresh the data.
Below is a screen shot of the refreshed pivot table.
SOLUTION 2: Use VBA to hide empty rows
The below code is attached to a button made visible on the worksheet. (n.b. There are other ways to activate the code such as change events, but this will depend on a number of factors outside the scope of this answer).
Sub HideRows()
Dim AllCatRange As Range
Set AllCatRange = Worksheets("Sheet1").Range("B2:B12")
Dim iLoop As Long
For iLoop = 1 To AllCatRange.Rows.Count
If AllCatRange.Cells(iLoop, 1) = "" Then
AllCatRange.Cells(iLoop, 1).EntireRow.Hidden = True
Else
AllCatRange.Cells(iLoop, 1).EntireRow.Hidden = False
End If
Next iLoop
Set AllCatRange = Nothing
End Sub
Below is a screen shot of the data tab with the button added.
After clicking the button, it now looks like this ...
This code will expand rows when they contain data, and collapse rows when they do not.
A potential problem with this approach is that expanding/collapsing rows will change the size of the chart, if the chart lays over those rows.
SOLUTION 3: Use VBA to modify the chart
The below code is attached to a button, and used to modify the chart after the source data is changed.
Sub ModifyChart()
Dim AllCatRange As Range
Set AllCatRange = Worksheets("Sheet1").Range("B2:B12")
Dim lastRow As Long
lastRow = 1
Dim iLoop As Long
For iLoop = 1 To 11
If AllCatRange.Cells(iLoop, 1) <> "" Then
lastRow = lastRow + 1
End If
Next iLoop
Dim PlotCatRange As Range
Set PlotCatRange = Worksheets("Sheet1").Range("B2:B" & lastRow)
Dim PlotSerRange As Range
Set PlotSerRange = Worksheets("Sheet1").Range("C2:C" & lastRow)
Worksheets("Sheet1").ChartObjects(1).Chart.FullSeriesCollection(1).XValues = "=Sheet1!" & PlotCatRange.Address
Worksheets("Sheet1").ChartObjects(1).Chart.FullSeriesCollection(1).Values = "=Sheet1!" & PlotSerRange.Address
Set AllCatRange = Nothing
Set PlotCatRange = Nothing
Set PlotSerRange = Nothing
End Sub
Below is a screenshot of the data tab with the new button in place.
Below is a screenshot of the tab after clicking the button.
Wrap up
My personal preference is to use VBA to modify the chart after the data is modified, as this reflects what you would do manually.
I found a solution! Thanks largely to this page. Here's what I did:
I created made a named range using this formula: =OFFSET(Report!$B$1,1,0,COUNTIF(Report!$B$2:$B$30,"<>-"),1)
For that to work, I had to change all the empty cells to output "-" when empty in stead of "". I couldn't get COUNTIF to accept "<>""" or "<>" or any other weird tricks I tried. Using "-" was easier.
That makes a dynamic named range that changes size as I put data into the sheet. I named a similar range for everything I'm trying to chart (Approved Status, Call Ready, Not Ready). The chart was able to accept those names and now it's dynamically sized. If I only have three agents on the sheet, it shows three huge bars. With twenty, it shows twenty bars (exactly what I was looking for).
One other tip: I changed my first row to output "" when empty, so that COUNTIF(Report!$B$2:$B$30,"<>-") always returns at least 1 (otherwise you get annoying errors because you have a named range referencing a 0-length array).

Ignore #'¡DIV/0! Values in both axes of a bar chart - Excel

I have this bar chart in excel :
As you see, if there is #'¡DIV/0!, the values are 0. What I really want is that the bar chart just shows the numeric values. In this case. just the first three values would have to be visualized. I tried the formula IF.ERROR(FORMULA,"") but the result was the same,
So, How Can I do that?
You need dynamic ranges for the chart, or filter the data source to hide the rows with the error. Excel charts will not include data that is hidden with a filter.
To apply dynamic ranges, create two range names with the following formulas:
chtLabels =OFFSET(chtValues,0,-1)
chtValues =Sheet1!$B$12:INDEX(Sheet1!$B:$B,MATCH(99^99,Sheet1!$B:$B,1))
Adjust the chtValues range name to start in the first cell with a number in column B instead of B12 of the example.
Edit the column chart series values and enter the range name, preceded by the sheet name, like
=Sheet1!chtValues
Edit the Horizontal axis labels and replace the range with the dynamic named range
=Sheet1!chtLabels
When you exit the dialog, Excel will replace the sheet name with the file name. The result will be similar to this:
Note how the series formula shows the range names instead of the cell ranges.
=IFERROR(value, value_if_error) where value_if_error should equal 0 or "" or whatever you want

Resources