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.
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 have created a ComboBox to do a Google-style search of Cities. I have my ComboBox on sheet 1, that is linked to a dynamic range of cities on sheet 2. The ComboBox output cell is what changes the dynamic range.
Here’s the problem I’m having. I start typing “sea” and I get two results in my drop down list-seaport and Seattle. So far so good. Then I use the down arrow to Select Seattle and the program crashes and restarts. I believe it is because when I hit down one time, seaport is selected and this narrows down my range to just the one option and Seattle disappears.
Is there any way around this? Either disabling the use of the keyboard down arrows for selections or preventing the output to the linked cell until a selection is finalized?
This the code for my combobox on Sheet1
Private Sub TempCombo_Change()
TempCombo.ListFillRange = "Cities"
Me.TempCombo.DropDown
End Sub
The named range "Cities" is defined by formula on sheet 2
='City Data'!$L$2:INDEX('City Data'!$L$2:$L$570,MAX('City Data'!$K$2:$K565,1))
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".
I need to copy a set of rows from one tab to another tab of the same Excel document by just clicking a button.
Also, can I also get information on how can I copy a set of rows that are hidden and paste it in the same tab without copying the "hidden" format?
If 'Copystart' is your original rows, and 'Copyend' is where you want to paste them, then using named ranges:
Sub Copybutton_Click()
Range("Copyend").value = Range("Copystart").value
Range("Copyend").visible = True
End Sub
If you have multiple named ranges with the same name, then add [Sheetname]. in front of the range, where Sheetname is the name of the sheet that the named range is in that you want to reference.
There are no native functions in Excel that will allow you to do this. You will need to write a macro and assign that to a button control (which you can drop onto your worksheet by using the Control Toolbox toolbar - View > Toolbars > Control Toolbox).
You would usually then assign the macro to that button by double-clicking the button (while it's still in Design View) and calling your macro in the newly-generated `CommandButton_Click` event. As Lance says, named ranges would be the easiest to work with.
To answer the last part of your question, programmatically copying a range doesn't copy the formatting or formula as well. It only takes the value of the cell. So regardless of whether your source range is hidden, the destination will not need to have its `visible` property explicitly set - the hidden attribute is ignored when copying.