ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   fighting the type mismatch error (https://www.excelbanter.com/excel-programming/302448-fighting-type-mismatch-error.html)

mrmark[_5_]

fighting the type mismatch error
 
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

--
Message posted from http://www.ExcelForum.com


Doug Glancy

fighting the type mismatch error
 
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

"mrmark " wrote in message
...
I want to use cell numerical values from a spreadsheet in a program in
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 operator
with a worksheets("sheetx").Range("x") value which i think VB thinks
these are srings and not intergers. Will CInt work for my purpose or
no? Also is my syntax right for calling a cell
(Worrksheets("sheet2").Range("B15"))?
-Mark


---
Message posted from http://www.ExcelForum.com/




gitcypher[_24_]

fighting the type mismatch error
 
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

--
Message posted from http://www.ExcelForum.com



All times are GMT +1. The time now is 09:34 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com