简体   繁体   中英

Excel VBA userform : "Change Event" of Combobox when fired executes the code in the "ButtonClick Event"

I have a combobox named ReportList in userform. I am writing some code in two of its events - DropButtonClick() and Change() .

In DropButtonClick() event, the combox is cleared and repopulated with entries of column A.

In Change() event, I am filling a few textboxes with values related to the selected entry in combobox.

What is happening is, the code in DropButtonClick() event is getting executed as intended but as soon as I select an entry from the combobox drop down, instead of the Change() event getting fired, the code inside DropButtonClick() event is getting executed again. Is this a bug or am I missing something?

Private Sub ReportList_DropButtonClick()
   
    'Debug.Print "Event Triggered"  '
    Dim DB_Sheet As Worksheet
    Set DB_Sheet = ThisWorkbook.Sheets("DataBase")
    Dim LstRow As Long
    LstRow = DB_Sheet.Cells(DB_Sheet.Rows.Count, "A").End(xlUp).Row
    Dim i As Long
    ReportList.Clear
    For i = 3 To LstRow
      ReportList.AddItem (DB_Sheet.Range("A" & i).value)
    Next
      
End Sub

'-------------------------------------------------------------------------

Private Sub ReportList_Change()

  If ReportList.value <> "" Then
     ' Some code here to get the rownumber of selected entry in combobox
      ReportAuthor.value = DB_Sheet.Range(NumToLet(1) & RowNumber).value
      ReportType.value = DB_Sheet.Range(NumToLet(2) & RowNumber).value
  End if

End Sub

Please, take a look at this document which tells the sequence of events in a Combobox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM