0

I have a UserForm with a ListBox for the user to select values. Those values are populated in UserForm_Initialize() via a function call to the base module, which returns an array as variant. This works without problems.

If the user selects some values and presses a button, the buttons Click event calls another function in the base module to pass on the user-entered array and compute things. This does not work at all. The value received in the base module is always nonexistent (not even null, but I don't know the correct VBA term, nothing is there at all).


Things I have tried so far:

  • Passing all arguments ByVal: Did not make a difference
  • Using global shared variables: This did work, but I don't want to rely on them if all I do is pass a single array to a single function. This also introduces state into the code which has to be managed, especially when reusing the function
  • Accessing the functions by full qualifiers: Did not make a difference. The functions are found and executed correctly, but the argument variables are empty, therefore the functions fail later on when doing the calculations.

My question is: How can I pass arrays from UserForms to Modules (not vice versa) without relying on global variables and without losing the array content?

This question may be related to this question about passing a String from Form to Module, but the accepted answer does not help in my case (using global variables).

3
  • Where is the code for your base module? Commented Feb 16, 2016 at 16:37
  • Show us the relevant pieces of code please. Commented Feb 16, 2016 at 16:48
  • You can see my answer (passing argument array from userform to a module with 2 methods (function or sub) here stackoverflow.com/questions/35211641/… Commented Feb 16, 2016 at 20:37

1 Answer 1

0

When adding the code as requested in the comments, I stumbled upon that fact that I could print the content of the array, but it would not show anything in the debugger and the size would be 0.

The size issue was because I used Len(array) instead of Application.CountA(array) and I had a leftover On error resume next from earlier still in the code, which meant that no error was raised and size was always set to zero... This was the reason for the strange behaviour.

Sign up to request clarification or add additional context in comments.

1 Comment

len(join(array)) would work i think. If the array is not 0 size, ubound(array) works too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.