Excel ActiveX ComboBox Displays Previous Value Issue - excel

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

Related

Excel VBA textbox to populate combobox

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

Excel ActiveX Combobox shows previous selection when losing focus

I have this code which fills a combobox on Sheet1 with the Name column of Table1 on Sheet2.
Public Sub Worksheet_Activate()
Me.ComboBox1.List = Worksheets("Sheet2").ListObjects("Table1")_
.ListColumns("Name").DataBodyRange.Value
End Sub
Works fine but it has a weird effect when I click off the combobox onto the sheet. The selected entry in the box quickly flashes to the previous entry. For example, the currently selected item is "b" and then I select "c". If I click on the worksheet the entry in the box quickly flashes to "b" before going back to "c".
I've put this code alone in a new file and I still get the same effect. Has anyone else seen this?
Edit regarding reason for Public Sub:
Forgot to include the Workbook_Open code so that Sheet1 is considered Activated when you open the Workbook. But it doesn't matter if I keep that code or not, I still see the effect.
Private Sub Workbook_Open()
Call ActiveSheet.Worksheet_Activate
End Sub
Adding a LostFocus event with code that selects a cell on your worksheet should cause the flicker not to happen when you select a cell after changing the ComboBox's value.
Like the following:
Private Sub ComboBox1_LostFocus()
ActiveSheet.Range("A1").select
End Sub

ComboBox _Click and _Change events triggering on ListFillRange edit? (Excel VBA)

Using Excel 2010 here. Starting with a blank sheet, I create an ActiveX combo box.
1) Open the properties window and set:
ListFillRange = "A1:A3"
2) Manually populate each cell in range A1:A3 with the words "one", "two" and "three", respectively (these strings are arbitrary).
3) Click the combo box drop-down arrow to check that the values appear in the list.
4) Enable Design Mode and double-click the combo box to open the VBA editor, then paste the following code (which assumes the combo box name is is "ComboBox1"):
Private Sub ComboBox1_change()
MsgBox "Change event"
End Sub
Private Sub ComboBox1_click()
MsgBox "Click event"
End Sub
5) Return to the sheet containing the combo box and disable Design Mode.
6) Click the combo box drop button and select the top item ("one") from the list. Immediately after you release the mouse button you should get two MsgBox windows, one for the _click event and another for the _change event, as instructed by the above Subs.
7) With "one" still selected, double-click in any of the cells in ListFillRange (so, for example, cell "A2")
Result: Step 7 triggers both the _click and _change events! This happens whenever a value in the ListFillRange range is edited AND the string in the combo box exactly matches at least one of the values in ListFillRange.
Any idea why this could be? Is it a bug? Thanks in advance!
I don't use ListFillRange for ActiveX controls. You should probably avoid using those. If the data is static then try populating it using AddItem through code.

excel vba: show message on dropdown list selection change if changed more than once?

I am trying to create a piece of vba code which will generate a message to the user when a user selects a value from a dropdown list/validation list in excel.
So far my script does this fine. However, I want the user to be able to select their first value from the dropdown list without getting a message, and then on the second, third or fourth time etc, if they should change their selection in the dropdown list I want the message to display.
Can someone please show me a way of doing this? Thanks in advance
'Check number of times a user has changed their selection
Dim rM As Range
Set rM = Range("M" & ActiveCell.Row).Cells.SpecialCells(xlCellTypeAllValidation)
If Intersect(Target, rM) Is Nothing Then
Else
MsgBox "changed"
End If
Add ..
Private BooRangeSelected as Boolean
... at the very top of the VBA code.
Set this value to False in the Initialize event.
Set this value to True when the dropdown list is updated.
If the value is already True when the dropdown list is selected then issue the message.
I'm assuming this macro is attached to a Workbook rather than a Form stored in "personal.xlsb". In Excel / view code double click the "ThisWorkbook" icon (see below
Private Sub Workbook_open()
Private BooRangeSelected as Boolean
BooRangeSelected = False
End Sub
This sets the variable to False once the Worksheet is opened. You can then set this to True once the dropdown has been selected
BooRangeSelected = True

Pass UserForm textbox value to cell

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.

Resources