Variable in Textbox Event

helivw

New Member
Joined
Oct 6, 2018
Messages
26
I make a selection in a Userform.Listbox with data coming form the Textbox in a Userform
Can I pass a variable to the Textbox Sub ..._Change event
ie. Sub TextBx_Change(ByRef ....)
I don't want to use a Public variable
Thanks
 
The userform Initialize event fires before showing the userform so that's a good place to populate the array.

Thanks for the quick reply.
I not quite sure how to proceed so I give you an example what I'm doing
************
Sub Example()
Dim Arr() as variant, Arr1()

Arr1=Range("F1:J200")
Arr=Range("A1:E200")
Userform1.Listbox1.List = Arr
Userform1.Show

end sub
************
Now i have a textbox where each time the user enters a character my listbox is reduced in size
In case of mistake of user I need again te full array in my listbox
Sub tb_Char_Change()
...
if there are no characters in tb_Char then the Listbox has 200 items
If the user enters 1 character the listbox has only 100 items left
if the user enters a second character the listbox has only 20 items left
....
if the user enters a wrong character the listbox restart with the complete 200 items again

So I need to transfer Arr from the normal procedure sub Example() to the userform where I can use Arr at several occasions
because in the Subs of the userform I mix-up Arr1 and Arr

Thanks a lot for helping me and sorry for the inconvenience
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
The userform Initialize event fires before showing the userform so that's a good place to populate the array.
Thanks for the quick reply.
I not quite sure how to proceed so I give you an example what I'm doing
************
Sub Example()
Dim Arr() as variant, Arr1()

Arr1=Range("F1:J200")
Arr=Range("A1:E200")
Userform1.Listbox1.List = Arr
Userform1.Show

end sub
************
Now i have a textbox where each time the user enters a character my listbox is reduced in size
In case of mistake of user I need again te full array in my listbox
Sub tb_Char_Change()
...
if there are no characters in tb_Char then the Listbox has 200 items
If the user enters 1 character the listbox has only 100 items left
if the user enters a second character the listbox has only 20 items left
....
if the user enters a wrong character the listbox restart with the complete 200 items again

So I need to transfer Arr from the normal procedure sub Example() to the userform where I can use Arr at several occasions
because in the Subs of the userform I mix-up Arr1 and Arr

Thanks a lot for helping me and sorry for the inconvenience
 
Upvote 0
Store the array in a userform module level temporary array upon activating the userform ...Something along these lines maybe:

Code:
Private vTempArr() As Variant

Private Sub UserForm_Activate()

    vTempArr = ListBox1.List

End Sub

vTempArr should now contain the same data as Arr and the data and should be available throughout the entire userform module .
 
Last edited:
Upvote 0
If you want to get really slick, put this in the userform's code module

Code:
Dim arrArray as Variant
'...

Public Sub Start(inputArray as Variant)
    arrArray = inputArray
    Me.Show
End Sub

'...

and invoke the userform with code like
Code:
Userform1.Start(Array(1, 2, 3))
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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