mismatch error and Variants

G

Guest

I added an argument to a subroutine and then started getting the By Ref
argument type mismatch error--even though it appears my arguments were of the
right type. I eventually "fixed it" by changing one of the arguments to
Variant rather than Integer. My question is, what's going on here? Why am I
getting a type mismatch when I'm correctly dimensioning my variables?

Here's the call to the sub
GetP WRow, AnIBlk
in which WRow is Dim as Integer, and AnIBlk is Dim as Boolean.

And the Sub itself follows (It's presented in its entirety, although the
subs it calls aren't included). As I said, WRow is declared "As Variant"
because that's the only way I could avoid the mismatch error...when I had it
"As Integer" I got the error every time. The other way I found I could avoid
the error was to declare WRow As Integer globally (that is, at the top of the
module).

I'd appreciate any insights!

Sub GetP(WRow As Variant, AnIBlk As Boolean)

Dim IncAgl() As Double
Dim msg As String
Dim i, j, k, l As Integer
Dim coord As Variant
Dim Pp() As Double

ReDim Preserve Cx(Ncnr + 2) As Double
ReDim Preserve Cy(Ncnr + 2) As Double
ReDim Preserve r(Ncnr) As Double
ReDim IncAgl(Ncnr) As Double

ReDim Pp(2 * Ncnr + 3) As Double
For i = 0 To Ncnr + 1
Pp(2 * i) = Cx(i): Pp(2 * i + 1) = Cy(i)
Next i

For i = 0 To 2 * Ncnr + 3
Cells(WRow + 8, 3 + i) = Pp(i)
Next i

If Not AnIBlk Then

Dim pc(0 To 2) As Double
Dim tc(0 To 2) As Double
Dim nc(0 To 2) As Double
Dim pt As Variant
Dim pla, nlaAs Double
Dim d, ang As Double
Dim bAAs Double
Dim bx() As Double
Dim by() As Double
Dim B() As Double
ReDim bx(2 * Ncnr + 1) As Double
ReDim by(2 * Ncnr + 1) As Double
ReDim B(2 * Ncnr - 1) As Double

For k = 1 To Ncnr
pc(0) = Cx(k - 1): pc(1) = Cy(k - 1): pc(2) = 0
tc(0) = Cx(k + 0): tc(1) = Cy(k + 0): tc(2) = 0
nc(0) = Cx(k + 1): nc(1) = Cy(k + 1): nc(2) = 0

pla = AglX(tc, pc)
nla= AglX(tc, nc)
IncAgl(k) = pla - nagl
If IncAgl(k) < 0 Then IncAgl(k) = 2 * pi + IncAgl(k)

d = Abs(r(k) / Tan(IncAgl(k) / 2))
ang = pla

bx(2 * k - 1) = Ppx(tc, ang, d)
by(2 * k - 1) = Ppy(tc, ang, d)
d = Abs(r(k) / Tan(IncAgl(k) / 2)) 'see figure
ang = nagl

bx(2 * k) = Ppx(tc, ang, d)
by(2 * k) = Ppy(tc, ang, d)
'Set lineObj = ThisDwg.ModelSpace.AddLine(tc, pt)
Next k

bx(0) = Cx(0): by(0) = Cy(0)
bx(2 * Ncnr + 1) = Cx(Ncnr + 1): by(2 * Ncnr + 1) = Cy(Ncnr + 1)

ReDim Pp(4 * Ncnr + 3) As Double

For i = 0 To 2 * Ncnr + 1
Pp(2 * i) = bx(i): Pp(2 * i + 1) = by(i)
Next i

For i = 1 To 2 * Ncnr - 1 Step 2
k = (i + 1) / 2
bA= Tan((pi - IncAgl(k)) / 4)
B(i) = bAmt
Next i

For i = 0 To 2 * Ncnr + 1
Cells(WRow + 9, 3 + 2 * i) = bx(i)
Cells(WRow + 9, 4 + 2 * i) = by(i)
Next i
For i = 1 To 2 * Ncnr - 1 Step 2
Cells(WRow + 10, 3 + i) = B(i)
Next i
End If

End Sub
 
M

mudraker

Tony


when you call GetP is the data that you are using to supply to Wrow a
Integer if it is not
you will get a mismath error

eg the following code errors because data in Macro1 is a Long number
and Wrow expecting a Integer number

sub Macro1
dim myLong as Long
myLong = 45
call Macro2(myLong)
end sub

sub Macro2(Wrow as Integer)

mycodehere
end sub
 
G

Guest

Yes, WRow is dimensioned as an integer in the subroutine that has the GetP
call. I didn't want to include that entire subroutine as well because it's
also rather long. But it has lines:
Dimm WRow as Integer
Dim AnIBlk as Boolean

That is what is so puzzling to me about all this.

-Tony
 
J

Jim Cone

Your sub has multiple undeclared variables in it.
You should use "Option Explicit" as the first line of
every module. That forces you to declare all variables and
highlights misspelled variables for you.

I am guessing that your declaration of Wrow as an Integer may be flawed.
In your sub you use this line ... "Dim i, j, k, l As Integer" ...
which results in i, j, k, being declared as Variants and only
l being an integer.
If you did the same thing with Wrow, that is your problem.

In general, avoid the use of Integer as a data type.
The Long data type is preferred. Using an Integer for a row
number variable will cause an error above row ~32700.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"T_o_n_y" <[email protected]>
wrote in message
Yes, WRow is dimensioned as an integer in the subroutine that has the GetP
call. I didn't want to include that entire subroutine as well because it's
also rather long. But it has lines:
Dimm WRow as Integer
Dim AnIBlk as Boolean

That is what is so puzzling to me about all this.

-Tony
 
M

mudraker

Tony

Jim is correct if you declared your variables in your macros as you
have in your posted code
Dim i, j, k, l As Integer
It is only the last variable that is declared as a Integer

You need to use
Dim i as Integer
Dim J as Integer
Dim K as Integer

If you modify your declartions as above in all your macros

You can turn on the Option Explicit for all new workbooks & modules by
going to Tools > Options > Editor Tab > Require Variable Declaration

or having the very 1st entry on your modules as
Option Explicit

Using this option saves a lot debugging problems
 
G

Guest

Thank you, Jim. That really helped.

Jim Cone said:
Your sub has multiple undeclared variables in it.
You should use "Option Explicit" as the first line of
every module. That forces you to declare all variables and
highlights misspelled variables for you.

I am guessing that your declaration of Wrow as an Integer may be flawed.
In your sub you use this line ... "Dim i, j, k, l As Integer" ...
which results in i, j, k, being declared as Variants and only
l being an integer.
If you did the same thing with Wrow, that is your problem.

In general, avoid the use of Integer as a data type.
The Long data type is preferred. Using an Integer for a row
number variable will cause an error above row ~32700.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"T_o_n_y" <[email protected]>
wrote in message
Yes, WRow is dimensioned as an integer in the subroutine that has the GetP
call. I didn't want to include that entire subroutine as well because it's
also rather long. But it has lines:
Dimm WRow as Integer
Dim AnIBlk as Boolean

That is what is so puzzling to me about all this.

-Tony
 
G

Guest

It seems strange to me that you must declare each integer one at a time. But
if that's the way it must be done...

Anyway, thank you for the replies.
 

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