I have a command button, when i click it, it shows a customized userform which contains a combobox with items taken from a sheet cells. the user is suppose to press the command then the userform appears, he needs to select one item from the list, the item is a sheet name in different workbook, accordingly it will import the data from that sheet. however i am facing a problem: when the userform appears, i click on the combobox it shows no items, then if i click once on the userform then check the combobox again it shows the items correctly, if i click again on the userform, the combobox items will be doulbled! if i selected an item it works perfectly. So what i want to achieve is: i want the combobox to show the items directly without clicking on the userform. the code is this:
In the userform:
Private Sub UserForm_Click()
Dim mlf As Workbook
Dim adad As Long
Dim mada As String
Dim lastRow As Long
Set mlf = ActiveWorkbooklastRow = Sheet3.Cells(Rows.Count, 1).End(xlUp).Row
For adad = 1 To lastRow
mada = Sheet3.Cells(adad, 1)
With ComboBox1
.AddItem mada
End With
Next
End Sub
Private Sub cmdOkay_Click()
'Verify that an item was selected
If Me.ComboBox1.BoundValue = vbNullString Then
MsgBox "You did not choose an item!", vbOKOnly
Exit Sub
Else
MsgBox "You have selected " & Me.ComboBox1.BoundValue, vbOKOnly
Sheet3.Cells(1, 2) = Me.ComboBox1.BoundValue
End If
Unload Me
End Sub
In the CommandButton
Dim testbook As Workbook
Set testbook = ThisWorkbook
Set database = Workbooks.Open(Filename:=myPath & myFile)
testbook.Worksheets("Sheet4").Range("A1:B5").Clear
With UserForm1
.Caption = "Settings of the test"
End With
Dim lo As Integer
For lo = 1 To database.Sheets.Count
testbook.Sheets("Sheet4").Cells(lo, 1) = database.Worksheets(lo).Name
Next
UserForm1.Show
That is because you are telling it to do that on the click event.
Change
Private Sub UserForm_Click()
to
Private Sub UserForm_Initialize()
Once you change this, the combobox issue will also go away.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加