How To Use Conditional (IF) Statements In Excel VBA (Real-World VBA Task S3 P6)
How To Use Conditional (IF) Statements In Excel VBA (Real-World VBA Task S3 P6)
The concept of ‘conditionality’ is the final coding concept we cover in the Excel VBA Real-World Task series, season 3. Alongside loops, position control and variables, if statements will lend your VBA toolkit a sense of completeness, equipping you with what you need to get most tasks done. So, what are they all about?
DOWNLOAD THE EXCEL FILES
%20Statements%20In%20Excel%20VBA%20(Real-World%20VBA%20Task%20S3%20P6).jpg)
CLICK HERE TO WATCH THE VIDEO
An if statement in Excel VBA sends the code one of two or more ways according to a ‘condition’. I think about it like this: ‘If something is happening, do that; if it’s not happening, do this’. There you go, conditional statements, simply explained! So, that’s the concept; but how does it apply to our task?
Our example should help your understanding. We want to add the game result (1, X , 2) to our collated analysis, but only if the team in the dataset is the ‘target’ team. In other words, if we’re creating form analysis for Greyhound, we only want to grab a result if the row in the dataset relates to Greyhound’s home games. That verbose explanation may or may not help – the video makes things clearer …
In the video, I use an If … End if statement to get the function we need. Excel VBA offers a range of conditional statements (Select Case is supremely powerful and well-worth checking out), but the If … End if construct is the one you’ll use most. Watch me build it step-by-step in this video and, as always, make sure you’re working along with me.
We’re getting towards the end of the Excel VBA Real-World task series, season 3! Well done for getting this far, I would love to hear how you are getting on in the YouTube comments. See you in video 7.
ABOUT OUR MEMBER COMMUNITIES
Our learning communities provide exclusive Excel learning content, direct access to Chris, support and inspiration from others just like you and much, much more. They are the best way to develop your Excel skills long-term, and to make a few friends along the way! Check out the options below, we would LOVE to welcome you ...
Our learning communities provide exclusive Excel learning content, direct access to Chris, support and inspiration from others just like you and much, much more. They are the best way to develop your Excel skills long-term, and to make a few friends along the way! Check out the options below, we would LOVE to welcome you ...
ABOUT REAL-WORLD VBA TASKS SEASON 3
In the Real-World VBA Task series, we tackle a VBA challenge similar to the jobs that professionals you like are trying to automate daily using Excel VBA.
In the Real-World VBA Task series, we tackle a VBA challenge similar to the jobs that professionals you like are trying to automate daily using Excel VBA.
In Season 3 of the Excel VBA Real-World Task series, we explore a job I’ve been tasked with many times in my career: creating form analysis for football teams in a league table. Form analysis the kind of thing you’ll see alongside football league tables in the newspaper or online, and it’s the perfect topic for this series for two reasons. First, it’s a task that’s not easily done in Excel without using VBA (though it’s possible using a series of very long formulae). Second, it requires powerful techniques such as loops, conditional statements, position control and more – so it’s broadly applicable to VBA tasks generally. In other words, the series should help whatever VBA job you’re currently wrestling with.
In this 7-part season, I walk you through the VBA task step-by-step, from the critical planning and conceptualization phase, to application of loops, variables and more in VBA, through to combining techniques together to create a powerful ‘click-of-button’ solution. Even I was surprised by how quickly this macro gets the job done …
It’s about more than impressing you with individual techniques such as loops in Excel VBA, however. It’s about learning an overall approach to VBA development that you can apply in your work. Look out for the Excel ‘Metaskills’ such as debugging that I showcase during the series – they really are the hidden gems in Excel learning. I hope enjoy the series and do let me know in the YouTube comments how you get on.