Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Sub Update_Chart()
'This is the main routine - from which all other
'(sub)routines are called. To activate this code,
'you could add a button to your worksheet,
'right-click on the button, choose "Assign Macro"
'and then choose this main routine (Update_Chart).
'Naturally, don't expect it to work until AFTER
'you've performed all the steps covered below...
Application.ScreenUpdating = False
Update_DataRange 'see routine below
Extract_Data 'see routine below
Set_Extdata 'see routine below
Set_Chart 'see routine below
'You'll need to change the filename below
'(currently Chart Example.xls) to the same
'name as the name of your file.
Windows("Chart Example.xls").Activate
ActiveCell.Select
Application.ScreenUpdating = True
End Sub
Sub Update_DataRange()
'Updates the range name "data" - for the
'number of records in the sheet containing
'your data (from which chart data is derived).
'You'll need to change "Database" below to the
'name of the sheet containing the data.
Worksheets("Database").Select
[data].Select
ActiveCell.Select
topcell = ActiveCell.Address
'You need to change the row-offset below
'(currently 10), for the number of columns in
'your database.
botmcell = [A65536].End(xlUp).Offset(0, 10).Address
'Column A above is a column in my data that
'will ALWAYS be populated. You'll possibly
'need to change to another column - if your data
'won't always contain data in Column A.
'If were to change the Column from A to C for
'example, then you would also need to change
'the offset (currently 10) to 8. (10 less 2 = 8
'because the starting point (Column C) is
'2 columns to the right).
rng = topcell & " : " & botmcell
'Based on 'topcell' & 'botmcell' above, the
'following row resets the range name "data".
Range(rng).Name = "data"
End Sub
Sub Extract_Data()
'This is for extracting "selective" data
' (i.e. without the #N/A rows) to a separate sheet.
'Note: A "copy" of the data is extracted –
'leaving the original data "as is". You'll need to
'insert a sheet for this purpose, and give the sheet
'a name. I'm using the name "ChartData" (the next
'subroutine (Set_Extdata) references "ChartData").
'The code below references 3 range names:
'1) "data" - is the range name already mentioned –
'in the Update_DataRange routine (above).
'2) "crit" - is a range name that you'll need to assign
'to 2 cells on a worksheet. Preferably, you should
'create a separate sheet for criteria used with Excel's
'Advanced Filter - because this will avoid problems
'that can arise, especially in earlier versions of Excel.
'I always name this separate sheet "Criteria". On this
'sheet, I usually also include notes relating to the
'criteria and misc. other aspects of the file that relate
'to automation, etc.
'On the Criteria sheet, I normally enter the criteria
'starting in Column B, because I reserve Column A
'for entering (as a label) the name of the range name
'I assign to each criteria I create.
'For now, you only require the one criteria, named
'"crit". Therefore, I would enter the label "crit" in
'Column A (any row near the top is fine - doesn't
'have to be row 1). To the right of the cell you enter
'the label "crit", assign the range name "crit". As
'previously mentioned, this name has to be assigned
'to 2 cells.
'Use these Steps:
'1) Highlight the cell opposite the label "crit", and the
'cell immediately below - in Column B. (e.g. if you
'entered the label "crit" in A5, then highlight B5:B6)
'2) Hold down <Ctrl> and hit <F3> (This opens the
'"Define Name" window).
'3) Type the name - e.g.: crit ...and <Enter>.
'The following formula needs to be entered in the
'2nd (lower) cell of the range named "crit" - B6 in
'the above example.
'=XXX<>"#N/A" ...where you need to replace the
'"XXX" with the field name that identifies the column
'where you'll have "#N/A" for some of your records.
'IMPORTANT: Leave the top cell (B5) empty.
'This is REQUIRED when a formula is used in
'creating criteria.
'NOTE: The above example is the simplest of
'examples (2 cells). Whenever required, it's possible
'to use any number of cells - other columns to reference
'additional fields, and/or other rows to include
'additional criteria. Basically, including additional
'fields makes it a "AND" condition, and including more
'rows makes it an "OR" condition for the criteria.
'Back to the range names below - now the 3rd name
'- "out". You need to assign this name on the
'"ChartData" sheet (mentioned previously), to the cells
'containing the field names of those fields containing
'the data you wish to extract for the chart.
Range("data").AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:="crit", _
CopyToRange:=Range("out"), _
Unique:=False
End Sub
Sub Set_Extdata()
'This routine assigns the range name "extdata" to the
'data extracted to the "ChartData" sheet.
Worksheets("ChartData").Select
[out].Select
ActiveCell.Offset(1, 0).Activate
topcell = ActiveCell.Address
'The row below references Column "A", with column
'offset of "1", to reflect two fields of data being
'extracted. Naturally if you are including a different
'number of fields, you'll need to change this row
'accordingly.
botmcell = [A65536].End(xlUp).Offset(0, 1).Address
rng = topcell & " : " & botmcell
Range(rng).Name = "extdata"
End Sub
Sub Set_Chart()
'This routine simply updates the existing chart with
'the data extracted - i.e. the data named "extdata".
'Be sure, however to change the Chart # below
' see ChartObjects(“Chart 1”) to match
'with the Chart # of your existing chart.
Worksheets("Chart").Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SetSourceData [extdata]
End Sub