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.
Related
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.
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.
I have made two active X combo boxes for a list of 220 sites. Before making the active x combo box i have made a sheet that searches through my data and finds the sites that match with what i am typing as i go.
I then used the name manage, refering to the formula in the first cell of the list
=Sheet1!$G$2:INDEX(Sheet1!$G$2:$G$220,COUNTIF(Sheet1!$G$2:$G$220,"?*"))
I have then writen this in the ListFillRange in the properties of my combo box.
It works fine for one, but once i had made the second one and selected the site the first one will no longer let me search through.
I have used the same formulas on both but they originate from different sheets to see if this fixed the problem however that was unsuccessful. (the boxes are on different sheets) When i click on the next sheet after using the box on the first sheet, it still shows part of it as if it is crashing.
The boxes are independent so I'm not sure what to do next as i need to add another 3 on separate sheets.
I am also using this code for each box
Private Sub ComboBox1_Change()
ComboBox1.ListFillRange = "MList"
Me.ComboBox1.DropDown
End Sub
and similar for the other button but with a different range.
There is no need to use VBA for this, the Change Events specifically, if you just want to use and fill the combo boxes with Named Ranges.
The scenario I think you try to do is:
Create Named Ranges that will be the source of your combobox:
Fill the range with your data, select the range, Right Click, Select Define Name and give the range a name. MList in your case I believe.
Create Combobox:
Goto Developer Tab, Insert in your case ActiveX ComboBox, Draw it on your sheet, right click the ComboBox, select properties, find ListFillRange in properties and enter the name of the Named Range you created in step one
Repeat for Combobox 2, with the same or a different Named Range depending on what you try to do
Leave Design Mode
Boths Comboboxes can now be used to type in what you are looking for.
If this is not what you tried to do, please try edit your question and in detail try to explain what you try to do and what you like to accomplish by doing so.
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.
ActiveX combobox objects in Excel do not behave well when their ListFillRange refers to a formula-based Named Range (Defined Name).
I think I have encountered other errors and possibly even Excel crashes thanks to this, but right now all that happens is the combobox_change() event is triggered anytime ANY cell in the workbook is changed.
I am not sure if this is really a bug, or if there is a fix, or a workaround. If it is a bug, how do I report it to the Excel people?
And finally, the real meat of my question is "How do I work around this issue best?" I would like to have some formula-based named ranges, but it seems like this won't be possible.
To reproduce this bug, do the following:
Create a new workbook. On Sheet3, create a small table 3 columns across, and several rows high.
Create a named range with this formula (or an equivalent): =OFFSET(Sheet3!$A$2:$C$36,0,0,COUNTA(Sheet3!$A:$A),COUNTA(Sheet3!$4:$4)) To do this use Input>Name>Define. Name the range something like "demoRange"
Go to Sheet1 and create a combobox, (it must be on a separate sheet). (Use the Control Toolbox menu, not the Forms menu).
Click on the Design Mode button (the blue triangle with pencil), then right click on the combo box and go to Properties.
In the properties window for the combobox, change the ListFillRange property so that it points at the named range you created in step 2 ("demoRange").
You may want to change the ColumnCount property to 3, and the ColumnWidths property to "50,50,50"
Set the linkedCell property to cell "A1" by typing A1 in the linkedCell property.
Close the properties window, and double click on the combobox to define its change() event.
Put a Debug.Assert(false) or Msgbox("demo") line in the subroutine for the new combobox's change event.
Exit design mode
important - Now select an item in the combobox. The event should trigger normally the first time. (The bug will not show if you don't do this step--something must be selected in the combobox)
Edit cells anywhere in the workbook [Edit] or any other open workbook [/edit], on any sheet and any location. Each time you edit any cell, (at least for me), the onchange event for the combo box is run.
Again, is this normal, and what is the best alternative for what I am doing? This combo box gets linked to various cells, and is supposed to be a replacement for the tiny font in the data validation dropdowns excel provides by default.
My advice is to never use ListFillRange and LinkedCell. They are just trouble. Fill your listbox with List and use the Change event to write to the cell. Somewhere, maybe the Workbook_Open event, fill the listbox
Private Sub Workbook_Open()
Sheet2.ListBox1.Clear
Sheet2.ListBox1.List = Sheet1.Range("demoRange").Value
End Sub
Then in the change event in the Sheet2 module, check that something was clicked and write it to the cell
Private Sub ListBox1_Change()
If Me.ListBox1.ListIndex >= 0 Then
Sheet2.Range("A1").Value = Me.ListBox1.Value
End If
End Sub
I have a few options available that I am aware of thus far. The best I can come up with is this:
Avoid directly using formula-based named ranges. Instead, define a subroutine that will check whether the defined range "demoRange" should be changed from what its current value is. Run this subroutine on the workbook_open and sheet3_deactivate events. If needed, prompt the user to ask if it's all right to update the named range. [edit] The macro that updates "demoRange" could probably just copy from a "demoRange_FormulaBased" named range into "demoRange" which would be static. [/edit]
This solution works well because you can keep using the linkedcell property, you don't have to use VBA to populate the comboboxes, and the named range can still be used for whatever other purposes it already had. Avoid using the onchange event to run this new subroutine, since it might end up being triggered thousands of times if a user opens the Find/Replace dialog and chooses "Replace All".