Results 1 to 4 of 4

Argument ByRef in a Function when not using the return value.

Threaded View

  1. #1
    Registered User
    Join Date
    09-02-2022
    Location
    Brussels, Belgium
    MS-Off Ver
    2016
    Posts
    2

    Argument ByRef in a Function when not using the return value.

    I have noticed an oddity when passing argument ByRef in a function when not using the return value. Take this example:


    Option Explicit
    
    Function Triple(ByRef x As Integer) As Integer
    
    x = x * 3
    Triple = x
    
    End Function
    
    
    Sub test()
        Dim x As Integer
        Dim y As Integer
        x = 5
        y = Triple(x)
        Debug.Print x
    End Sub
    It print 15, as expected. Now remove the assignement when calling Triple:

    Option Explicit
    
    Function Triple(ByRef x As Integer) As Integer
    
    x = x * 3
    Triple = x
    
    End Function
    
    
    Sub test()
        Dim x As Integer
        Dim y As Integer
        x = 5
        Triple(x)
        Debug.Print x
    End Sub
    To my surprise, it prints 5. Why this behavior? Is it documented somewhere? (I am on Excel 2016).
    Last edited by Ovier; 09-02-2022 at 04:26 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. byref argument type mismatch error in function
    By Aphis in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-29-2015, 05:46 AM
  2. [SOLVED] Function ByRef Argument Error
    By cocacrave in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-25-2015, 06:39 AM
  3. ByRef argument type mismatch - problem wint function
    By kmeld in forum Excel Programming / VBA / Macros
    Replies: 38
    Last Post: 12-30-2013, 09:53 AM
  4. Not able to execute a function - ByRef argument type mismatch
    By dax2ib in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-22-2013, 01:56 AM
  5. [SOLVED] ByRef argument type mismatch
    By Lloyd Blankfein in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-06-2012, 07:26 AM
  6. [SOLVED] ByRef argument type mismatch / Passing array as a function argument
    By pzling in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-29-2012, 06:23 PM
  7. Byref argument type mismatch
    By ref in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-16-2012, 06:17 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1