![]() ![]() ![]() It comes pre-initialized.Īn empty string is not the same as an uninitialized variable. That’s because the default value for a string is the empty string "". If we declare the variable as a string, the IsEmpty function will always return False. ![]() This doesn’t work for all data types! It only works for the Variant in this example because the default value for a variant is Empty. That’s what we did right before our 3rd MsgBox. Only when you set the variant back to Empty will the IsEmpty command once again return True. Once you assign it the value of Hello there!, the variable is no longer empty so the IsEmpty function returns False. In other words, even though it’s been declared a variable of type Variant, it hasn’t been assigned a value. The first MsgBox returns True because the variable str1 has not been initialized - it’s empty. Sub IsEmptyExample2 () Dim str1 As Variant MsgBox IsEmpty ( str1 ) 'Returns True str1 = "Hello there!" MsgBox IsEmpty ( str1 ) 'Returns False str1 = Empty MsgBox IsEmpty ( str1 ) 'Returns True End Sub IsEmpty Examples Check if a Cell is Blank Let’s take a look at some examples to see what I mean. The “Expression” argument IsEmpty accepts is a Variant, so it can be pretty much anything - a variable, an object, a range and more.Īs I mentioned in my IsNumeric VBA Tutorial, it’s a good practice to check if your cells are empty before applying certain VBA functions. Recall, a Logical Boolean returns either True or False. It gives you your result as a Logical Boolean. We’ll get to some examples shortly, but first, let’s start this tutorial with some IsEmpty Basics. In other words, you can use it to see if your macro has assigned a value to a variable. ![]() Not only can IsEmpty check if a cell or range is empty, it can check if a variable is uninitialized. The IsEmpty VBA function can be used to check if a cell is blank, just like the Excel ISBLANK worksheet function. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |