Excel VB Listbox losing Value and Text Property - excel

I'm trying to solve this problem along 2 days but I've not found the solution.
I have a lot of listboxes on my excel and each of these listboxes are filled with different data, also I use these listboxes to change some filters at a pivot table using a VB code.
The problem is that after some usage these listboxes stop to work, they stop to get the values that are selected.
I've discovered that two properties, 'Value' and 'Text' has been disappeared after the problem happened. To use the listboxes again I need to close excel and open it again, also if I try to save the workbook, it stop to work.
I've tried to bring some images of it, but I got no reputation yet.
Who can help me?
Updating:
Here is the images:
And here is the code behind the listboxes (all listboxes has a similar code):
If ListFaturaFatura.List(ListFaturaFatura.ListIndex) = "Total" Or _
ListFaturaFatura.List(ListFaturaFatura.ListIndex) = "" Then
Sheets("Fatura_Data").PivotTables("DinamicaFatura1").PivotFields("FATURA").ClearAllFilters
Else
Sheets("Fatura_Data").PivotTables("DinamicaFatura1").PivotFields("FATURA").CurrentPage = _
ListFaturaFatura.List(ListFaturaFatura.ListIndex)
End If
Also, I think that is an excel problem, because when it happens all workbooks are affected with the same problem.

I saw that one of the listboxes was filled with a named range, I just removed the named range and filled the listbox manually, this seems to work fine and didn't give me more problems.

Related

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

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.

Why does Excel freeze after Resizing a Table/ListObject which is used as a RowSource for a ListBox?

The following example is a realiable way to crash excel 2010.
Insert Userform with ListBox
Insert a Table into the same Workbook
Set the 'RowSource' Property of the ListBox to 'TableName'
Resize the Table
The resizing causes a freeze, excel will then try to restart but usually doesn't manage to do it. The only way to get out is to x-out the restart menu or restart your computer. You will certainly loose all unsaved progress so please don't do this when working on something.
Why does this happen?
How to avoid it/ solve it?
Edit 1: It comes even better! This question states that there is no other way than RowSource to get titles for your MultiColumn Listbox (unless you do it yourself with lables). So either leave a potentially lethal bug in the code or do all headers with your own code..
If the resizing table causes rowsource to crash, then avoid it before resizing it.
listbox1.rowsource = ""
do your things
listbox1.rowsource = "your range"

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.

Excel VBA forcing selection of objects after run

I've researched this to death and feel like I'm the only person it's ever happened to.
I have some VBA that:
Creates a copy of 3 sheets to a new wb
In the new wb converts to values, deletes objects (shapes and controls) and all but 3 ranges
Opens an existing third file and sets the contents of three ranges in that to match the new wb
Closes the existing file (saved)
Closes the new wb (saved)
Gives a message box saying complete
At the end of all this, something weird happens with the state of the windows. The selected cell does not appear selected. If I try and click a control afterwards, it selects the object (hence users could drag them). It shouldn't and this is the big problem.
I've tried selecting a cell through code, it throws an error. I had limited success by forcing drawing mode off using Call CommandBars("Drawing").Controls("Select Objects").Execute and activating a specific sheet & selecting a cell. However, even then if I even click on a few cells afterwards, the next time I select a control it will select it as an object rather than click the thing.
I have no idea why and can't find anyone who's seen this before.
Any ideas on what I can do?
Thanks,
Basil
I didn't figure it out entirely, but I did find a fix. Hopefully it works for anyone else who finds this problem.
At the end of the code I added this:
ActiveSheet.Shapes.Range("ctrlExportPrices").Select
ActiveSheet.Range("B8").Select
So it forced a control on the sheet to be selected, and then a cell.
The next time I select the control manually, it clicks it rather than selecting the drawing object.

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