I'm after some help that has had me stumped for a while. Excuse the long explanation.
I have a combobox that populates from a range when the userform initializes. When I type into the combobox the preemptive text appears as it is supposed to. I then have a Change event for a textbox which populates based on what gets typed into the combobox. That part all works fine (I got that code from another site).
I have two ways to enter the data into the combobox, one is by typing, and the other is when the text in another text box changes, it also populates the combobox. I do this by "combobox1 = textbox1.value". Now here is the part when I am stumped. When using the combobox1 = textbox1 method, it doesn't work properly (or more so, how I want it to work). It enters the text that is contained in textbox1, but it doesn't show the full line of preemptive text like how it does when typing in the combobox, nor does it then populate the textbox that changes when the combobox changes. If I then click in the combo box and hit the space bar the change event fires and the rest of the preemptive text from the range appears. I tried putting a space " " command at the end combobox1 = textbox1 & " " in the hope it would think there is more text to come but that didn't work. Is there any way to get VBA do do this, or am I asking too much of it?
Hopefully this makes sense.
cheers
paul
seems like ComboBox AutoCompletion feature is triggered by UI input only
you can work around it as follows:
Private Sub TextBox1_Change()
Dim iList As Long
With Me.ComboBox1
For iList = 0 To .ListCount - 1
If Left(.List(iList), Len(Me.TextBox1.Value)) = Me.TextBox1.Value Then
.ListIndex = iList ' if any combobox1 value matches textbox1 value then select it
Exit Sub
End If
Next
.ListIndex = -1 ' if no combobox1 value matches textbox1 value then "deselect" combobox1
End With
End Sub
Related
I have an Excel VBA application that goes through a sheet which contains product orders on a sheet in a workbook and searches the worksheet for orders that match various criteria which is populates in a search worksheet. The contents from this worksheet are then displayed in list box. There are several user forms that allow the user to select an order and then manipulate the order. After doing this the order manipulated may not meet the search criteria so I want to clear the list box contents and the selected row in the list box. I have tried numerous things but nothing seems to work. My latest is something list this:
Private Sub ClearListBox()
UserForm5.lstOpenO.ListIndex = -1
UserForm5.lstOpenO.RowSource = ""
End Sub
But I have tried setting the UserForm5.lstOpenO.Selected to false for all the rows. I have tried clearing the search worksheet and then displaying that which should only show the headers on the columns but the highlight in the selected row remains.
Any help would be greatly appreciated
Bruce
First of all you should not use the default instance of the userform in your code. This will lead to ambigous behaviour
I'd suggest
Private Sub ClearSelectionInListBox()
Dim varItm as variant
With lstOpen0
varItm = .MultiSelect
.MultiSelect = 0
.MultiSelect = 1
.MultiSelect = varItm
End With
End Sub
This will clear the selection within the listbox.
It is not clear if you really want to clear the contents. Because if you want then it does not make sense to think about clearing the selection.
If you want to clear the listbox then it is not neccessary to clear the selection first.
Private ClearListBox()
With lstOpen0
.RowSource = ""
.Clear
End With
End Sub
But after that you need to fill the listbox again.
Further reading
VBA userform
Userform.Show
ListBox
I have a sheet with a bunch of ComboBoxes(form control) and I want to detect when a user changes any one of them and write text in a cell. Using Worksheet_Change on the target cells doesn't work. I have tried a bunch of things that don't work. I'm not sure what needs to be in the private sub line or the if statement.
Private Sub DropDowns_DropButtonClick()
If ActiveSheet.DropDowns.Value > 1 Then
Cells(13, 5).Font.Bold = True
Cells(13, 5).Font.Color = vbRed
Cells(13, 5).Value = "!!! Selections have been changed. !!!"
End If
End Sub
I have tried
ComboBox_AfterUpdate()
ComboBox_Change()
DropDowns_AfterUpdate()
DropsDowns_Change()
and anything else I could find. I've also tried a few different things in the if statement with no luck.
I appreciate any help.
Chris
If I'm reading you correctly, you're comboboxes are in a userform. If I'm correct, simply open your userform in 'Visual Basic' and double click on the relavant combobox. This will open the code pane and create an empty Private Sub routine called 'Private Sub <Combobox Name> ()'.
Enter your code to place your data in the sheet (or whatever else you want) into the subroutine and Bob should be your uncle.
Apologies in advance if there's something I've missed.
RannochRob
Edit...
OK, my mistake, it's a form control.
My first comment is that it's easier to use an activex control if you can... however, with a form control, should (a) Use the cell link box in the 'Format Control' drop down ('Control' tab) to place the result in a cell... however, that result will not be the content of the box but an integer equal to the position of the selected entry on the list of entries in the combobox. You then need to (b) assign a macro to the combobox which will pick up the result and use it to get the required information from the range containing the list of entries. Like I say, much easier with an activex control...
RannochRob
Here's how you can do it by assigning a macro to the combobox (right click on the combobox>assign macro) as #BigBen mentioned in the comments section:
Option Explicit
Sub DropDown1_Change()
Dim sht As Worksheet
Dim dd As DropDown
Set sht = ThisWorkbook.Worksheets("Name of your Worksheet") 'Name of the worksheet in which the combobox is located
Set dd = sht.DropDowns("Drop Down 1") 'name of your combobox
sht.Range("G1").Value = "The selected value is: " & dd.List(dd.Value) 'dd.value returns the index of the selected value
End Sub
You can use the same code for each one of your comboboxes.
For demonstration purposes i have used the following set-up:
You can easily modify the code to best fit your needs.
Issue with Embedded ActiveX ComboBox form on a spreadsheet where:
Upon changing the value in the ComboBox, the value changes properly
However, when a cell/shape is selected on the sheet, the ComboBox value reverts back to the previous value for a split second before going back to the new value
Problem because: If a button for a macro is pressed after changing the ComboBox value, the old ComboBox value is displayed while the macro is running, rather than the new value
Question: Is there a way to force this event (reverting to old value before going displaying new value) programmatically?
I've tried using the following in the Change event for the ComboBox, as well as within the macro that is called by another shape on the sheet:
Calculating the worksheet
Selecting/Activating a cell
ScreenUpdating = false, ScreenUpdating = true
Did a bit more searching and have found this question Excel ActiveX Combobox shows previous selection when losing focus with the same issue
Added a LostFocus event to the ComboBox that had a line that selected a cell. This removed the "flicker" to the previous value when another cell on the sheet was selected, and also caused the ComboBox value to "flicker" back to the new value after "flickering" to the old value when a command button was clicked after changing the ComboBox's value...
Therefore this solved my issue (mostly -- was unable to prevent the "flicker" from happening upon running another macro via command button, but at least the value does not remain stuck at the old value while the other macro runs)
Idea came from this thread: What event is triggered when user selects value from drop down ComboBox (ActiveX)?
Will start from here:
I created a comboBox with various fruits as items inside.
I clicked on pear. Then pineapple. then clicked on cell A1, firing the lost focus event.
Private Sub ComboBox1_LostFocus()
Debug.Print ComboBox1.Value
Debug.Print ComboBox1.Value
Debug.Print ComboBox1.Value
End Sub
This was text in my immediate window:
pineapple
pineapple
pineapple
This makes me think that it is a rendering issue, not a value change, though maybe it is happening so fast that my debug.print doesn't catch it.
Interestingly if you make the calls from inside VBA, the flash does not occur:
Sub Main()
ComboBox1.Value = "mango"
Range("A1").Select
End Sub
Nor if you just run
ComboBox1.Value = "mango"
then click on the worksheet and select a cell
I have one solution that is sort of a work around.
I inserted an ActiveX Label to the worksheet (on the same location as the ComboBox) and set the labels visible state to False.
Everytime the drop-down box closes, the focus is set on the invisible label
First a global variable.
Public DropDownBegin As Boolean
Then the sub-routine
Private Sub ComboBox1_DropButtonClick()
DropDownBegin = Not DropDownBegin
If Not DropDownBegin Then
ActiveSheet.Shapes("Label1").OLEFormat.Object.TopLeftCell.Select
End If
End Sub
I have a combobox on my userform that should be changed by the vba codes, but I dont want any user to be able to type values, only the vb should have this power, the user should only be able to select values.
Any ideas?
As suggested by Chris, add the items and then set a default value for the box, however, don't set locked, by selecting the combobox in this box select the yellow option.
Just change the "Locked" property to True. It will prevent the user from changing the value: however VBA will still be able to. For example, the following code locks the ComboBox; then adds two values and selects one:
Private Sub UserForm_Initialize()
With ComboBox1
.Locked = True ' Prevents the user changing anything
.AddItem "Hello"
.AddItem "World"
.Value = "Hello" ' Sets the value with VBA
End with
End Sub
Community, I'm moderately new to excel. I have a textbox called Box_One. This has been set up on my userform. All I want to do is have a cell value constantly equal whatever value is in the textbox.
I tried the following, but not quite sure how to implement properly
Home.Range("A2").Value = Box_One.Value
how about using the Change event of your text box to run the code you want? Something like
Private Sub TextBox1_Change()
Range("BU1").value = TextBox1.value ' specify the destination sheet and cell here
End Sub
I tested this real quick and it worked.