Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

What is going wrong with my ByRef call in VBA (Excel  2013)?

Posted on 2014-11-06
3
Medium Priority
?
127 Views
Last Modified: 2014-11-07
I am passing a parameter by reference but it is not changing in the way that I would expect.  I set it to 16.1, pass it to a subroutine by reference which should lead to its value changing to 42.42 but this does not happen.  What am I doing wrong?  I am working in Excel 2013.

Sub AdjustANumber(ByRef a As Double)

    a = 42.42
    
End Sub
Sub TestAdjustANumber()

    Dim a As Double
    a = 16.1
    
    ' Here the debugger tells me a = 16.1. This is correct.
    AdjustANumber (a)
    ' Here the debugger tells me a = 16.1 but I expected a = 42.42 since it was passed by reference to the AdjustAnumber subroutine.
    MsgBox a  ' This MsgBox displays 16.1 which is incorrect
    
End Sub

Open in new window


This seems to be in agreement with this:
http://www.excel-easy.com/vba/examples/byref-byval.html
and this:
http://msdn.microsoft.com/en-us/library/ddck1z30.aspx
Maybe I didn't configure my *.xlsm file to allow passing by reference?  The above code is defined in Modules->Module1 of my Excel file.
0
Comment
Question by:e_livesay
3 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40427663
Hello,

use it like this:

Function AdjustANumber(ByRef a As Double) As Double

    AdjustANumber = 42.42
    
End Function
Sub TestAdjustANumber()
Dim a As Double
 
    a = 16.1
  
    ' Here the debugger tells me a = 16.1. This is correct.
   a = AdjustANumber(a)
    ' Here the debugger tells me a = 16.1 but I expected a = 42.42 since it was passed by reference to the AdjustAnumber subroutine.
    MsgBox a   ' This MsgBox displays 16.1 which is incorrect
    
End Sub

Open in new window

0
 
LVL 17

Accepted Solution

by:
vb_elmar earned 2000 total points
ID: 40427726
instead...
AdjustANumber (a)
try it without brackets...
AdjustANumber a

Sub AdjustANumber(ByRef a As Double)
    a = 42.42
End Sub

Sub TestAdjustANumber()
    Dim a As Double
    a = 16.1
    AdjustANumber a
    MsgBox a
End Sub

Open in new window

0
 

Author Closing Comment

by:e_livesay
ID: 40428172
Straightforward solution that addressed the ByRef issue directly instead of trying to bypass it.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a serious pitfall that can happen when deleting shapes using VBA.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question