how to convert a cell value into boolean

C

cghersi

Hi.

I'm facing up the following issue: in a sheet there's a matrix of
boolean values (TRUE, FALSE) that I have to read/write via vba.

Sometimes (not deterministically), a statement like:

if Cells(2,4).value then
' do something
else
' do something else
end if

returns a type mismatch because it reads the cell as string and not as
boolean (obviously in Cells(2,4) there is a TRUE value).

This behaviour doesn't always happen, only sometimes (non
deterministically, as previously said).

The solution that I've thought to is to force the format of the cell
as boolean, bu I don't know how.

Any ideas? or anything else?

unfortunately the application must be language independent, so I
cannot use a comparison like

if Cells(2,4).value = "TRUE" then ...

Moreover, I would like not ot use parsing function (like CBool)
because the performance get worse and worse, and this is another
issue...

thank u very much

bye

CRI
 
J

Jim Cone

You could try...
If Trim(Cells(2,4).Value) = True Then
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)



"cghersi"
wrote in message
Hi.

I'm facing up the following issue: in a sheet there's a matrix of
boolean values (TRUE, FALSE) that I have to read/write via vba.

Sometimes (not deterministically), a statement like:

if Cells(2,4).value then
' do something
else
' do something else
end if

returns a type mismatch because it reads the cell as string and not as
boolean (obviously in Cells(2,4) there is a TRUE value).
This behaviour doesn't always happen, only sometimes (non
deterministically, as previously said).
The solution that I've thought to is to force the format of the cell
as boolean, bu I don't know how.
Any ideas? or anything else?
unfortunately the application must be language independent, so I
cannot use a comparison like
if Cells(2,4).value = "TRUE" then ...
Moreover, I would like not ot use parsing function (like CBool)
because the performance get worse and worse, and this is another
issue...
thank u very much
bye
CRI
 
D

Dave Peterson

You sure that there's a value of TRUE in that cell?

I've seen the mismatch error when there was an error in that cell:

with activesheet.cells(2,4)
if iserror(.value) then
msgbox "it's an error"
else
if .value = true then
msgbox "It's true"
else
msgbox "it's not true"
end if
end if
end with
 
C

cghersi

Try this:

MsgBox CBool(Cells(2,4).Value)














- Mostra testo tra virgolette -

Hi, thank u to all for the replies.

what about the performance of CBool operator?
what's the difference between a simple check:
if Cells(2,4).value then
and a CBool check like:
if CBool(Cells(2,4).value) then

thank u

CRI
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top