# defining a boolean without it having an actual value

#### mortgageman

##### Well-known Member
In the following code, the test fails, because, once bTest is declared as a Boolean, it becomes equal to FALSE (I checked this after much head scratching). How does one assign a value to a boolean variable only after you make your test?

Code:
``````Sub foo()

Dim bTest As Boolean

If Range("a20").Value = False Then bTest = False ' <= a20 is kept empty
MsgBox bTest

End Sub``````

### Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

#### Lewiy

##### Well-known Member
Something tells me that the solution is to ensure that your Boolean variable is given a particular value whatever the outcome of the test, i.e:
Code:
``````Sub foo()
Dim bTest As Boolean
If Range("a20").Value = False Then
bTest = False
Else: bTest = True
MsgBox bTest
End Sub``````

#### mortgageman

##### Well-known Member
Something tells me that the solution is to ensure that your Boolean variable is given a particular value whatever the outcome of the test, i.e:
Code:
``````Sub foo()
Dim bTest As Boolean
If Range("a20").Value = False Then
bTest = False
Else: bTest = True
MsgBox bTest
End Sub``````

I don't think that did it. When I ran the code (I added the end If :wink: )
It displayed TRUE for bTest. Which means that the test was evaluating an empty cell the same as FALSE. I am quite perplexed to say the least

Code:
``````Sub foo()
Dim bTest As Boolean
If Range("a20").Value = False Then
bTest = False
Else: bTest = True
End If
MsgBox bTest
End Sub``````

#### Joe4

How does one assign a value to a boolean variable only after you make your test?
Here is my theory as to what is happening.

Once you declare a variable as boolean, it must be either TRUE or FALSE. In most computer languages, FALSE is equivalent to 0 and TRUE is usually equivalent to 1 (or -1).

So, when you declare it as a BOOLEAN, it probably defaults to 0, which is equivalent to FALSE.

Just like if you were to declare as an INTEGER. If you check the value of that variable before you assign a value to, it will return 0, not NULL.

#### Norie

##### Well-known Member
Gene

Why not actually use the result of the expression to assign a value to the variable?
Code:
``bTest = Not(Range("A20").Value =False)``

#### Lewiy

##### Well-known Member
Ahh, "End If" my arch nemesis!!

What is the value of Range("A20")? Is it simply a True/False result?

#### mortgageman

##### Well-known Member
Run this code in a brand new worksheet. Don't type anything in any cell. You will see that the result is TRUE. Why should that be? Why should an empty cell have the same value as FALSE?

Code:
``````Sub Foo()
MsgBox (Range("A20").Value = False)
End Sub``````

#### mortgageman

##### Well-known Member
JM - you are 100% correct. But that doesn't explain my second question which is why does an empty cell (seem to) have the same value as FALSE?

#### Norie

##### Well-known Member
Gene

If you hover over Range("A20").Value the value is Empty.

My guess is that VBA is regarding that as false just like it would regard 0 as false.

This is probably to do with type conversion which can produce strange results in VBA, as well as Excel itself.

#### Lewiy

##### Well-known Member
Perhaps:
Code:
``````Sub Foo()
If Range("A20")="" then
MsgBox "False"
Else
MsgBox (Range("A20").Value = False)
End If
End Sub``````

Replies
0
Views
120
Replies
1
Views
51
Replies
2
Views
173
Replies
10
Views
416
Replies
151
Views
2K

1,191,025
Messages
5,984,198
Members
439,877
Latest member
kellylet

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back