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
Related
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
I've been searching and found various links, but none seem to address the problem that I have.
I have a userform with a label, and I want the caption of the label to be whatever cell B2 is. This is what I currently have:
Private Sub Label1_Click()
UserForm1.Label1.Caption = Worksheets("Sheet1").Range("B2").Value
End Sub
My problem is that I have a Label1_Click() and the label only appears in my userform when I click. Which do I choose to make the label appear in my userform immediately as it opens?
Either:
Private Sub UserForm_Initialize()
Me.Label1.Visible = True
End Sub
Or:
Change settings of the label if you don't want it hidden at all:
Click label in editor
Under properties change Visible to True
I have a userform with multiple pages, each with textboxes that I want to make sure are numbers before I dump them back into a worksheet.
I have other normal userforms that do this:
Private Sub myTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
OnlyNumbers
End Sub
Private Sub OnlyNumbers()
If TypeName(Me.ActiveControl) = "TextBox" Then
With Me.ActiveControl
If Not IsNumeric(.Value) And .Value <> vbNullString Then
MsgBox "Sorry, only numbers allowed"
.Value = vbNullString
End If
End With
End If
End Sub
This works fine, but when I try to do something similar with this multipage userform it doesn't work.
I tried using
Me.ActiveControl.ActiveControl
but get a "Object doesn't support this property or method" run time error. This would work when the textboxes are inside a frame, but it seems like pages are not treated the same way.
Yeah, this one is tricky. The MultiPage control you can think of as the parent of the controls that sit on top of it. What you need to do is select the MultiPage control first, then select the active control from there.
It looks like:
Me.MultiPage1.Pages(Me.MultiPage1.Value).ActiveControl.Name
The Pages property allows you to select which page you want to select. You can get this by using the index of the MultiPage control (the Index starts at 1). Then you can call the ActiveControl and receive the expected control.
Hope it helps!
I'm trying to click a selection on an ActiveX ListBox and have value assigned to a TextBox then clear the ListBox. It seems straightforward but I'm
getting 'Object doesn't support this property or method' on the first line
This is what I'm using:
Private Sub ListBox1_Click()
ActiveSheet.OLEObjects("TextBox3").Object.Value = ActiveSheet.OLEObjects("ListBox1").Object.Selection
ActiveSheet.OLEObjects("ListBox1").Object.Selection = ""
End Sub
Any thoughts on how to make this word or resources to search are appreciated.
Your code would be cleaner written like this:
Private Sub ListBox1_Click()
TextBox1.Value = ListBox1.Text
ListBox1.Selected(ListBox1.ListIndex) = False
End Sub
But there is a problem: the second line doesn't work inside the ListBox1_Change event (because it would create an infinite loop). I tried with Application.EnableEvents = False, but it didn't help.
I think you need to put the reset of the selection in another event, like the KeyUp or MouseUp.
im having some weird things happen with some code in excel
Private Sub CommandButton2_Click()
Dim thiswb As Workbook
Set thiswb = ActiveWorkbook
Call EnterDataToSS(thiswb)
Me.Hide
BeamNoFind.Show vbModal
End Sub
the called code basically drops some values into a spreadsheet.
any left over values are populated to LISTBOX1 on BeamNoFind Userform.
Then on the BeamNoFind userform there is a button and the LISTBOX1. when you select and item from listbox1, and click the button, a third userform opens (VBMODELESS) to allow placement of the value.
below is the code of the button to show the third userform.
Private Sub CommandButton2_Click()
Dim Selected_Length As String
Dim Selected_Name As String
Dim Selected_Length_index As Integer
Selected_Length_index = BeamNoFind.ListBox1.ListIndex
With BeamNoFind.ListBox1
If .ListIndex > -1 Then
Selected_Name = .Column(0, .ListIndex)
Selected_Length = .Column(1, .ListIndex)
CellInputForm.beam_length_label.Caption = Selected_Name & " [ " & Selected_Length & " ] "
BeamNoFind.Hide
'ChngDataSrc.Hide
'Unload ChngDataSrc
CellInputForm.Show vbModeless
Else
MsgBox "No selection", vbExclamation, "Oops!"
End If
End With
End Sub
the weird thing is, when i click the button to show my modeless userform, to place the data in a cell, the initial macro is being triggered that drops you into the first userform.
Where i have the commented code 'ChngDataSrc.Hide' and 'Unload ChngDataSrc' are my attempts to stop the userform from displaying when i dont want it to.
When i unload the form, i then get an error instead of the form displaying, the error is with the initial macro:
Sub get_scheduling_data(control As IRibbonControl)
ChngDataSrc.Show
End Sub
It has something to do with vbModeless because if i replace "vbModeless" from "CellInputForm.Show vbModeless" line with "vbModal", it shows correctly, without the unwanted form (ChngDataSrc). but then the function of the form (select cell, press ok button, value placed in selected cell) is gone.
I found a solution, but its not a real solution,
i placed
ChngDataSrc.Hide in the Activate sub of the CellInputForm userform.
So when CellInputForm.show vbModeless is run, and the ChngDataSrc userform pops up unwantedly, it is then hidden again.
Id rather find out why it is being showed in the first place, but this fix seems to work for now.