Control label caption based on vlookup with a combobox and auto-update label

JeremyA1976

Board Regular
Joined
Aug 3, 2015
Messages
59
I am trying to auto-update label ("label9) from userform1 based on the description chosen from a combobox ("mt").
I have a worksheet ("Lists") that contains all the descriptions that populate a combobox in my userform1 starting at cell A1 and going down. Starting in cell C1, I have a corresponding prefix code for that description we use in our tracking number. When I initialize my userform1, then choose "Galvanized" (for example) from the list, I want Label9 to automatically change to its corresponding prefix code.

My userform1 initialize code:
Code:
Private Sub UserForm_Initialize()

    Dim wsLists As Worksheet
    Dim ws As Worksheet
    Dim TypeList As Variant, SizeList As Variant
    'Dim result As Variant


    
    Set wsLists = ThisWorkbook.Worksheets("lists")
    'result = Application.VLookup(mt.Value, Range("A1:C15"), 3, False)
    
    With wsLists
        TypeList = .Range(.Range("A1"), .Range("A" & .Rows.Count).End(xlUp)).Value2
        SizeList = .Range(.Range("B1"), .Range("B" & .Rows.Count).End(xlUp)).Value2
    End With


        'Dim iRow As Long
        'iRow = WorksheetFunction.Match(mt.Value, Range("metalnames").Columns(1).Cells)
        'Label9.Caption = result.Value
    
    mt.List = TypeList
    MThk.List = SizeList


    
    AddLists Me
    
       
End Sub


other code:

Code:
Sub AddLists(ByVal Form As Object)    Dim wsLists As Worksheet
    Dim TypeList As Variant, SizeList As Variant
    
    Set wsLists = ThisWorkbook.Worksheets("lists")
    With wsLists
        TypeList = .Range(.Range("A1"), .Range("A" & .Rows.Count).End(xlUp)).Value2
        SizeList = .Range(.Range("B1"), .Range("B" & .Rows.Count).End(xlUp)).Value2
    End With
    
    Form.mt.List = TypeList
    Form.MThk.List = SizeList
End Sub

I have tried and failed all morning at trying to figure this out for myself, figured it was a good time to bring in the gurus. Thanks in advance.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
How about
Code:
Private Sub mt_Change()
Me.Label9 = Application.VLookup(mt.Value, Sheets("[COLOR=#ff0000]Pcode[/COLOR]").Range("A1:C15"), 3, False)
End Sub

Private Sub UserForm_Initialize()

    Dim wsLists As Worksheet
    Dim TypeList As Variant, SizeList As Variant
    
    Set wsLists = ThisWorkbook.Worksheets("[COLOR=#ff0000]pcode[/COLOR]")
    
    With wsLists
        TypeList = .Range(.Range("A1"), .Range("A" & .Rows.Count).End(xlUp)).Value2
        SizeList = .Range(.Range("B1"), .Range("B" & .Rows.Count).End(xlUp)).Value2
    End With
    mt.List = TypeList
    MThk.List = SizeList
End Sub
 
Last edited:
Upvote 0
Thanks Fluff... The label is not changing. I have "prefix" entered into the caption in the properties bar. Does that matter? The combobox "mt" starts out blank, does that matter? where is the private sub mt_Change() called? or is it automatic? and should I put it under a standard module?
 
Upvote 0
Forgot to mention in my previous post that you need to change the sheet name ( I've now highlighted them in red).
All the code needs to go in the userform module & whenever you change the combobox, the label should automatically update
 
Last edited:
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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