Peltier Tech Blog

Excel Chart Add-Ins | Training | Custom Solutions | Charts and Tutorials | PTS Blog

 

Main menu:

 
Peltier Tech
Chart Add-Ins

Peltier Tech Waterfall Chart Utility
Peltier Tech Cluster-Stack Chart Utility
Peltier Tech Box and Whisker Chart Utility
Peltier Tech Marimekko Chart Utility
Peltier Tech Dot Plot Utility
Peltier Tech Cascade Chart Utility

 
Excel Dashboards

Subscribe

Site search

Subscribe

Site search


Recent Posts

Recently Commented

Popular Posts

Archive


 

Categories


 

Privacy Policy

Creative Commons License
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.

Make Your Recorded Macro Independent of Which Sheet is Active

by Jon Peltier
Monday, March 16th, 2009
Peltier Technical Services, Inc., Copyright © 2010.
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.

When you run a recorded macro, it may work great, except it keeps referring back to the original worksheet. It also refers back to the original data range. Both of these situations can be remedied by editing of the recorded macro.

In How To: Fix a Recorded Macro I showed how to make a recorded macro more efficient by omitting and combining statements and eliminating the selection of objects during macro execution. This is a more specific example that makes the macro independent of the conditions (active sheet and selection) when it was recorded.

In this example, a macro is recorded after data is selected to capture the steps required to insert a chart object in the active worksheet. This recording was made in Excel 2003; the steps in 2007 are similar, and I’ll cover them in another post.

Here is our original data in C7:F13 of Sheet1:

Data for recorded macro /></p> <p>I recorded this macro while I created a chart, without changing any other formatting.</p> <pre>Sub Macro1() ' ' Macro4 Macro ' Macro recorded 3/16/2009 by Jon Peltier '  '     Charts.Add     ActiveChart.ChartType = xlXYScatterLinesNoMarkers     ActiveChart.SetSourceData Source:=Sheets(

The Recorded Macro

I recorded this simple macro while creating the chart, without changing any formatting:

Sub Macro1()
'
' Macro4 Macro
' Macro recorded 3/16/2009 by Jon Peltier
'

'
    Charts.Add
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("C7:F13")
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
End Sub
 

The worksheet name and selected range appear hard-coded in this macro. No matter what sheet is active or what range is selected, the chart will always appear on Sheet1 using the data in C7:F13 of Sheet1.

I will describe a couple ways to change this, both involving variables which are declared and assigned before the chart is created.

Fixing the Macro with String Variables

The sheet name and range address are present in the recorded macros as simple text. We can define string variables to contain the sheet name and the range address, as shown in this fixed macro.

Sub Macro1a()
'
' Macro4 Macro
' Macro recorded 3/16/2009 by Jon Peltier
' Fixed to use text variables for sheet and range

'
    Dim sSheetName As String
    Dim sDataRange As String

    sSheetName = ActiveSheet.Name
    sDataRange = Selection.Address

    Charts.Add
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SetSourceData Source:=Sheets(sSheetName).Range(sDataRange)
    ActiveChart.Location Where:=xlLocationAsObject, Name:=sSheetName
End Sub
 

Fixing the Macro with Object Variables

We can also use object variables, that is, a worksheet object for the active sheet and a range object for the data, as in the following fixed up macro.

Sub Macro1b()
'
' Macro4 Macro
' Macro recorded 3/16/2009 by Jon Peltier
' Fixed to use object variables for sheet and range

'
    Dim wksData As Worksheet
    Dim rngData As Range

    Set wksData = ActiveSheet
    Set rngData = Selection

    Charts.Add
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SetSourceData Source:=rngData
    ActiveChart.Location Where:=xlLocationAsObject, Name:=wksData.Name
End Sub
 

By adding only a few lines of code, the recorded macro has been made much more flexible. The choice whether to use string or object variables is not important in this case. In more complex procedures, one or the other approach may be advantageous, if a particular variable type is more useful later in the procedure.

Related Posts:

Bookmark and share this entry:
  • Twitter
  • Digg
  • Facebook
  • LinkedIn
  • del.icio.us
  • Technorati
  • StumbleUpon
  • Google Bookmarks
  • Reddit
  • MySpace
  • Slashdot
  • Yahoo! Buzz
  • Tumblr

Learn how to create Excel dashboards.

Comments


Comment from LEM
Time: Friday, October 16, 2009, 4:28 pm

Jon,

I have come to you before when I have hit dead ends, and I am hoping you can help me out with something I am running into with 2007. I wasn’t sure where to post this, but am in need of help.

In PowerPoint 2007 I have the option to paste my chart in, but with the entire workbook behind it (so that the user can actually go in and update everything themselves). My problem is, I get a PERSONAL.XLSB locked for editing error (when it is a chart). I have no idea how to get around this, and have been looking for answers all over… I saw mention of potentially creating my own Add-In?

Also, when I copy and paste my chart into PowerPoint, I will lose any additional text boxes containing formulas that I may have added. Any ideas (oh, and I don’t want it to be a picture…)?

I am missing 2003 about now, but hope you will be able to help ease the transition with tips for these things!

Thank you!!


Comment from Jon Peltier
Time: Friday, October 16, 2009, 6:01 pm

LEM -

I haven’t used Excel and PowerPoint 2007 together much, so I only know of the problems, not the workarounds. The deal with the personal.xlsb is that, if you change it to an add-in, it can be open multiple times without conflict. Which means it’s open in the instance in PowerPoint. That might be good or bad. So might having the workbook travel with the chart. Good to allow adjustments, bad to keep proprietary information secret.


Comment from Sarah
Time: Thursday, May 6, 2010, 5:05 pm

Ok i ran the below macro and it works great makes a new chart on the page before and uses the active sheet for referance.. the problem is, it only uses the active sheet for referance for 2 of my 4 data points not all… any suggestions?
Thanks a bunch, your website has saved me a dozen times!!!

Sub Chart()

‘ Chart Macro

‘ Keyboard Shortcut: Ctrl+k

Range(“C13:C64″).Select
ActiveWindow.SmallScroll Down:=-24
Range(“C13:C64,N13:N64″).Select
Range(“N13″).Activate
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 11
ActiveWindow.ScrollColumn = 15
ActiveWindow.SmallScroll Down:=-21
Range(“C13:C64,N13:N64,V13:V64″).Select
Range(“V13″).Activate
ActiveWindow.SmallScroll Down:=-18
Range(“C13:C64,N13:N64,V13:V64,AG13:AG64″).Select
Range(“AG13″).Activate
Charts.Add

ActiveChart.ChartType = xlColumnClustered
ActiveChart.PlotArea.Select
ActiveChart.SeriesCollection(1).XValues = “=”
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(4).Name = “=”"Current Case Volume”"”
ActiveChart.SeriesCollection(4).Values = “=’Chef Canned Pasta’!$C$13:$C$64″
ActiveChart.SeriesCollection(3).Name = “=”"Case OH YAG”"”
ActiveChart.SeriesCollection(2).Name = “=”"YAG Case Volume”"”
ActiveChart.SeriesCollection(1).Name = “=”"Case OH Current”"”
ActiveChart.PlotArea.Select
ActiveChart.ChartType = xlLine
ActiveChart.SeriesCollection(1).XValues = “=’Chef Canned Pasta’!$B$13:$B$64″
End Sub

Write a comment

I welcome comments from my readers. If you have an opinion on this post, if you have a question or if there is anything to add, I want to hear from you. Whether you agree or disagree, please join the discussion.

If you want to include an image in your comment, post it on your own site or on one of the many free image sharing sites, and include a link in your comment. I'll download your image and insert the necessary html to display the image inline.

Read the PTS Blog Comment Policy.





Subscribe without commenting

Peltier Tech Waterfall Chart Utility Peltier Tech Cluster-Stack Chart Utility Peltier Tech Box and Whisker Chart Utility Peltier Tech Marimekko Chart Utility Peltier Tech Dot Plot Utility Peltier Tech Cascade Chart Utility

Create Excel dashboards quickly with Plug-N-Play reports.