Katto Ishimura
Katto Ishimura

Reputation: 35

VBA - Is there an "on error" statement with an ELSE option?

In VBA, I need to have two options for using a variable in an excel cell:

  1. if there is a value in the cell
  2. if there is not a value in the cell

I assume that VBA will give me an error if I tried to read a cell when nothing is in it, so not sure how to do it.

I would like to do something like

A=sheet1.cells(1,1).value
on error goto fail

fail:
A=0
' need to continue here, with A from the cell or set to 0
B=A+1

Upvotes: 0

Views: 44

Answers (1)

Storax
Storax

Reputation: 12167

Although Scott's answer in the comment is certainly right I usually take the Len function to check for an empty cell or string

If len(A) = 0 then
' do sth
else
' sth else
end if 

The answer to the other question is: There is no Else for On Error

Upvotes: 1

Related Questions