Video transcription

Hi, my name's Dave Andrews. Today, I'm going to show you how to use an If-Then statement in Excel VBA. Let's open up Excel. Now, we have to open up the Excel of Visual Basic Editor. To do so, hold the alternate key on your keyboard. It's ALT right next to the space bar and press F-eleven. Now, what we're going to do here is we're going to go to insert and we're just going to say module. Now, within this module we're going to put in a function. So, let's just type a little bit of code here. Alright, now here's my If statement. If the worksheet cells one-one, which is going to correspond to cell A-one within the worksheet. If that cells value if equal to Dave with a capital D then and that's where the then is. Set worksheet cell one-two, which is going to be cell B-one. We're going to set that value to Hello Dave. Here we have an else. That means if this is not true meaning if that value is not equal to Dave, then run this code. And, this one will set B-one to say You're not Dave exclamation point. Here we have an End-If statement, which closes out our If. For every If you open, you have to end that If. And, then we end our function. Now, let's go and use the function. I'm going to minimize this. And, we have to have some way to make that happen. So, I'm going to insert a little shape here. Just a circle. I'm going to draw that circle somewhere on my worksheet. You can do it. You can use anything. I'm just using a shape. Right click on the shape, say assign macro. And, we want to create a new macro. This opens back up our VBA and it wants us to type in code for what happens when that oval is clicked. I'm going to say module one, which is the module we created. That's the name of it, myfunc. That just says run myfunc when this oval is clicked. Now, let's close our VBA. I'm going to type Todd into A-one. I'm going to click on the oval. Our If-Then statement is just run and it said you're not Dave! So, I'm going to delete that and type Dave. Click my oval again and it says Hello Dave! My name is Dave Andrews and I've just showed you how to use an If-Then statement in Excel VBA.