[VBA Excel] si variable = vide alors...

Solved
Hello,
I am working on a VBA program with a table containing references and I would like to search for a reference in this table.
However, the reference given is not necessarily in the table.

So, I created a text box to input the reference from the keyboard, and a search box with a variable.range function to find the reference in the table.

So far, so good.

After that, I want to have a variable if the reference is in the table then this happens...
and
else if the reference is not in the table then something else happens...

But I get error 91 when I don't have the same reference in the table, error = variable.range is empty.

I would like to know if we can write if variable = empty, because I don’t know how to do it. (I tried ="" and <>"")

Or if another solution exists other than searching.

Thank you.

3 answers

  • Votes
  • Date
  • Descending date
  1. Hello,

    Typical example:

     Option Explicit Sub Test() Dim Value As Variant Value = ActiveCell.Value If (IsEmpty(Value)) Then MsgBox "Empty" Else MsgBox "Not Empty" End If End Sub 

    Lupin
    1. yet his answer was perfectly correct. Using IsEmpty is defensible.
      For clarity, IsEmpty(value) returns true if value is empty, false otherwise.
      1. Contributor
        The position is 4 years old :/.
        Moreover, IsEmpty and Is Nothing do not serve the same purpose.
    2. Lupin.A's solution didn't really help me, but thanks anyway.

      Actually, all I needed to write was:
      If variable is Nothing then

      and not:
      If variable = nothing then.