【问题标题】:VBA lookup loop according to lookup count根据查找计数的 VBA 查找循环
【发布时间】:2015-07-10 03:15:45
【问题描述】:

您好,我是 VBA 编程新手 我正在尝试进行搜索查找, 是的,我可以搜索单个数据,但如果搜索计数 >1,那么我需要 做一个 msgbox,它会根据字符串存在的次数出现

我得到了这个结果:

是的,我得到了确切的结果,但它只适用于第一行查找 下一行包含 Salary: 234,871 和 SSN 241-652 怎么样?

我想我需要根据 vlookup 计数进行循环,但是该怎么做呢?

我需要查看 2x MsgBox,因为它有两个条目,所以当我单击第一个 msgbox 时,确定 然后另一个将跟随..请帮助谢谢!

这是我的代码

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

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    我会这样做:

    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
    

    【讨论】:

    • 太棒了!!!你怎么做的这么快?谢谢它有效!幸运的是我找到了 VBA 专家 ...
    • 顺便说一句,可以使用 Range 来完成吗?而不是细胞?
    • 是的。例如,您将使用 Range("C" & i) 而不是 Cells(i ,3)
    • 顺便说一句,你这里没有使用 vlookup 是速度不同吗?如果我们使用 vlookup 会更方便?
    • VLOOKUP 只是让寻找下一个事件变得很痛苦。我这样做的方式要容易得多。我不知道这里的速度会有所不同。
    【解决方案2】:

    这也可以。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-12
      • 2022-08-13
      • 1970-01-01
      • 1970-01-01
      • 2011-11-01
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多