Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2014-11-06
3
121 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

860 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