filmov
tv
The Only 3 IF Statements You Need In Excel VBA - Excel VBA Beginner Fundamentals (6/6)
Показать описание
Conditional statements allow us to ‘send’ Excel in one of two (or more) directions when running VBA code. They work through a ‘condition’ or a logical statement – is something happening in the workbook?
📊DOWNLOAD COURSE HANDBOOK / EXCEL FILES
For example, is the value in cell A1 more than 10? If the ‘condition’ or logical statement is met, then Excel jumps to one place in the code; if not, it jumps to another. In this way, Excel diverts the code in one of two directions. Cool!
Conditional statements feature in most powerful computer programmes. I love learning (and teaching) conditional statements because you only need 3 structures to do everything you would need to do involving conditionality in Excel VBA. They build nicely from a very simple to sophisticated. So, let’s look at the first one.
🔥Excel VBA Beginner Fundamentals Playlist
1. ONE-LINE CONDITIONAL STATEMENT USING IF
The first building block is a one-line conditional statement using IF. It’s simple but, applied correctly, is often enough to get the job done. In literal terms, it tells Excel to do something if something is happening. There are a few syntax rules to observe here – the most important of which is the whole statement must be on a single line. That means both the condition - and what to do if the condition is met.
Follow along with the video and build and test the one-line conditional statement with me.
2. IF – ELSE – END IF
The second building block for conditional statements in Excel VBA is more powerful but significantly more difficult to put together. IF – ELSE – END IF gives us two options: what to do if the condition is met, and what to do if it isn’t.
Once again – syntax is important. The main difference compared to the one-line conditional statement is the instructions are now placed on a different line to the condition. This can confuse and has cost me hours of time over the years! Once you’ve practiced the basic IF – ELSE – END IF structure, however, you’ll be harnessing its power for years to come. Follow along with the video as I build, test and debug it.
A sidenote here: you can build on this structure using ELSE IF to introduced more than one mutually exclusive option. I find this syntax too complicated, however, which is why I favour our final building block …
3. SELECT CASE
So, we’ve looked at options to divert Excel in one direction if a condition is met, and in one of two directions. But, what if you needed to programme 3 options, or more?
This is where SELECT CASE comes in. Yes, the language is different which certainly takes some getting used to. But, it’s so worth it in the end because SELECT CASE gives us the flexibility to programme any kind of conditionality logic using Excel VBA. It’s one of my all-time favourites!
First, we use SELECT CASE (object) to show Excel what to look at. Then, we list options for the (object); Excel automatically jumps to the option that applies. We can even introduce a CASE ELSE catch-all option to ensure all bases are covered.
Once again, follow along with the video as I create a SELECT CASE statement with Excel VBA. If the syntax is too much for the moment, that’s fine: simply watch the video and try to appreciate the power of SELECT CASE – you’ll be using it in no time!
So there we have it, the three structures I use to programme conditionality with Excel VBA. If you’re steady and systematic and build from one idea to the next, I’m absolutely sure you’ll be able to integrate these powerful mechanisms into your Excel VBA vocabulary, adding flexibility and power to your VBA applications.
If you’ve been following along, I hope you’ve enjoyed the Excel VBA Building Blocks series. Finally, there will be a special session to celebrate the end of this series only with those who have signed up for the resources on the website. So, make sure you sign up at the link above to access this special live session, the details of which I will send out in the coming weeks.
ABOUT THE EXCEL VBA BUILDING BLOCKS COURSE
📊DOWNLOAD COURSE HANDBOOK / EXCEL FILES
Learn the fundamentals of Excel VBA in around an hour with Tiger's FREE Excel VBA Building Blocks course. The course is based on Chris' 10 years of Excel training, consultancy and content creation, and teaches the basics of Excel VBA around 18 simple ideas or 'building blocks'.
In addition to the 6 basic videos on Excel VBA, on the website you can find more 'metaskill' videos (to be uploaded through Summer 2021) about next level concepts that could unlock your potential with Excel VBA.
A course handbook is provided for free that lists each of the Excel VBA beginner building blocks and includes a learning template for each session - make sure you download and use it to support your learning.
Use the link below to download the course handbook and access the learning template for this session.
📊DOWNLOAD COURSE HANDBOOK / EXCEL FILES
For example, is the value in cell A1 more than 10? If the ‘condition’ or logical statement is met, then Excel jumps to one place in the code; if not, it jumps to another. In this way, Excel diverts the code in one of two directions. Cool!
Conditional statements feature in most powerful computer programmes. I love learning (and teaching) conditional statements because you only need 3 structures to do everything you would need to do involving conditionality in Excel VBA. They build nicely from a very simple to sophisticated. So, let’s look at the first one.
🔥Excel VBA Beginner Fundamentals Playlist
1. ONE-LINE CONDITIONAL STATEMENT USING IF
The first building block is a one-line conditional statement using IF. It’s simple but, applied correctly, is often enough to get the job done. In literal terms, it tells Excel to do something if something is happening. There are a few syntax rules to observe here – the most important of which is the whole statement must be on a single line. That means both the condition - and what to do if the condition is met.
Follow along with the video and build and test the one-line conditional statement with me.
2. IF – ELSE – END IF
The second building block for conditional statements in Excel VBA is more powerful but significantly more difficult to put together. IF – ELSE – END IF gives us two options: what to do if the condition is met, and what to do if it isn’t.
Once again – syntax is important. The main difference compared to the one-line conditional statement is the instructions are now placed on a different line to the condition. This can confuse and has cost me hours of time over the years! Once you’ve practiced the basic IF – ELSE – END IF structure, however, you’ll be harnessing its power for years to come. Follow along with the video as I build, test and debug it.
A sidenote here: you can build on this structure using ELSE IF to introduced more than one mutually exclusive option. I find this syntax too complicated, however, which is why I favour our final building block …
3. SELECT CASE
So, we’ve looked at options to divert Excel in one direction if a condition is met, and in one of two directions. But, what if you needed to programme 3 options, or more?
This is where SELECT CASE comes in. Yes, the language is different which certainly takes some getting used to. But, it’s so worth it in the end because SELECT CASE gives us the flexibility to programme any kind of conditionality logic using Excel VBA. It’s one of my all-time favourites!
First, we use SELECT CASE (object) to show Excel what to look at. Then, we list options for the (object); Excel automatically jumps to the option that applies. We can even introduce a CASE ELSE catch-all option to ensure all bases are covered.
Once again, follow along with the video as I create a SELECT CASE statement with Excel VBA. If the syntax is too much for the moment, that’s fine: simply watch the video and try to appreciate the power of SELECT CASE – you’ll be using it in no time!
So there we have it, the three structures I use to programme conditionality with Excel VBA. If you’re steady and systematic and build from one idea to the next, I’m absolutely sure you’ll be able to integrate these powerful mechanisms into your Excel VBA vocabulary, adding flexibility and power to your VBA applications.
If you’ve been following along, I hope you’ve enjoyed the Excel VBA Building Blocks series. Finally, there will be a special session to celebrate the end of this series only with those who have signed up for the resources on the website. So, make sure you sign up at the link above to access this special live session, the details of which I will send out in the coming weeks.
ABOUT THE EXCEL VBA BUILDING BLOCKS COURSE
📊DOWNLOAD COURSE HANDBOOK / EXCEL FILES
Learn the fundamentals of Excel VBA in around an hour with Tiger's FREE Excel VBA Building Blocks course. The course is based on Chris' 10 years of Excel training, consultancy and content creation, and teaches the basics of Excel VBA around 18 simple ideas or 'building blocks'.
In addition to the 6 basic videos on Excel VBA, on the website you can find more 'metaskill' videos (to be uploaded through Summer 2021) about next level concepts that could unlock your potential with Excel VBA.
A course handbook is provided for free that lists each of the Excel VBA beginner building blocks and includes a learning template for each session - make sure you download and use it to support your learning.
Use the link below to download the course handbook and access the learning template for this session.
Комментарии