Chandoo: Did somebody just chart?
Jeff: Yes. Yes I did. More on that later. But first, let’s take a sniff of Mike Alexander’s outliers, shall we?
Over at the bacon bits blog, Mike has an interesting post on using something called the Tukey Method to identify outliers in a data set. That article is worth reading for John Walkenbach’s comment alone.
Here’s Mike’s sample dataset, with the data points identified as outliers highlighted in orange:
![]()
The Tukey method that Mike blogs about constructs a fence around “reasonable” readings, and that fence is described mathematically by an arbitrary numerical factor:
(Quartile 1) – (Arbitrary_Factor × IQR)
(Quartile 3) + (Arbitrary_Factor × IQR)
Typically a factor of 1.5 is used. Check out Mike’s blog for a detailed explanation of this stuff.
That’s all good, but it also produces a fairly arbitrary cut-off, depending on what factor you use. So rather than using an algorithm to determine outliers, my preference is to sort the data from lowest to highest value, then plot it and look at the resulting shape:

—Edit— Jon says in the comments:
Your line chart would be easier to read if you’d used markers. I use markers to indicate where the data actually IS, and help show that the line only ties the data together and doesn’t indicate more data, until the points are nearly touching.
Trust Jon to chart in my face. But he’s right. So here it is:

[Aside: That chart’s done in Excel 2013. What’s weird is that those markers aren’t centered on the line, but seem to sit just above it by a point or two. Whoops, Microsoft.]
And here it is with data labels, so it’s easier to see the actual values:

Some may say that the data labels are redundant, because you can gauge the values from the axis. My mature response to that is “Ffffffrrrrrt”. I like the data labels…once I’ve used the line to quickly judge what may be outliers, the labels let me confirm the jump in values without having to move my head back and forth like I’m watching Roger Federer play Andy Murry at Wimbledon.
In fact, maybe I can combine the marker with the labels, and get rid of that axis altogether:

Hey, that looks cool. Anyone going to get Tufte on me?
—Edit over—
This is akin to making a bunch of actors line up in order of shortest to tallest, and saying:
Okay…Elijah, Dominic, Billy, and Sean…you’re shortest. And by golly, you four look a lot shorter than the others. You guys can be the Hobbits.

[Aside: I recreated the below graph from one a site called SFScope. Check out the outliers at both ends, and click on the picture to visit the original]

I like this graphical approach. I think it takes less effort to visually identify outliers than to programatically identify them. For instance, let’s look at Mike’s sample data again for a moment:

Looking at this data, I visually identify pretty much the same outliers as Tukey would – points 1,2,3, 19, and 20. In addition, it looks like that 4th data point – with a value of 13 – looks like it has outlier stamped all over it too, when you see it in context of the other data.
Another benefit of plotting ranked data is that it also allows you to ask questions about interesting trends within the datapoints that clearly are not outliers. For instance, what’s the deal with the sudden ‘acceleration’ in the trend between datapoints 16 and 17 caused by? Understanding drastic changes within non-outlier points might be worth as much money to a business as understanding the outliers themselves.
Lose the horizontal axis?
Sometimes with larger datasets, that horizontal axis can be distracting, because Excel only has enough space along that axis to display the labels for every nth rank.
For instance, take the below graph, which looks at just how much money an organization receives from each of its customers by way of annual membership subscription each year:

See what I mean? You find yourself trying to decipher the trend in the data labels, and this really draws your eye away from the incredible trend shown in the graph above.
That’s much less distracting. Wow: many of our customers hardly subscribe to anything, and a few practically keep this place afloat!
What else can we show on a graph like this?
Sorting your data like this also lends itself to visually segmenting your customers by how much they contribute to your total revenue.
For instance, the below graph shows just how many customers it takes to account for each subsequent 25% of revenue, and what the average annual subscription within each group is. This gives you a real appreciation into just how valuable your larger customers are in comparison to smaller customers:

Wow, half our subscription revenue comes from our Key Accounts and Large Customers groups, who make up just 10% of our subscription base. Let’s be especially nice to those customers. And lots of our effort is spent in servicing small clients that don’t buy much. Can we grow their business? Should we sack some of them as customers, so we can spend that effort finding bigger ones?
Using revenue ‘buckets’ of 25% was a fairly arbitrary choice. What if we designed a chart template that let you dynamically choose different sized revenue buckets, as well as let you use more buckets if you wanted to?
For instance, looking at the above graph, it looks to me that we have a whole bunch of ‘Tiny Customers’. And we also might want to segment that group of Median customers that all have exactly the same sized subscription into a group of their own.
Well, the chart template I’ve put together for this post lets you do just that:

Wow. Jeff charted again. Man, look at all those time-wasting small accounts…they’re about as welcome as a chart in an elevator!
Note that the above graph was produced using Excel 2013. Excel 2013 automatically puts in those grey lines connecting the data lables with the series. Those are called Leader Lines. They rock.
Unfortunately, earlier versions of Excel only use leader lines for pie charts. But fear not, intrepid reader, for my chart template uses a bit of VBA to automatically puts lines in for you using shapes, if you’re using Excel 2010:

What’s cool about this template is that all the data labels are dynamic: change the ‘breakpoints’ between groups or the number of groups in the ‘Controls’ table [see screenshot below], and the details within the data labels are updated automatically. Bing!

I modified a version of Jon Peltier’s great Label Last Point routine to refresh the placement of the data labels. (Thanks, Jon). Here’s the template, so you can play around in the privacy of your own screen:
Segmenting customers by revenue contribution_V1 [Not tested in Excel 2007 or earlier]
Oh yes. I most definitely charted, boss.
Updates
—Update 1—
Prompted by some great action in the comments below, I whipped up this redesign in both gray and white:

While I like the grey, I do think it’s harder on the eyes than black text on white background. And I don’t think a grey chart would work well on say a dashboard. But that said, there’s no doubt in my mind that this chart is sexier than my original. Might look nice in the Economist. Here’s a link to the revised sample file: Segmenting-customers-by-revenue-contribution_with_Leader_Lines V1
—Update 2—
Kaiser Fung has some great ideas on how to redesign this in his post Visualizing Uneven Distributions. Go check it out, and be sure to subscribe to both his Junk Charts blog as well as his Big Data, Plainly Spoken blog. Both are gold. Both will make you a better analyst.
Added by Chandoo
If you like this chart, chances are you are going to love the below too:


















38 Responses to “Time to showoff your VBA skills – Help me fix ActiveSheet.Pictures.Insert snafu”
I tried your code with 2003, it works.
But, I know Addpicture does not take URLs anymore with 2007 onwards, perhaps its the same with picture.insert as well.
http://support.microsoft.com/kb/928983/en-us
The above link gives the solution as "picture fill in a shape such as a rectangle".
Tried to recreate this, but it worked fine for me. I just took the image of the error you showed in the post. Is there more info that can narrow this down a bit?
Don't know if this helps?
http://www.theserverside.com/discussions/thread.tss?thread_id=47101
Hi
Not sure if this is what you're after, but I just tried this
Sub Macro1()
ActiveSheet.Pictures.Insert("http://www.google.co.uk/intl/en_uk/images/logo.gif").Select
End Sub
Tied a button to it on the sheet and it seems to work; hope this helps a little
Ian
@All.. the issue is in Excel 2007. In 2003 ActiveSheet.Pictures.Insert seems to work fine. Unfortunately, I have design this in Excel 2007.. that is why I posted it here..
v2
Sub Macro1()
Set n = ActiveSheet.Pictures.Insert("http://www.google.co.uk/intl/en_uk/images/logo.gif")
With Range("c12")
t = .Top
l = .Left
End With
With n
.Top = t
.Left = l
End With
End Sub
Ian
That didn't come out very well. This positions at c12, so can change easily:
Sub Macro1()
Set n = ActiveSheet.Pictures.Insert("http://www.google.co.uk/intl/en_uk/images/logo.gif")
With Range("c12")
t = .Top
l = .Left
End With
With n
.Top = t
.Left = l
End With
End Sub
Works OK in 2007
Ian
The above codes work fines to my EXCEL 2007. Thanks.
Chandoo:
Try 'ActiveSheet.Pictures.Insert'
With ActiveSheet.Pictures.Insert("C:\Example.png")
.Left = ActiveSheet.Range("A1").Left
.Top = ActiveSheet.Range("A1").Top
End With
activesheet.pictures.insert "C:\Documents and Settings\Jon Peltier\Desktop\2007 stuff\insert_charts_2007.png"
Works for me in 2003 SP3 and in 2007 SP2.
Check the URL, and make sure you have internet connectivity.
What also works, and is newer (pictures.insert was supposedly deprecated in '97):
activesheet.shapes.addpicture "C:\Documents and Settings\Jon Peltier\Desktop\2007 stuff\insert_charts_2007.png", false, true, 200,200,100,100
Unfortunately you must specify dimensions (the last four arguments) and you don't necessarily know them. But the picture size is still related back to the original picture size, so you could use scaleheight and scalewidth to fix this.
Chandoo: I just re-read your post.
The code I posted works for me. However, I'm using a local picture. If you try to add a picture from the web, this won't work.
I remember solving this problem before by adding a rectangle shape first, then using the Shapes.AddPicture method to get a picture from the web.
I'll find that code and post it here.
Some more updates... The code "ActiveSheet.Pictures.Insert (path)" works fine in Excel 2007 at home. Strange it failed miserably on my work laptop. Do you think this has got something to do with SP2 of MS Office 2007 or something like that?
@Ian, Jon: Thanks for the code snippets. I guess I will use my home installation of excel to do this.
Chandoo:
Try this on your work laptop:
Sub test()
ActiveSheet.Shapes.AddShape msoShapeRectangle, 50, 50, 100, 200
ActiveSheet.Shapes(1).Fill.UserPicture _
"http://www.datapigtechnologies.com/images/dpwithPig6.png"
End Sub
FYI:
http://support.microsoft.com/kb/928983/en-us
I didn't mean to post code with a local file, because both approaches worked with an internet image as well. This is in Excel 2007 SP2.
activesheet.pictures.insert "http://peltiertech.com/images/2009-07/col_area_noblanks.png"
Jon: Looks like I have SP1 on my client machine! I wasn't paying attention.
Just checked my home computer where I have SP2, and you're right...looks like they fixed it.
I didn't even bother testing in SP1, though I could if anyone cares enough.
I'm afraid I don't have a solution, but I find it remarkable that after attaining a certain status in the Excel world, Chandoo does not need to post on an Excel discussion forum to get help for an Excel problem. Instead, he posts on his blog and all the gurus come rushing to his help.
Isn't Web 2.0 great?
Teylyn - I saw Chandoo's tweet first, and followed the link back to his blog.
@Mike.. thank you. I have seen the fill rectangle solution before posting the query here. For that matter, I have also tried the solution of embedding a browser control on a spreadsheet. both of these seemed a bit extreme. That is why I have asked it here.
But I guess I will end up using it if I had to build this in work laptop.
@Teylyn: I have thought of posting this in a forum. (Unfortunately I have not been to any excel group in the last 5 years. Last time I was active was when I built a jave based excel sheet construction solution using POI.HSSF classes of Apache... ) After searching for a few hours, I found several forum posts where others had same problem and the solution recommended (using .left and .top parameters) is not working for me. Incidentally most of these solutions are from a certain Jon Peltier 😛
I thought may be the problem is interesting for fellow blog readers. So I posted it here.
Hi,
Adapting the code in the question,
[code]
Sub InsPicture()
pPath = "http://chandoo.org/images/pointy-haired-dilbert-excel-charts-tips.png"
With ActiveSheet.Pictures.Insert(pPath)
.Left = Range("a1").Left
.Top = Range("a1").Top
End With
End Sub
[/code]
Seems to work fine
Looks like it was a problem in 2007 up to SP1, which was corrected in SP2.
@Jon.. seems like the case. I just checked the version at work laptop. it is 12.0.6331.5000 (SP1).
Thank you so much every one. I really appreciate your time and suggestions in solving this.
Glad to help. I couldn't understand why something so straightforward wasn't working.
Hi All
Is there a way of inserting a motion clip eg animated gif or swf or flv?
Thks
You can insert animated GIFs by inserting them in a browser control through VBA. For other types of movies, I can guess you can insert them as clip art.
I WANT THE INSERT PICTURE BY USING COADING
so currently i was struggling same as you, chandoo, with the insert picture method in excel 2007/10 from an url and came along your thread here.
so i re-designed the code on the addshape method as mike was suggesting it and all of the sudden it works just fine.
thanks alot to you guys, you were a great help
a big salut from switzerland
Hi guys,
I need help copying and pasting an image with the path in a cell.
I leave the code.
And thank you very much!
Sub Copiarimg()
Dim pic As Picture
With ActiveSheet
Set pic = .Pictures.Insert(Range("f2").Value)
With .Range("e9:g22")
pic.Top = .Top
pic.Left = .Left
pic.Width = .Width
pic.Height = .Height
End With
End Sub
I've played around with the approaches in these comments, and the code below is what I've come up with. The ImagePath can be a local file or a URL. As Jon mentioned above, the trick is to set an arbitrary value for the width and height, then call the ScaleWidth and ScaleHeight methods afterward to reset the picture to its original size. Once the LockAspectRatio property is set, you can change the picture width and the height will automatically scale (or vice-versa).
Sub AddPictureToRange(TopLeftCellAddress As String, ImagePath As String)
Dim pic As Shape
Dim l As Single, t As Single
Dim temp As Single
l = Me.Range(TopLeftCellAddress).Left
t = Me.Range(TopLeftCellAddress).Top
temp = 10# ' arbitrary value
Set pic = Me.Shapes.AddPicture(ImagePath, msoFalse, msoTrue, l, t, temp, temp)
pic.ScaleHeight 1#, msoTrue
pic.ScaleWidth 1#, msoTrue
pic.LockAspectRatio = msoTrue
End Sub
I need some help with inserting pictures. I have an excel file with a column of item numbers next to this row I want to insert a picture of this item. The pictures are coded with the item number so I tried to insert it with one of the codes above:
Sub InsPicture()
pPath = "http://img.bricklink.com/P/80/55236.gif"
With ActiveSheet.Pictures.Insert(pPath)
End With
End Sub
That worked but I need to do that for every row separtly.
So I tried in the code
pPath = "http://img.bricklink.com/P/80/"&Text(a1;"#")&".gif"
But that gives errors.
Anybody ideas?
Hi Nicholas, I used your solution in a related problem in Excel 2003 and it worked flawlessly..thank you!
Hi Mike Alexander,
Your solution with some changes was helpful in my problem in XL 2007, thanks.
Hi,
thanks all. In addition, I had a problem with multiple pictures inserting (every new picture replaced the prior one). I've changed it a bit, may be helpful..
Sub test()
ActiveSheet.Shapes.AddShape msoShapeRectangle, 50 , 50, 100, 200
ActiveSheet.Shapes(1).Fill.UserPicture _
"http://www.datapigtechnologies.com/images/dpwithPig6.png"
ActiveSheet.Shapes(1).Copy
ActiveSheet.Paste
End Sub
Try this instead:
Sub test()
ActiveSheet.Shapes.AddShape msoShapeRectangle, 50 , 50, 100, 200
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Fill.UserPicture _
"http://www.datapigtechnologies.com/images/dpwithPig6.png"
End Sub
Thanks to everyone, this thread has been very helpful. However, image inserting still doesn't work quite as expect for me.
While I can get a picture inserted into an Excel 2010 worksheet using either:
1) ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Fill.UserPicture...
2) ActiveSheet.Pictures.Insert(pPath), and
3) Shapes.AddPicture...
unfortunately the images all insert with a display size determined not by the actual pixel dimensions of the image but by the dpi resolution.
So for example, if I insert two copies of the exact same 600x600 pixel image, one with a 300dpi resolution and the other with 72dpi, they display at vastly different sizes on screen.
While this might be intended behaviour for Excel in order to maintain a WSYWIG printing layout, I actually need a way to insert the image based on the the actual pixel dimesnsions and ignoring the dpi resolution.
Any help appreciated.
Thanks
Kez
Not doing an intentional bump, but realised I posted in rely to one of the repsonses here instead of to the main thread, so reposting.
=====
Thanks to everyone, this thread has been very helpful. However, image inserting still doesn’t work quite as expected for me.
While I can get a picture inserted into an Excel 2010 worksheet using any of the below methods:
1) ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Fill.UserPicture....
2) ActiveSheet.Pictures.Insert(pPath), and
3) Shapes.AddPicture....
unfortunately the images all insert with a display size determined not by the actual pixel dimensions of the image but by the dpi resolution.
So for example, if I insert two copies of the exact same 600×600 pixel image, one with a 300dpi resolution and the other with 72dpi, they display at vastly different sizes in Excel on screen.
While this might be intended behaviour for Excel in order to maintain a WYSIWYG printing layout, I actually need a way to insert the images based on the the actual pixel dimesnsions and ignoring the dpi resolution.
Any help appreciated.
Thanks
Kez
Well, answered my own question 🙂
For those who might be interested, you can use this function:
Public Function GetPicDims(strFilePath As String, strFileName As String) As String
GetPicDims = CreateObject("Shell.Application").Namespace((strFilePath)). _
ParseName(strFileName).ExtendedProperty("Dimensions")
End Function
to get the dimensions of the image you want to insert. Then you can parse the return string and use the width and height values to add a rectangle shape of the appropraite size, like:
ActiveSheet.Shapes.AddShape msoShapeRectangle 50, 50, iWidth, iHeight
which you then fill with the picture:
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Fill.UserPicture "c:\temp\test.jpg"
This way the picture gets inserted using the pixel dimensions and the (print) resolution gets ignored.
If desired, the GetPicDims function can be made more generic to get other ExtendedProperties.
Regards
Kez