find names value via combobox

hsolanki

Board Regular
Joined
Jan 16, 2020
Messages
204
Office Version
  1. 2010
Platform
  1. Windows
Morning everyone

I have got the code which perfectly working. combobox1 finds the staff number and then populates names in Combobox 1 & 5 however i would like to change this from a staff number to names find

below is the code whereby it finds it using the staff number. the staff number is in column A & names in column B

VBA Code:
Private Sub ComboBox1_Change()

    If Me.ComboBox1.Value <> "" Then
        Me.TextBox1.Value = Application.WorksheetFunction.VLookup(CLng(Me.ComboBox1.Value), ThisWorkbook.Sheets("Monthly Summary").Range("A:B"), 2, 0)
        Me.TextBox6.Value = Application.WorksheetFunction.VLookup(CLng(Me.ComboBox1.Value), ThisWorkbook.Sheets("Monthly Summary").Range("A:C"), 3, 0)
        
    Else
        Me.TextBox1.Value = ""
    End If

End Sub
 
VBA Code:
Private Sub ComboBox1_Change()
    With ThisWorkbook.Sheets("Monthly Summary")
    If Me.ComboBox1.Value <> "" Then
        Me.TextBox1.Value = WorksheetFunction.Vlookup(Me.ComboBox1.Value, .Range("A:C"), 2, 0)
        Me.TextBox6.Value = WorksheetFunction.Vlookup(Me.ComboBox1.Value, .Range("A:C"), 3, 0)
    Else
        Me.TextBox1.Value = ""
        Me.TextBox6.Value = ""
    End If
    End With
End Sub
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi I get an error when I tried the suggested code
 

Attachments

  • 1674726541946.png
    1674726541946.png
    8.6 KB · Views: 6
Upvote 0
I hope this will solve your problem:
VBA Code:
Private Sub ComboBox1_Change()
    With ThisWorkbook.Sheets("Monthly Summary")
    If Me.ComboBox1.Value <> "" Then
        Me.TextBox1.Value = WorksheetFunction.Vlookup(CLng(Me.ComboBox1.Value), .Range("A:C"), 2, 0)
        Me.TextBox6.Value = WorksheetFunction.Vlookup(CLng(Me.ComboBox1.Value), .Range("A:C"), 3, 0)
    Else
        Me.TextBox1.Value = ""
        Me.TextBox6.Value = ""
    End If
    End With
End Sub
 
Upvote 0
Solution
Hi Flash sorry, this is the final code whereby it works now


VBA Code:
Private Sub ComboBox1_Change()

  With ThisWorkbook.Sheets("Monthly Summary")
    If Me.ComboBox1.Value <> "" Then
        Me.TextBox1.Value = .Cells(WorksheetFunction.Match(Me.ComboBox1.Value, .Range("B:B"), 0), 1).Value
        Me.TextBox6.Value = WorksheetFunction.VLookup(Me.ComboBox1.Value, .Range("B:C"), 2, 0) 
    Else
        Me.TextBox1.Value = ""
    End If
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

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