Excel VBA ListBox (userform) - Multiple Selection Problem with fmMultiSelectSingle - excel

I actually just created my own account here because I was not able to find an answer to the problem I currently have with Excel VBA..
I am creating a Userform with a ListBox, which is being populated via an multi-dimensional array. In certain cases, the ListBox is also repopulated after clicking on the ListBox, for example in a new row of that List (which automatically leads to a click-event).
Referring to the headline of my question, I discovered that when clicking on a new row of the list, both the previous and the selected row are underlined (i.e. "clicked", even though the MultiSelect option of the ListBox is set to "0-fmMultiSelectSingle"). If I request the .ListIndex Value, it just shows me the value of the previous row (not of the now new selected row).
The biggest problem though is the repopulation of the ListBox, which only (!)does not seem to work when clicking (and creating this multiselection) in a new row of the list.
I provided an example code of my situation. Naturally, my code is much more complex so I tried to shorten it up to the main aspects. I would love to send you guys a screenshot of my userform, but the data is sensible (I hope you understand my problem anyways).
The Question: Is there a solution or sufficient workaround to populate the ListBox in a situation, where -by a Click-Event-, you choose a new row in the ListBox?
Thank you in advance for your kind help. I have been trying and searching for quite some time now; maybe somebody has faced a similar problem before!
-Graf_Rasen
Sub ListBox_Click()
intListIndex = Userform.ListBox.ListIndex
'When evaluating intListIndex in combination with clicking in a new row of 'the ListBox (which executes "Sub ListBox_Click"), intListIndex merely returns the value of the previously selected ListIndex
'defaults ListIndex, needs to be done for repopulation
Userform.ListBox.ListIndex = -1
'This does not work when changing into a new row in the ListBox
Userform.ListBox.List() = arr_DataListBox
'Redefines ListIndex
Userform.ListBox.ListIndex = intListIndex
End Sub

You cannot click in a "new" row in a listbox. It may look like you are selecting a new row, but really the event that fires is just the fact that you clicked the listbox. Hence the ListIndex value still shows the previous selection's index (which is actually still the current selection.

Related

ComboBox values disappears

I have a sheet in excel with some comoboxes (I have three). The purpose of these are just for someone to be able to choose three different values (i.e. one value in every comobox) and then I have made some VBA code which retrieves some data and plots some graphs.
However, the problem is that I want the comoboxes to have a specific value every time someone either open the sheets or runs the daily update (the sheet is in a big excel file with multiple sheets which is updated on a daily basis). I have made the VBA in such a way that it remembers whatever you just pressed before, i.e. if you choose X in combobox1, Y in combobox2 and so on it remembers this. This is done by I first make a variable which I set to be equal to the value of ComboBox1, i.e. X = ComboBox1.Value and then just before the sub ends I set ComboBox1.Value = X. This works perfectly.
However, for some reason, I am not able to set some standard value for every combobox when opening the excel file for an example, i.e. by making a Workbooks_Open Sub in ThisWorkbook and manually setting the combobox values, i.e. ComboBox1.Value = "something". This is since if I go to the sheet where the the ComboBoxes are, and then afterwards go to another sheet, and then back to the combobox sheet, then the ComboBox values have disappeared.
This do not happen when I run the sub where the data is retrieved. Therefore, I thought I could just run some sub (when the excel file is updated) which manually sets the values of these combobox to something specific I choose. But again, when I go to another sheet and then back to the combobox sheet the combobox values have disappeared again. I don't understand why.
Any help to solve this is very much appreciated. Thanks.

How to run code when item is added to list box

I have built a userform where a customer can pick items from a list of articles (listbox 1) to add them to their shopping chart (listbox 2) by doubleclicking the item in listbox 1.
I want code to run each time an item is added to listbox 2.
This very simple examplewhich I tried on the "Change", "Enter" and "AfterUpdate" event works probably as designed, but not as I need it to:
Private Sub Basket_Change()
MsgBox "Change!"
End Sub
Nothing happens when an item is added, but when I manually select an item in listbox 2, the message appears.
I seem to recall that the change event is not triggered for textboxes when the change is not entered manually. This seems to be the same with listboxes.
I could write the line from listbox1 to a worksheet in the background at the same time I write it to listbox2, and start the code (some calculations that write values to other boxes in the same userform) from there, but I feel I should try a more direct alternative, if there is one, before I go that way.
Thanks for any hint in the right direction!

How to populate ComboBoxes from ListBox selection?

So I have a UserForm that has a ListBox and ComboBoxes.
I also have a button above the ListBox that when clicked, the data from the spreadsheet will be displayed.
If I clicked the value from the ListBox, it will be automatically displayed on the ComboBoxes below per column.
Say the value from the Schema column from the ListBox says "ABC", "ABC" will also be displayed on the ComboBox below it. It is the same with the other ComboBoxes.
Is there a possible way to do it?
I've been looking for answers but none of the methods have worked on me and I couldn't find any similar problems. Please help :(
You need to create a sub on Click event and then select one by one the columns
Private Sub <ListBox>_Click()
<ComboBox>.Value = <Userform>.<ListBox>.Column(0)
End Sub
Replace the < name > with the actual name of your objects
But as PEH said, I see no reason why this case would be a ComboBox, you may want to consider changing it for a TextBox

Cell connected by controlsource keeps losing its formula

There are two cells in my spreadsheet, one contains input value only (let's say cell_1) and another one (cell_2) has formula referencing to the former one (=cell_1). For convenience, I also create a Userform with a Textbox whose controlsource property is linked to cell_2.
If there is no UserForm, everything works just fine. But with UserForm (shows with vbModeless) and the value in cell_1 changes (no matter it is changed via manual or another VBA subs), the formula in cell_2 will be overwritten by the value of cell_1 and the value in TextBox stops updating.
Has anyone encountered this strange thing?
ps: I've created a simple example, you can download it here
http://wikisend.com/download/192680/Control_Source_Bug_Test.xlsm
[update] 2014/11/29
Although the root cause is still unknown, I found that rowsource property of ListBox works perfectly; the formula of the cell connected by rowsource won't be affected. With proper setting it will look just like TextBox and one can use it as a replacement!
i think, you should have added your reset formula almpst everywhere.
Range("C7").Formula = "=C4"
added to the spinbuttons and also into userform textboxes.
here is same example sheet which I corrected my way and I think it works. Test and tell me if I am right.

Dynamically Populate Listbox - Exclude Empty cells

I am creating a form in excel (not a userform) and I am populating the listbox using cells. However, these cells are sometimes A1:10 and sometimes they are A1:A4. Is there a way to dynamically change what is shown in the listbox?
Right now, when I use A1:10 and there are only 4 cells populated, I get the list of 4 populated cells followed by 6 blank entries. I'd like to get rid of the 6 blanks when there are only 4.
Unfortunately, this is more of a workaround than a solution. However, it may be able to do what you need it to do. I've been hitting the same wall you are with trying to make ranges dynamic.
Without seeing some code to know exactly what you're doing, try something like this.
Private Sub ListBox1()
x = 1
'Add items to listbox until you reach an empty cell.
Do while Cells(x,1) <> ""
ListBox1.AddItem Cells(x,1)
Loop
I'm not very familiar with listboxes outside of userforms but this should do approximately what you want to do.
Edit your original post with your code so we can try and get a better understanding of what you've tried and what you're trying to do.
You can create a named range using a dynamic formula. Either of the below formulas will work.
=A1:INDEX($A$1:$A$10,MATCH("",$A$1:$A$10,0)-1)
=OFFSET($J$7,0,0,MATCH("",$J$7:$J$32,0)-1)
To create the named range, click ctrl+F3 then click new, insert one of the two options above into the "refers to:" section, then label the new range whatever you would like. in the "row source" section of the listbox simply type in the name you selected for your new named range.

Resources