Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50214

Requery a combo box

$
0
0
Hello,

I have a problem with a user form. There are two combo boxes, the second combo box list is populated with different values based on the selection on the first combo box.

The problem is once you've made a selection in the first combo box - if you change your mind and select another option the second combo box doesn't update with the new values. You have to close then open the user form to reset the comobo boxes.

Any help would be appreciated.


First combo box


Code:

Private Sub JobName_Change()
Dim job As String
Dim MaterialsList As Range
Dim lr As Long
For Each Worksheet In Worksheets
    If Worksheet.Name = UF_Despatch.JobName.Value Then
    UF_Despatch.JobNumber = Worksheet.Cells(6, 2)
    UF_Despatch.FE = Worksheet.Cells(3, 2)
    Exit For
    End If
 Next
 

For Each Worksheet In Worksheets
    If Worksheet.Name = JobName Then
    lr = Worksheet.Cells(Rows.Count, 1).End(xlUp).Row
    Set MaterialsList = Range(Worksheet.Cells(9, 1), Worksheet.Cells(lr, 1))
    Exit For
    End If
Next


For Each cell In MaterialsList
    UF_Despatch.Material1.AddItem cell
    UF_Despatch.Material2.AddItem cell
    UF_Despatch.Material3.AddItem cell
    UF_Despatch.Material4.AddItem cell
    UF_Despatch.Material5.AddItem cell
Next


End Sub



Second combo box


Code:

Private Sub Material1_Change()
Dim ws As Worksheet
Dim myrange As Range
Dim lr As Long
Dim result As Variant

For Each Worksheet In Worksheets
    If Worksheet.Name = JobName Then
    lr = Worksheet.Cells(Rows.Count, 2).End(xlUp).Row
    Set myrange = Range(Worksheet.Cells(9, 1), Worksheet.Cells(lr, 3))
    Exit For
    End If
Next
On Error Resume Next

result = CStr(Application.VLookup(Material1, myrange, 2, False))
If result = "Error 2042" Then
Ref1 = vbNullString
Else:
Ref1 = result
End If

result = CStr(Application.VLookup(Material1, myrange, 3, False))
If result = "Error 2042" Then
Limit1 = vbNullString
Else:
Limit1 = result
End If


           
End Sub

Thanks,

Viewing all articles
Browse latest Browse all 50214

Trending Articles