Solved

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

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now