Difficulties in calling arrays to functions or procedures using ByRef

JaeDyAy

New Member
Joined
Nov 8, 2011
Messages
6
Good evening, Mr Excel!

I'm in the process of writing a dynamic programming piece of code as part of a university case study. The assignment itself is very open-ended and doesn't mention VBA so there's no need to worry that I'm simply scounting for answers.

This would be the largest piece of VBA I've written and involves the need to write functions or procedures in a separate module in order to keep the code itself clear and repeatable. However, these functions involve altering the contents of arrays and I cannot for the life of me find a way to stop the arrays from going out of scope once the function/procedures have completed. Attempts to search for solutions involving ByRef with arrays on the Internet are met with frustrating errors.

Here is a snippet of the code; feel free to pm if I intrigue you enough to want to look at the whole thing. I'm willing to write comments in the code highlighting whereabouts the issue arises if that's the case.

-----------------------------------------

Module "Main"

Public alngHeadsAv(1 to 10) As Long

[...]

Public Sub Main()

[...]

headsAvEntry 'Function

End Sub

Module "Functions"

Public Function headsAvEntry()

alngHeadsAv(1) = 1
alngHeadsAv(2) = 3
[...]
alngHeadsAv(10)=12

End Function 'data goes out of scope here.

----------------------------------------------

I'm quite sure I need to use ByRef somehow, but every variation of its use on the Internet is met with errors. I'm using Excel 2010 and am on Windows 7 at a University computer. Any help at all would be appreciated; even an abstract example of ByRef for arrays that would WORK would make me very appreciative indeed.

Thank you~!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I'm not sure you've supplied enough information to help.

The code you posted will run without error and you are not passing any variables (ByRef or otherwise). You are using a global array (which is fine and will be valid in all Modules of the active VB Project.

What exactly do you mean by out of scope? Are you getting an error or just getting unexpected results?
 
Upvote 0
Ah, my apologies. After what you'd said, I've just added a bunch of dummy object and they pick up the values of interest. My problem was that I thought "<out of context>" in the debugger window meant the information had been wiped. Turns out it hadn't... which is great! Thank you very much for your consideration all the same.
 
Upvote 0

Forum statistics

Threads
1,216,586
Messages
6,131,571
Members
449,655
Latest member
Anil K Sonawane

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top