Hey guys, welcome back. So in this video we're going to discuss the workbook new chart event. So the workbook new chart event, whatever code you write in there gets triggered whenever you insert a new chart on your workbook. Okay, so you can get it by going to the VBA editor and double clicking on this workbook and then selecting workbook from this menu and selecting new chart from this menu here. Now, whatever code you enter in here, is going to be triggered by inserting a new chart messagebox for example, you have inserted a new chart, okay, and now if I try to insert a new chart, let's say I've got some sales figures here. Just going to insert a new chart here are the Recommended Charts.
This clustered column looks fine. Okay, so I've inserted a chart and as you can see here, the message box popped. I clicked ok. Then the chart appears. Okay, so it gets triggered whenever you insert a new chart, and actually it works before the chart, it's inserted on your workbook. Okay, so maybe you want to do something more complicated with a new chart than just display a message box. So maybe you want to change the charts title, for example, to have a certain title whenever you insert a new chart, so you can do that with the workbook new chart event.
So in this workbook new chart event, actually there is a parameter here called ch which is basically an object variable that it's type is a chart, okay, so it has all the methods and properties for a chart object in Excel. So we could write CH, okay, dot and then we will have all the methods and properties for a chart and then we're just going to change our chart title. So we're going to write ch dot chart title and then this produces a chart title object, and it has a property called text. Okay, so dot text equals my chart, for example. Okay, so that whenever I insert a new chart, I would have it named as my chart, it would have its title as my, my chart. So let's try that.
So I'm going to delete here my previous chart, though, I'm going to insert a new chart, I'm going to highlight my data go to insert. I'm going to go to Recommended Charts here and select this chart here again, click OK. As you can see here, the title is now my chart. So whenever you insert a new chart, its title is going to be my chart. You could also use the workbook new chart object to maybe have some default colors for your chart. So maybe have a set of default colors for your chart so that whenever you insert a chart, it will be inserted with certain colors, maybe the colors of your brand, for example, but actually there's another way the iPad To use rather than VBA code, but I'll show you how to do it with VBA code. And then I'll show you the other way.
So basically, to do that with VBA code actually is very hard to memorize the methods and properties for charts and stuff, because there are lots of details in them. So actually, as one of the methods to learn about a certain object is to actually record a macro. And we've discussed that at the beginning of the course. And we've said that as one of the methods to learn VBA is to actually record macros and examine the code. So this is when this comes handy. So I can actually select this chart here and let's say I want to change its colors.
Okay, so maybe here on this style, I'll go to colors, okay, on this chart styles here, I'll go to color and maybe give it some colors. You know, just to check, maybe I want this template here, color three. So I want every chart to have this set of colors. Okay, so what I'm going to do here, I'm gonna put Everything back to what it was. I'm gonna do that using the macro recorder. Okay, so I'm gonna record a macro, I'm going to go to the developer tab on the ribbon here, click on record macro.
And maybe I'll call it chart macro something, okay, I'll call it chart macro, click OK. And then I select my chart and I'll start editing its color. Okay, so I'll go to that color here. So I've changed the color of the chart. And now I'll stop recording. Okay, and now let's examine the code that I've recorded. Okay, so I'll open the macro here, I'll go to a macros here and click on Edit to check the code that I've recorded.
As you can see here on the active sheets, I'm selecting a certain charge, I'm activating it, I don't need that. I actually need this one here, the chart color property to be set to be equal to 12. So this is the part I actually need. So just copy that and I'll go to my event. Then I'll write here ch dot chart color equals 12. Actually, I just duplicated that.
Okay, so ch chart color equals 12. And now I'll just delete my chart here and try inserting a new chart. And let's see what happens. So I'll just highlight my data again, go to Recommended Charts, select this chart here, click OK. As you can see, here, the chart has been inserted with the color that we specified on the event and that we have obtained through recording. Okay, so this is when recording is useful, really, because there are some times there's some objects that you don't know everything about. And it's actually easier to record than even to research Google.
Or maybe you can record and get the name of the certain method or property for that object that you're searching for or that you want to modify or play with. And then you can take it on Google. You can search it on Google, under on everything about it, and then, you know, go back to excel and start playing with it. Okay, so this is when recording is useful. Okay, so I mentioned before that there's another way actually, to do this, that is easier or in my opinion, it's easier. Okay, so I'm just going to delete my charts here.
And I'm going to go to the VBA code, I'm going to delete the code in the event so that you wouldn't be executed. And actually, there's a non VBA method, which I believe is easier. Okay, so I'm going to highlight my data again here and insert the new chart. And then what I'm going to do, okay, so I've inserted my chart, maybe I don't like this color, I want another color. So choose that color. I'll choose that one here.
And then I can save this chart as a template. Okay, so I'm going to right click on it and click on Save as template and save the template. I'll call it chart one. For example. This is a previous template the high save, I don't need it, I'll just replace it. So I clicked yes to replace it.
So now I have a template that I can use. So to create a new chart using The template that you have created, I'll just show you how to do it. So I'll delete this chart here. And I'll just highlight my data again go to Insert, go to recommended chart here and then go to all charts, and then select templates from here and this is the template here I've got a template called chart one. So I'll click OK and then the new chart will be inserted according to my template and I believe this is an easier way to do it if you just want to change the colors. Okay?
This is easier than VBA This is not needing an expert user in Excel somebody who is familiar with VBA you know to do it so that's it guys for the workbook, new chart event. Thank you very much watching this video. I'll see you on the next one.