5 rarely used features in Excel VBA

.

Dear friends,

As promised last week, in this article, I am going to talk about 5 rarely used features of Excel VBA. [I am not saying that all of these features are awesome to use… I have provided my personal choices for each of them]

1# Using IIF in Excel vba

IIF is a type of IF ELSE END IF. Of course, it is not exactly the same as a normal if-else statement.

Syntax of IIF Statement

IIF(Expression to check, value if true, value if false )

As you can see this, it pretty much looks like If, do this, else do this. But this is not the same as the If else statement.

Difference between If-Else and IIF

  1. In If-else, else part is not mandatory. You can write an If statement even without any else blocks whereas in IIF both – true and false values are mandatory to provide. You can skip either of them.
  2. In If-else, If condition expression is true then – Code inside IF block is evaluated and executed and Else Block is completely ignored. Whereas in IIF, both the expressions [value if true part] and [value if false part] are evaluated even though only one is executed at a time. Therefore you should be careful while using this statement to evaluate some conditions.

Note:

2nd difference mentioned above is one of the main differences between the If Else and IIF statements. This is also one of the main reasons – I do not prefer to use this more often.

Above explanation will be more clear with the following example.


Sub IIF_Demo()

Dim x As Integer
Dim y As Integer
Dim div As Integer
x = 0
y = 2
IIf x <> 0, div = y / x, div = 0
Debug.Print div
End Sub


Note: Above program will throw a run time error divide by zero error. As mentioned above, even though condition x is not equal to zero – true and code should execute div = 0, it will also evaluate div = y/x expression as well and therefore this error.

Same logic is written using If Else in the below code and it will run successfully without any error.


Sub IFELSE_Demo()
Dim x As Integer
Dim y As Integer
Dim div As Integer
x = 0
y = 2
If (x <> 0) Then
    div = y / x
Else
    div = 0
End If
Debug.Print "If Else executed and div value is calculated... div = "; div
End Sub

2# End If is not always mandatory for If statement

If you can type your IF and/or Else statement(s) in single line as shown below then you do not need to type End If.


 Sub IfWithoutElseDemo()
    Dim a As Integer
    Dim b As Integer
    a = 1
    b = 2
    If (a > b) Then MsgBox a Else MsgBox b
End Sub

Remarks: Personally I prefer this only when
1. There is no Else part in If Statement
2. Single statement is executed as part of If

If any of the above criteria is not met, then I prefer using the If and End If – Complete set. This is my personal preference</>. This is also because it makes the code more readable else it will be difficult to read the If Else part.

3# Colon in VBA as – End of Statement

As you know in VBA new line itself is considered the end of the statement. Therefore, if you want to type multiple statements in a single line then you can use a colon as the end of a statement in VBA.


Sub ColonAsEndOfStatement()
    Dim a As Integer
    Dim b As Integer
    a = 1
    b = 2
    If (a > b) Then MsgBox "Line 1": MsgBox "Line 2": MsgBox "Line 3" Else MsgBox "Else part 1": MsgBox "Else part 2"
End Sub

Note: Personally I do not like this at all. I prefer each statement written in a new line. Using multiple statements in a single line using a colon reduces the code readability drastically.

4# Using immediate window and Debug statement

This is one of my favorite features of Excel VBA. To know more about this, I have written a detailed article about this. Read this article to know more about:
What is immediate window?
What is Debug command and how to use it?

5# Replace text in a String using mid function

Using Mid function, you can replace text in a string.


Sub replaceTextUsingMid()
    Dim inputStr As String
    inputStr = "Excel is logical"
    Mid(inputStr, 10, 2) = "ma"
    MsgBox inputStr
End Sub

Result:

The above code will result in the following message box with Text – “Excel is magical”

Note:

Integer Length specified in the Mid function should be the same as the length of the string which you want to replace it with. For example, in the above code, I have used 2 in mid function and length of string ma is also 2.

In case these two are not equal, the expression will not throw any error. It will simply take the lowest of both and does its job.

Example 1:
Following will result – Excel is magical


    inputStr = "Excel is logical"
    Mid(inputStr, 10, 3) = "ma"

Example 1:
Following will result – Excel is magical


    inputStr = "Excel is logical"
    Mid(inputStr, 10, 2) = "man"

[content_boxes layout=”icon-on-side” columns=”1″ backgroundcolor=”#F4EB94″ ][content_box title=”Important Info” icon=”” backgroundcolor=””]This method can be used effectively when you want to replace some fixed number of characters located at some specific location in a given string irrespective of what those strings are.
In this case VBA.Replace() is not useful because to use this Replace() function – you need to know what exactly the string is at a given location in a string to replace.

So this is a useful feature that can be used effectively in a particular situation.[/content_box][/content_boxes]

Buy a coffee for the author

Adsense

Download FREE Tools and Templates

There are many cool and useful excel tools and templates available to download for free. For most of the tools, you get the entire VBA code base too which you can look into it, play around it, and customize according to your need.

Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide
Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide

In today's tutorial, we'll be diving into the exciting world of dynamic arrays and spill functions in Office 365 Excel. These features have revolutionized the way we work with data, providing a more flexible and efficient way to handle arrays. I am going to explain...

How to Declare a Public Variable in VBA
How to Declare a Public Variable in VBA

While programming in VBA sometimes you need to declare a Public Variable that can store the value throughout the program. Use of Public Variable: Let's say you have 4 different Functions in your VBA Code or Module and you have a variable that may or may not be...

How to Copy content from Word using VBA

As many of us want to deal with Microsoft Word Document from Excel Macro/VBA. I am going to write few articles about Word from Excel Macro. This is the first article which opens a Word Document and read the whole content of that Word Document and put it in the Active...

What is Excel Formula?

Excel Formula is one of the best feature in Microsoft Excel, which makes Excel a very very rich application. There are so many useful built-in formulas available in Excel, which makes our work easier in Excel. For all the automated work, Excel Macro is not required. There are so many automated things can be done by using simple formulas in Excel. Formulas are simple text (With a Syntax) which is entered in to the Excel Worksheet Cells. So how computer will recognize whether it is a formula or simple text? Answer is simple.. every formula in Excel starts with Equal Sign (=).

You May Also Like…

3 Comments

  1. Kabilan

    Good one Vishwa

    Reply
  2. Sandeep Kothari

    Gr8!
    You are truly Vishwamitra!

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join and get a FREE! e-Book

Don't miss any articles, tools, tips and tricks, I publish here

You have Successfully Subscribed!

Pin It on Pinterest