Results 1 to 8 of 8

Thread: [RESOLVED] Calling Procedures

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    5

    Resolved [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.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    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:
    1. Dim N As Integer, M As Integer 'THIS
    2. Sub CopyInteger()
    3.      XLRead N, M
    4.      XLPrint N, M
    5. End Sub
    6. Sub XLRead(N As Integer, M As Integer)
    7.      N = Cells(1, 2).Value
    8.      M = Cells(2, 2).Value
    9. End Sub
    10. Sub XLPrint(N As Integer, M As Integer)
    11.      Cells(1, 3).Value = N
    12.      Cells(2, 3).Value = M
    13. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    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:
    1. Dim N As Integer, M As Integer
    2. Sub CopyInteger()
    3.      XLRead
    4.      XLPrint
    5. End Sub
    6. Sub XLRead()
    7.      N = Cells(1, 2).Value
    8.      M = Cells(2, 2).Value
    9. End Sub
    10. Sub XLPrint()
    11.      Cells(1, 3).Value = N
    12.      Cells(2, 3).Value = M
    13. 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"

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.
    VB Code:
    1. ' Your code

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    5

    Re: Calling Procedures

    Thanks very much. Somewhere I thought variables could all be Dim-ed without repeating the 'As' declaration.

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    5

    Re: Calling Procedures

    Thank you very much.
    Ken.

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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
  •  



Click Here to Expand Forum to Full Width