search with Interdependent combobox

Joined
Sep 16, 2021
Messages
15
Office Version
  1. 365
Platform
  1. Windows
H
I have two combobox one for emp Id(combobox12) and one for emp name(combobox13).The values are added in combobox with change event of combobox1. if I select name from combobox13 the it display corresponding Emp id in combobox12 my code is as follows

Private Sub ComboBox13_Change()
1632471621451.png


Dim i As Long, lastrow As Long
Dim sh As Worksheet
With ActiveWorkbook.Sheets("Other_Details")
Set sh = ActiveWorkbook.Sheets("Other_Details")
lastrow = sh.Range("AH" & Rows.Count).End(xlUp).Row
For i = 4 To lastrow

If sh.Cells(i, "AH").Value = Me.ComboBox13.Value Then
Me.ComboBox12.Value = sh.Cells(i, "AG").Value

End If
Next
End With
End Sub

But if I use the code other way round it does not work.for example if I select the employee ID it does not display employee name nor it gives any error my code is as follows

1632472030656.png

Private Sub ComboBox12_Change()

Dim i As Long, lastrow As Long
Dim sh As Worksheet
With ActiveWorkbook.Sheets("Other_Details")
Set sh = ActiveWorkbook.Sheets("Other_Details")
lastrow = sh.Range("AG" & Rows.Count).End(xlUp).Row
For i = 4 To lastrow

If sh.Cells(i, "AG").Value = Me.ComboBox12.Value Then
Me.ComboBox13.Value = sh.Cells(i, "AH").Value

End If
Next
End With

the data range is in whorksheet "Other_Details" cells AG:AH
My same logic works for 2 others comboxes in same userform but I cannot figure out what is gone wrong?
can any one help?
 

Attachments

  • 1632471989762.png
    1632471989762.png
    163.8 KB · Views: 6

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try this option:

VBA Code:
  Dim sh As Worksheet   'At the beginning of all the code
  
Private Sub ComboBox12_Change()
  If ComboBox12.ListIndex = -1 Then Exit Sub
  ComboBox13.Value = sh.Range("AH" & ComboBox12.ListIndex + 4).Value
End Sub
Private Sub ComboBox13_Change()
  If ComboBox13.ListIndex = -1 Then Exit Sub
  ComboBox12.Value = sh.Range("AG" & ComboBox13.ListIndex + 4).Value
End Sub

Private Sub UserForm_Activate()
  Dim lr As Long
  Set sh = ActiveWorkbook.Sheets("Other_Details")
  lr = sh.Range("AG" & Rows.Count).End(xlUp).Row
  ComboBox12.List = sh.Range("AG4:AG" & lr).Value
  ComboBox13.List = sh.Range("AH4:AH" & lr).Value
End Sub
 
Upvote 0
I done think there is issue in the code. Same issue occurs .It displays value in combobox12 when criteria is combobox13 but gives blank in combobox 13 when criteria is combobox12.The code works on other 2 interdepended comboxes in the same userform.Is it any thing to do with property of combobox or data in the range which I have to check?.
Both comboboxes are searchable and matchentry is set to 2-frmmatchentry none.
can you guide on this

ComboBox12.Value = sh.Range("AG" & ComboBox13.ListIndex + 4).Value
 
Upvote 0
Try this:

- You must keep this property: 2-frmmatchentry none.
- If combo12 is numeric then I am using val( ) function.
- If combo13 is text, then I am using the lcase( ) function.
- I added the global variable "searching".

Copy all the code.

VBA Code:
  Dim sh As Worksheet   'At the beginning of all the code
  Dim searching As Boolean

Private Sub ComboBox12_Change()
  Dim i As Long
  If searching = True Then Exit Sub

  searching = True
    ComboBox13.Value = ""
    For i = 0 To ComboBox12.ListCount - 1
      If ComboBox12.List(i) = Val(ComboBox12.Value) Then
        ComboBox12.ListIndex = i
        ComboBox13.Value = sh.Range("AH" & ComboBox12.ListIndex + 4).Value
        Exit For
      End If
    Next
  searching = False
End Sub

Private Sub ComboBox13_Change()
  Dim i As Long
  If searching = True Then Exit Sub

  searching = True
    ComboBox12.Value = ""
    For i = 0 To ComboBox13.ListCount - 1
      If LCase(ComboBox13.List(i)) = LCase(ComboBox13.Value) Then
        ComboBox13.ListIndex = i
        ComboBox12.Value = sh.Range("AG" & ComboBox13.ListIndex + 4).Value
        Exit For
      End If
    Next
  searching = False
End Sub

Private Sub UserForm_Activate()
  Dim lr As Long
  Set sh = ActiveWorkbook.Sheets("Other_Details")
  lr = sh.Range("AG" & Rows.Count).End(xlUp).Row
  ComboBox12.List = sh.Range("AG4:AG" & lr).Value
  ComboBox13.List = sh.Range("AH4:AH" & lr).Value
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,911
Messages
6,122,196
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top