简体   繁体   English

根据名字和姓氏查找ID

[英]Finding ID based on first and last name

I have a list that contains Employee ID in Col A, First Name in Col B, Last Name in Col C. I am needing to write a macro that gives a userform to input First and Last Name and from there it will put the proper Employee ID into the first unused cell in Col E and then loop back to the userform. 我有一个列表,其中包含Col A中的Employee ID,Col B中的名字,Col C中的姓氏。我需要编写一个宏,该宏为用户提供一个输入First and Last Name的信息,然后将适当的Employee放入该宏中。将ID放入Col E中的第一个未使用的单元格中,然后循环回到用户表单。

I already know how to build the Userform and will have two buttons on it one that reads "Next" and one that reads "End". 我已经知道如何构建用户窗体,并且上面将有两个按钮,其中一个显示“ Next”,另一个显示“ End”。 The "Next" button will loop the Userform and the "End" will just close the Userform. “下一步”按钮将循环用户窗体,“结束”将仅关闭用户窗体。

Plan on leaving the Userform named Userform1 and naming the input boxes as "FirstName" and "LastName". 计划保留名为Userform1的用户窗体,并将输入框命名为“ FirstName”和“ LastName”。 So I know that to reference these from the macro I would call for Userform1.FirstName.Value or Userform1.LastName.Value depending on which part I need at the moment. 因此,我知道从宏中引用它们时,我会根据当前所需的部分来调用Userform1.FirstName.Value或Userform1.LastName.Value。

The part I am not sure on is how to the matching of two variables and then looking to the left for the ID. 我不确定的部分是如何匹配两个变量,然后在左侧查找ID。 I can move the ID Col to be after the name Cols if that helps but I am still not sure how to write so that both names must match. 如果可以,我可以将ID Col移到Cols名称之后,但是我仍然不确定如何写,以便两个名称必须匹配。

As for error trapping I planned on having a MsgBox state "There are no matching entries." 至于错误捕获,我计划使用MsgBox状态“没有匹配的条目”。 If the person does not exist in the list. 如果此人不存在于列表中。 However I am unsure of how to handle the super unlikely but possible situation of if two people on the list have the same name. 但是,我不确定如果列表中的两个人具有相同的姓名,该如何处理极不可能但可能的情况。 So any suggestions for this would be greatly appreciated. 因此,对此的任何建议将不胜感激。

I am using Excel 2013. 我正在使用Excel 2013。

Try this for the next button 尝试下一个按钮

Private Sub NextButton_Click()

Dim emptyRow As Long
Dim matchFound As Boolean
Dim matchCount As Long
Dim matchRow As Long
Dim i As Long

'Determine the first empty cell in column E
If Cells(1, 5).Value = "" Then
    emptyRow = 1
Else
    emptyRow = Cells(Columns(5).Rows.Count, 5).End(xlUp).Row + 1
End If

matchFound = False
matchCount = 0
matchRow = 0

'Loop through all of rows that have an employee id
For i = 1 To Cells(Columns(1).Rows.Count, 1).End(xlUp).Row
    If (UCase(FirstName.Value) = UCase(Cells(i, 2).Value)) And (UCase(LastName.Value) = UCase(Cells(i, 3).Value)) Then
        matchCount = matchCount + 1
        matchRow = i
        matchFound = True
    End If
Next

'Alert user of any errors
If matchFound = False Then
    MsgBox ("There are no matching entries")
ElseIf matchCount > 1 Then
    MsgBox ("There were multiple matches")
Else
'If there are no errors add employee id to the list
    Cells(emptyRow, 5).Value = Cells(matchRow, 1).Value
    emptyRow = emptyRow + 1
End If

'Clear the userform
FirstName.Text = ""
LastName.Text = ""

End Sub

I'm not sure what the best course of action to take if there are multiple matches, so for now I just included a message to alert the sure. 我不确定如果有多个匹配项,该采取的最佳措施是什么,所以现在我只包含一条消息来提醒确定。 It wouldn't be hard to change the code to track each of the matched rows instead of just the last one. 更改代码以跟踪每个匹配的行而不是仅跟踪最后一行并不困难。

I would suggest using vlookup, as it's basically designed around your problem. 我建议使用vlookup,因为它基本上是针对您的问题而设计的。 It takes an input, finds it in one column, then outputs the same row in a different column. 它接受输入,在一列中找到它,然后在另一列中输出同一行。 It shouldn't be difficult to make the output dependent on two vlookups matching to the same output. 使输出依赖于与同一输出匹配的两个vlookup并不难。

http://www.howtogeek.com/howto/13780/using-vlookup-in-excel/ http://www.howtogeek.com/howto/13780/using-vlookup-in-excel/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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