Data not visible in ComboBox drop down - excel

When I run this code, my ComboBoxes are populated, but do not display the data
I created a single ComboBox userform, and it worked fine. When i added additional Textboxes, it stopped displaying the data, even though it was populated.
Private Sub UserForm_Initialize()
For i = 1 To 10
cboRaceNum.AddItem i
cboHorseNum.AddItem i
Next i
End Sub
I can drop down the box, not see the data, but can select a blank line, and the selected data appears in the ComboBox

Seems like somehow your data is aligned from the Right. Below code should work.
Try :
Private Sub UserForm_Initialize()
cboRaceNum.TextAlign = fmTextAlignLeft
cboHorseNum.TextAlign = fmTextAlignLeft
For i = 1 To 10
cboRaceNum.AddItem i
cboHorseNum.AddItem i
Next i
End Sub

Related

Combobox.dropdown in Excel shows outside the userform

I have created a userform and included a combobox populated with numbers 1 to x in userform.initialize and then added .setfocus and .dropdown commands. For some strange reason the combobox (with dropdown) appears on my lefthand screen outside the userform. The combobox also appears on the userform, but without the dropdown displayed. The screen combobox is active and if I click a number in the dropdown list the code accepts this input.
I have tried deleting the combobox and inserting another one with a different name, but the behaviour persists. If I remove the .dropdown command, the combobox appears correctly on the userform without the screen copy and I can click the userform box and display and use the dropdown list.
On reading an unrelated post, I tried adding .visible=false, followed by .visible=true before the .dropdown command, but that didn't stop the behaviour.
I have tried exporting, deleting and re-importing the userform, but the behaviour persists.
The code I am using (in userform_initialize sub) is:
With cbxGroup 'combobox
.Clear
For i = 1 To PlayGroups
.AddItem i
Next
.ListIndex = -1
.Visible = False
.Visible = True
.SetFocus
.DropDown
End With
Has anyone come across such behaviour before and can explain what has happened, and maybe how to fix it. I can just recreate the userform, but it seems a lot of unnecessary work. I am using Office 365
I can confirm the behaviour.
I strongly assume that this is related to the fact that you are using the Initialize-Trigger. When this trigger is executed, the form is still in the creation-process and some internal properties (eg the exact screen position) are likely not calculated. As a consequence, when calling DropDown, the Combobox is painted on the screen but not at the right position.
You can keep the code to fill the combobox in the Initialize-Trigger, but you should move the SetFocus and DropDown-commands to the Activate-Trigger
Private Sub UserForm_Initialize()
fillCombo
End Sub
Private Sub UserForm_Activate()
showCombo
End Sub
Sub fillCombo()
With Me.cbxGroup
.Clear
Dim i
For i = 1 To PlayGroups
.AddItem i
Next
.ListIndex = -1
End With
End Sub
Sub showCombo()
With Me.cbxGroup
.SetFocus
.DropDown
End With
End Sub

Clearing a list box and any selected items in list

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

Userform listbox click code does not trigger

I created a userform in Excel 2016 with two ListBoxes, using the Tools menu. I double clicked them to create subs and inserted code to check whenever one is selected.
Here is the code:
Private sub SaleType_Click ()
If SaleType.Value ="Core" then
'make sale label visible
QTDV.visible =true
' show core option btn
Core.Visible = true
End if
End sub
When I have a ListBox created from the toolbox this works, but every other time the form is run the saletype ListBox will be value null and this is a problem because I have a check to make sure the ListBox is not empty. Code follows:
If saletype = "" then
Dim msg as string Msg = " please select sale type"
Msgbox msg, and vbcritical
End if
If the ListBox presents value null it will not see it as empty and skip the check if I try saletype = null it still skips it.
I searched and it seems creating ListBoxes on the tool box is weird because Excel does not know what kind of control it is. I opted for creating the ListBoxes in VBA.
Private sub userform_initialize()
Dim saletype as msforms.Listbox
Set saletype = me.Controls.Add("Forms.ListBox.1", "SaleType")
But when running the form and selecting any option on the ListBox the SaleType_Click sub does not trigger.
If you want to implement event handling (like SaleType_Click) you need to declare the object with the WithEvents keyword:
Dim WithEvents saletype as msforms.Listbox
And if a variable/property is not set then its value doesn't exist, so instead of empty string ("") you need to validate for NULL - the IsNull function can do that (= NULL doesn't work as = only works with values):
If IsNull (saletype.Value) then
I just had a problem with my listbox_Click not being fired "every other time". Maybe it was when the same selection was desired twice in a row. Anyway, put this in the sheet code for the sheet that is "Show"ing the userform:
userformXXX.listboxYYY.ListIndex = -1
userformXXX.Show
This doesn't work if it is in the userform code.

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

Changing label captions based on a combobox index?

I have a userform with a combobox that is populated with a list of client names that are stored in a hidden sheet. I am trying to make it so the user can select a name and click a button that brings up that client's profile (name address, phone number etc).
I am trying to get it to work for just the name before I do the other fields. When I view the profile the name label (which I named name_) is blank. ClientBox is the name of the combobox with the names which is on userform2, and ClientProfile is the name of the userform that has the name_ label.
The last 2 lines commented out are what I tried before I tried this method. Neither method works.
Sub ProfilePopulator()
Dim index As Integer, str As String
Application.ScreenUpdating = False
Sheets("Clients").Visible = True
Sheets("Clients").Select
Range("A1").Select
index = UserForm2.ClientBox.ListIndex
str = Cells(index + 1, 1)
ClientProfile.Name_.Caption = str
'ActiveCell.Offset(index, 0).Select
'ClientProfile.Name_.Caption = ActiveCell
End Sub
The problem wasn't actually in this part of the code (oops). It was in the code for the button that opens the ClientProfile userform. Before I had:
Private Sub CommandButton2_Click()
UserForm2.Hide
ClientProfile.Show
Call ProfilePopulator
End Sub
ProfilePopulator has to run before ClientProfile is shown, so now it looks like this and it works:
Private Sub CommandButton2_Click()
UserForm2.Hide
Call ProfilePopulator
ClientProfile.Show
End Sub

Resources