Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-11-06
3
Medium Priority
?
126 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 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 descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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 in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

722 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