fighting the type mismatch error

  • Thread starter Thread starter mrmark
  • Start date Start date
M

mrmark

I want to use cell numerical values from a spreadsheet in a program i
V.B.


Dim value1 As Integer
Dim value2 As Integer
Dim value3 As Integer


value1 = CInt(Worksheets("sheet2").Range("B15"))
value2 = CInt(Worksheets("sheet2").Range("B16"))
value3 = CInt(Worksheets("sheet2").Range("B17"))

If Abs(value1 - value2) <= 0.001 Then
value3 = Rnd() * 0.007

I get a type mismatch error because i am trying to do a math operato
with a worksheets("sheetx").Range("x") value which i think VB think
these are srings and not intergers. Will CInt work for my purpose o
no? Also is my syntax right for calling a cel
(Worrksheets("sheet2").Range("B15"))?
-Mar
 
I am unable to reproduce the error, in XL 2000 it does the computations of
text numbers even without Cint, so can't help there. I do have another
comment, though. Declaring your variables as integers means that they are
rounded/truncated so that .01 becomes 0 and 5.5 becomes 5, for example. I'd
declare them as doubles and use Cdbl if conversion is really required.

hth,

Doug Glancy
 
This is by no means a fix, but in the future, I've found it easier t
reference worksheets by their sheet number.
In the VB Editor, the upper left window (Project Explorer) lists th
sheet by name, then by number in parenthesis (SheetX). Instead o
Worksheets("sheet2").Range("B15"), you can use Sheet2.Range("B15")..
unless of course, your sheet has a different number.

-Gitcyphe
 
Back
Top