|
-
Feb 7th, 2006, 09:46 AM
#1
Thread Starter
New Member
[RESOLVED] Calling Procedures
I'm very new to VBA, but the following code produces a 'type mismatch' error (on the 'N' on the call to XLRead) but no error when all lines and references to 'M' are removed:
Dim N,M As Integer
Sub CopyInteger()
XLRead N,M
XLPrint N,M
End Sub
Sub XLRead(N As Integer,M As Integer)
N=Cells(1,2).Value
M=Cells(2,2).Value
End Sub
Sub XLPrint(N As Integer,M As Integer)
Cells(1,3).Value=N
Cells(2,3).Value=M
End Sub
Thanks for any help.
Ken.
-
Feb 7th, 2006, 09:51 AM
#2
Re: Calling Procedures
N is not declared as an Integer... its a variant... change the dim to this
(and try to use the vbcode tags it helps to read it)
oh yeah.. and WELCOME! 
VB Code:
Dim N As Integer, M As Integer 'THIS
Sub CopyInteger()
XLRead N, M
XLPrint N, M
End Sub
Sub XLRead(N As Integer, M As Integer)
N = Cells(1, 2).Value
M = Cells(2, 2).Value
End Sub
Sub XLPrint(N As Integer, M As Integer)
Cells(1, 3).Value = N
Cells(2, 3).Value = M
End Sub
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Feb 7th, 2006, 09:55 AM
#3
Re: Calling Procedures
hmm... I shouldve looked at your code more...
u are passing in variables to be set?
u should be using a function to return values...
really for what u are doing.. I would do this
VB Code:
Dim N As Integer, M As Integer
Sub CopyInteger()
XLRead
XLPrint
End Sub
Sub XLRead()
N = Cells(1, 2).Value
M = Cells(2, 2).Value
End Sub
Sub XLPrint()
Cells(1, 3).Value = N
Cells(2, 3).Value = M
End Sub
since you are declaring N & M in the gen dec section.. you dont need to pass them in at all....
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Feb 7th, 2006, 09:55 AM
#4
Re: Calling Procedures
Welcome to VB Forums! I moved your thread to here which is the forum for VBA.
You can also make your code easier to read if you surround the code with VBCode tags like this.
[vbcode]
'Your code
[/vbcode]
which will cause it to show up like this.
-
Feb 7th, 2006, 10:17 AM
#5
Thread Starter
New Member
Re: Calling Procedures
Thanks very much. Somewhere I thought variables could all be Dim-ed without repeating the 'As' declaration.
-
Feb 7th, 2006, 10:45 AM
#6
Re: Calling Procedures
They can be defined the way you did, but you really don't want to do it that way. The reason is that when you do Dim N, M As Integer only M is defined as an Integer and N is defined by default as a Variant. Variants are slower to use and larger than other data types and should be avoided.
-
Feb 7th, 2006, 01:26 PM
#7
Thread Starter
New Member
Re: Calling Procedures
Thank you very much.
Ken.
-
Feb 7th, 2006, 02:34 PM
#8
Re: Calling Procedures
Now that we've helped you, you can help us by pulling down the Thread Tools menu and clicking the Mark Thread Resolved button which will let everyone know that you have your answer.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|