janus Mack
janus Mack

Reputation: 53

VBA: call Forms in sub

I have a big problem with getting called a UserForm from a sub.

This is a part of my code:

Sub TestForArray(ObjectName,FormName As String)       

Forms(FormName).Controls(ObjectName).List = NewArr

End sub

when I call this sub in a Private Sub, like this

Private Sub UserForm_Initialize()

Call TestForArray("Form1", "ComboBox1")

End Sub

i get the error; "Sub or Function not defined" and higlights the word Forms

what am I doing wrong?

Upvotes: 0

Views: 935

Answers (1)

Axel Richter
Axel Richter

Reputation: 61860

If the goal is to have one Sub that can be called from different UserForms to initialize the UserForm's Controls, you can give the UserForm itself as a parameter.

Example:

Sub TestForArray(oForm As UserForm, sObjectName As String)
  aNewArr = [{1,2,3,4,5,6}]
  oForm.Controls(sObjectName).List = aNewArr
End Sub

and:

Private Sub UserForm_Initialize()
 Call TestForArray(Me, "ComboBox1")
End Sub

Greetings

Axel

Upvotes: 1

Related Questions