Excel ActiveX Combobox shows previous selection when losing focus - excel

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

Related

How do I use the FollowHyperlink worksheet event to recognize a hyperlink within a shape?

I've got two buttons on a worksheet that I've named "RemoveButton" and "AddButton". I've also added hyperlinks to both shapes and both shapes will point to the same cell once clicked. When I click both buttons, they point to cell A1 as expected, but the FollowHyperlink code does not recognize that a hyperlink has been clicked.
I wanted to use the FollowHyperlink worksheet event to recognize the shape that is clicked. I created the macro as below:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Debug.Print "Clicked!"
End Sub
When clicking on the shapes, they just point to A1 and "Clicked!" never shows in my Immediate window. However, I created a test hyperlink that is text only and when selected, "Clicked!" appears. This indicates that Excel isn't treating the buttons as hyperlinks even though they have hyperlinks added to them.
The reason for the hyperlinks on the shape is for them to run code. I could use the assign macro feature to the shape, but in doing so I wouldn't be able to add a ScreenTip to the shape. I really want the ScreenTip as this will help future users know what the button is for.
Can someone please help me understand if this is possible?
Screenshot of buttons
There is a workaround for this problem. Instead of the Worksheet_FollowHyperlink event, you can use the Worksheet_SelectionChange event.
To do this, you need a cell that is completely covered up by your button. If the button is too small to cover up a cell, you can just hide a row and a column and place the button at the intersection of the hidden row and column.
Now, we link the button with the "hidden" cell, C5 in this example:
Now the hidden cell can only be selected by clicking the button.
So if the Target in the Worksheet_SelectionChange event is the cell C5, we know that the button has been clicked.
To leave the previous selection unaffected, you can use the following code in the worksheet's code module:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Const HIDDEN_CELL_ADDESS As String = "$C$5" '<--Set hidden cell address here
Static previousSelection As Range
If Target.Address = HIDDEN_CELL_ADDESS Then
'Make sure the linked cell doesn't stay selected, otherwise the next
'click on the button may not be recognized
Application.EnableEvents = False
If Not previousSelection Is Nothing Then previousSelection.Select
If TypeName(Selection) = "Range" Then
If Selection.Address = HIDDEN_CELL_ADDESS Then Target.Offset(1).Select
End If
Application.EnableEvents = True
Call ShapeClicked
Else
Set previousSelection = Target
End If
End Sub
Sub ShapeClicked()
MsgBox "The button has been clicked"
End Sub
GWD's answer correctly solves the issue. I also found a slightly different way to work around this as well. Please see below:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("B11")) Is Nothing Then
Call dispatchLink(Target.Address)
End If
End Sub

Select named range via VBA and let user edit cell directly

I have a small issue, but I cannot find the code for it. For simplicity I made a simple example.
I made a validation userform with a modeless listbox (lsb_dataval_man). This listbox is populated with named ranges with only the names that have an error or are empty. Because there are many tabs, the user can navigate quickly to the sheets and cells which need attention and modify or populate them.
When you doubleclick a named range in the listbox, it will go to that reference and users need to enter data directly. See the simplified code for this double click below.
With the command Application.Goto ThisWorkbook.names(named_range).RefersToRange it will select the cell, but I am not able to type in the cell directly as like when you would do a select. A user needs to click the cell again or click in the formula bar and enter data. How can I change my code so after going to the cell of the named range, the user can type directly? Perhaps this is related to the modeless form which is still active?
The code:
Private Sub lsb_dataval_man_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim named_range As String
named_range = Me.lsb_dataval_man
'select the named range cell
Application.Goto ThisWorkbook.names(named_range).RefersToRange
End sub
This should work:
Private Sub lsb_dataval_man_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim named_range As String
named_range = Me.lsb_dataval_man
AppActivate ThisWorkbook.Windows(1).Caption 'switches focus from userform to workbook
ThisWorkbook.Names(named_range).RefersToRange.Select 'selects cell
End sub

Change active sheet based on selected item from a dropdown list

I have an Excel WorkBook with several Sheets. I would like to be able to select from a drop down list in the "Home" Sheet and after selection is made, automatically switch to the proper Sheet and select a specific Cell.
It appeared to be easy, but I have failed time and again to make it work.
Here is an example of what I would like to do:
The only code that I managed to have a little success with is the following:
Private Sub Worksheet_Activate()
With Sheets("Home")
If Cells(6, 3).Value = "A" Then
Sheets("A").Select
ActiveSheet.Range("B7").Select
End If
End With
End Sub
The problem with it is that it will not check for the value until the user moves to another sheet. Then when it comes back, it will check for it, and will take it to the correct one, but the user will be stuck in a loop without being able to go back to "Home". (I know that it will only work on cell C6, but I just wanted to try if it worked before changing the Range)
You need the worksheet change event, rather than activate. Try this, in the Home sheet module.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$6" Then
Application.Goto Sheets(Target.Text).Range("B7")
End If
End Sub

Excel vba - Userform - doesn't change output - focus worksheet

Trying to use excel vba userforms to automate many tasks in a spreadsheet. There is a Button1 on Sheet1 that has two commands.
sub Button1_click()
sheet1.range("a3").select
userform1.show
end sub
As an example there is a data entry worksheet (sheet2) that we want to switch to to input data values to a list.
To simplify in this example and to show my issue the userform has one button
sub CommandButton1_click()
userform1.hide ' hide the form
sheet2.activate
sheet2.range("b2").select
end sub
What I want to be able to do is use the user form button to switch to sheet2, select b2, and be able to enter data starting there immediately.
What I've been getting is a selection box on sheet2.range("b2") BUT I show color starting at sheet1!a3, then sheet1!b4,... I have entry occurring on sheet1!b2 etc.
Shows beginning and entry
Entry colors from sheet1 showing on visible sheet2 - data not appearing
Actual data is entered on sheet1 not sheet2 -
The problem seems to be (as you mention in the comments) that, at the end of Button1_Click, the focus returns to the sheet containing the button even though the ActiveSheet is now a different sheet.
For the moment (until someone comes up with a better solution) a "workaround" is to allow the Button1_Click event to finish running before showing the Form. That can be achieved by changing Button1_Click to something like:
Sub Button1_click()
Sheet1.Range("a3").Select
Application.OnTime Now(), "BypassBug"
End Sub
Sub BypassBug()
UserForm1.Show
End Sub

Excel Userform Combobox Properties Rowsource box issues?

I have a userform in Excel that works as a calculator.
In this userform I have two ComboBoxs (1 & 2)
In VBA editor, with ComboBox1 selected, In Properties, under Rowsourse I have: Sheet1!a4:a5
In Sheet1, A4 = Auckland and A5 = Christchurch
This is fine and when I run the userform there is a drop down arrow with the two options (Auckland or Christchurch).
However my problem is that when you open this workbook I have a VBA command to hide it from the users sight, leaving them only the userform to work with which is what is desired.
The issue is that if you have another workbook open then open this calculator workbook (which automatically hides itself). Then the combobox list is populated by Sheet1!a4:a5 on the other workbook that was already open, not the workbook that actually contains "Auckland" & "Christchurch" from which the userform is from.
I have tried making the Rowsource for the comboboxes more specific by putting the following in the rowsource box in properties: [book1.xlsm]sheet1!a4:a5 but this comes up with a "Invalid Property Value" error message.
I have also tried making a:
Private Sub Userform1_Initialize()
ComboBox1.Additem "Auckland"
ComboBox1.Additem "Christchurch"
End Sub
And also tried this:
Private Sub Userform1_Initialize()
ComboBox1.RowSource = Workbooks("book1.xlsm").Sheets("Sheet1").Range("a4:a5").Value
End Sub
However with both codes when it opens and runs now the comboboxes are empty and there is no list.
I think the easist solution would be to somehow put the full path (including workbook name) into the rowsource box under properties. But I must be missing something as its coming up with that error for me?
All help would be greatly appreciated.
Thanks
You are missing ' in your full path row source.
It should be like this:
Me.ComboBox1.RowSource = "'[book1.xlsm]Sheet1'!$A$4:$A$5"
I have similar question that can be found HERE.
Set the row source property of the combobox as: SheetName!$Col$Row:$Col$Row, e.g.: Location!$A$1:$A$3.
You may try adding this code on userform:
Private Sub UserForm_Initialize()
ComboBox1.list = Array("Auckland","Christchurch")
End Sub
Then set Combobox propert "MatchEntry" to "1".

Resources