View Full Version : Passing data between a main code and an UserForm
deltaquattro
11-12-2009, 09:36 AM
Hi!
I'm writing a code in VBA for Excel 2000. The code reads a long text file and loads an array of strings OpCond. I would like the user to choose one of these strings, using an UserForm. I created an User Form in the Visual Basic Editor, and I was going to edit the object code. However, I have a doubt. How can I pass the array OpCond to the UserForm? And how do I pass to the main code, the string that the user chose in the UserForm? Thanks,
Best Regards
Sergio Rossi
mdmackillop
11-12-2009, 09:47 AM
Welcome to VBAX
Set OpCond as a Public Variable, with another Variable for the response.
lucas
11-12-2009, 09:50 AM
You don't say what kind of object you want to use. This shows how to populate a listbox from an array. The provided example builds the array. You can just pass your array instead:
Private Sub UserForm_Initialize()
Dim FillMe()
Dim j As Long
FillMe = Array("yadda_1", "blah_2", _
"whoha_3", "foobar_4", _
"do-dah_5", "hohum_6")
For j = 0 To UBound(FillMe())
ListBox1.AddItem FillMe(j)
Next
ListBox1.ListIndex = 0
End Sub
If this works for you we can move on to selecting an item from the listbox.
deltaquattro
11-12-2009, 09:59 AM
Welcome to VBAX
Set OpCond as a Public Variable, with another Variable for the response.
Ciao, mdmackillop!
Thank you for the quick reply! I will try out your suggestion and let you know the results.
Best Regards
Sergio Rossi
deltaquattro
11-12-2009, 10:08 AM
You don't say what kind of object you want to use. This shows how to populate a listbox from an array. The provided example builds the array. You can just pass your array instead:
Private Sub UserForm_Initialize()
Dim FillMe()
Dim j As Long
FillMe = Array("yadda_1", "blah_2", _
"whoha_3", "foobar_4", _
"do-dah_5", "hohum_6")
For j = 0 To UBound(FillMe())
ListBox1.AddItem FillMe(j)
Next
ListBox1.ListIndex = 0
End Sub
If this works for you we can move on to selecting an item from the listbox.
Ciao Lucas!
You're right, I forgot to say that the object is a ListBox. Putting together the suggestions by you and mdmackillop, and adding some other suggestions I got, I think that in the UserFrom Object code I should write something like this:
Option Explicit
Public ChosenOpCond As String
Public OpCond() As String
Private Sub UserForm_Initialize(OpCond() As String)
Dim i As Long
'Load User Form
For i = 1 To NOpConds
ListBox1.AddItem OpCond(i)
Next
End Sub
Private Sub cmdOK_Click()
If ListBox1.ListIndex = -1 Then
MsgBox "Select a test case", vbExclamation
Else
'Save selection and close window
'the calling cod to get ChosenOpCond
ChosenOpCond = ListBox1.Text
Hide
End If
End Sub
Private Sub cmdAnnulla_Click()
'No selection done, I exit with an empty selection
ChosenOpCond = ""
Unload Me
End Sub
while in the main code I should write something like:
Option Explicit
Public ChosenOpCond As String
Public OpCond() As String
Redim OpCond(2)
OpCond(1)='foo'
OpCond(2)='bar'
UserForm1.Show
UserForm1.Show vbModal
If UserForm1.ChosenOpCond = "" Then
ThisWorkbook.ActiveSheet.Cells(1, 2).Value = "<none>"
Else
ThisWorkbook.ActiveSheet.Cells(1, 2).Value = UserForm1.ChosenOpcond
End If
Unload UserForm1
Set UserForm1 = Nothing
End Sub
Did I understand you correctly? I will try and let you know the results. Thanks,
Best Regards
Sergio Rossi
mdmackillop
11-12-2009, 10:22 AM
A very simple example
deltaquattro
11-13-2009, 09:32 AM
Thanks! That solved my problem. From your example I noticed that MyArr must be declared Public in Module1 - arrays cannot be declared Public in a object module.
Thanks,
Sergio
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.