Solved

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

Posted on 2014-11-06
3
124 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
[X]
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
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
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…

627 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