Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据查找计数的VBA查找循环_Vba_Excel - Fatal编程技术网

根据查找计数的VBA查找循环

根据查找计数的VBA查找循环,vba,excel,Vba,Excel,嗨,我是VBA编程新手 我正在做搜索查找, 是的,我可以搜索单个数据,但如果搜索计数大于1,则我需要 根据字符串存在的次数,执行将显示的msgbox 我得到了这个结果: 是的,我得到了准确的结果,但它只适用于第一行查找 下一行包含工资:234871和SSN 241-652如何 我想我需要根据vlookup计数循环,但怎么做呢 我需要看到2个MsgBox,因为它有两个条目,所以当我单击第一个MsgBox ok时 然后另一个会跟着来。。请帮忙,谢谢 这是我的密码 Private Sub Comm

嗨,我是VBA编程新手 我正在做搜索查找, 是的,我可以搜索单个数据,但如果搜索计数大于1,则我需要 根据字符串存在的次数,执行将显示的msgbox

我得到了这个结果:

是的,我得到了准确的结果,但它只适用于第一行查找 下一行包含工资:234871和SSN 241-652如何

我想我需要根据vlookup计数循环,但怎么做呢

我需要看到2个MsgBox,因为它有两个条目,所以当我单击第一个MsgBox ok时 然后另一个会跟着来。。请帮忙,谢谢

这是我的密码

Private Sub CommandButton2_Click()
On Error GoTo MyErrorHandler:
Dim E_name As String
E_name = InputBox("Enter the Employee Name :")
If Len(E_name) > 0 Then
For i = 1 To 3
  Sal = Application.WorksheetFunction.VLookup(E_name, Sheets("sample").Range("B3:D8"), 3, False)
  SSN = Application.WorksheetFunction.VLookup(E_name, Sheets("sample").Range("B3:D8"), 2, False)
  MsgBox "Salary is : $ " & Sal & Chr(13) & "SSN is : " & SSN
Next i
Else
  MsgBox ("You entered an invalid value")
End If
Exit Sub
MyErrorHandler:
If Err.Number = 1004 Then
  MsgBox "Employee Not Present in the table."
End If
End Sub

我会这样做:

Private Sub CommandButton2_Click()
Dim E_name As String
E_name = InputBox("Enter the Employee Name :")
If Len(E_name) > 0 Then
    lastRow = Range("C65000").End(xlUp).Row
    For i = 2 To lastRow
        If Cells(i, 2) = E_name Then
            found = 1
            MsgBox "Salary is : $ " & Cells(i, 4) & Chr(13) & "SSN is : " & Cells(i, 3)
        End If
    Next i
    If found <> 1 Then MsgBox "Employee Not Present in the table."
Else
    MsgBox ("You entered an invalid value")
End If
End Sub
Private子命令按钮2\u单击()
将E_名称设置为字符串
E_name=InputBox(“输入员工姓名:”)
如果Len(E_name)>0,则
lastRow=范围(“C65000”)。结束(xlUp)。行
对于i=2到最后一行
如果单元格(i,2)=E_名称,则
发现=1
MsgBox“工资为:$”&单元格(i,4)和Chr(13)以及“SSN为:”&单元格(i,3)
如果结束
接下来我
如果找到1,则MsgBox“表中不存在员工。”
其他的
MsgBox(“您输入的值无效”)
如果结束
端接头
这也会起作用

Private Sub CommandButton2_Click()

    Dim E_name, salary, ssn As String
    Dim row As Integer

    E_name = InputBox("Enter the Employee Name :")

    'Set the start row
    row = 3

    If Len(E_name) > 0 Then

        'Do until the name colum is blank
        Do While Sheets("sample").Range("B" & row) <> ""

            'If name are equal, show message box
            If E_name = Sheets("sample").Range("B" & row) Then

                salary = Sheets("sample").Range("D" & row)

                ssn = Sheets("sample").Range("C" & row)

                MsgBox "Salary is : $ " & salary & Chr(13) & "SSN is : " & ssn

            End If

            'Increase row
            row = row + 1

        Loop

    Else

        MsgBox ("You entered an invalid value")

    End If

End Sub
Private子命令按钮2\u单击()
Dim E_名称、薪资、ssn作为字符串
将行设置为整数
E_name=InputBox(“输入员工姓名:”)
'设置起始行
行=3
如果Len(E_name)>0,则
'执行此操作,直到名称colum为空
请勿在工作表(“样品”)中使用。范围(“B”和“行”)
'如果名称相等,则显示消息框
如果E_name=板材(“样品”)范围(“B”和行),则
工资=工作表(“样本”)。范围(“D”行和行)
ssn=板材(“样品”)。范围(“C”和行)
MsgBox“薪资为:$”&薪资和薪酬(13)以及“SSN为:”&SSN
如果结束
"增行"
行=行+1
环
其他的
MsgBox(“您输入的值无效”)
如果结束
端接头

太棒了!!!你怎么做得这么快?谢谢,这很有效!幸运的是,我找到了VBA专家…顺便问一下,它可以使用范围吗?而不是细胞?是的。例如,您可以使用范围(“C”&i)而不是单元格(i,3),因为您不使用vlookup,这里的速度不同吗?或者如果我们使用vlookup更方便?vlookup只是让寻找下一个事件变得很痛苦。我这样做容易多了。我不知道这里的速度会有什么不同。