how to convert a cell value into boolean

  • Thread starter Thread starter cghersi
  • Start date Start date
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
 
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
 
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
 
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
 
Back
Top